mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-03 00:00:07 +02:00
Move things around a bit
This commit is contained in:
+54
-41
@@ -122,47 +122,6 @@ impl IntoResponse<Body> for std::borrow::Cow<'static, [u8]> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Json<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse<Body> for Json<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn into_response(self) -> Response<Body> {
|
||||
let bytes = match serde_json::to_vec(&self.0) {
|
||||
Ok(res) => res,
|
||||
Err(err) => {
|
||||
return Response::builder()
|
||||
.header(header::CONTENT_TYPE, "text/plain")
|
||||
.body(Body::from(err.to_string()))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
let mut res = Response::new(Body::from(bytes));
|
||||
res.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static("application/json"),
|
||||
);
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Html<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse<Body> for Html<T>
|
||||
where
|
||||
T: Into<Bytes>,
|
||||
{
|
||||
fn into_response(self) -> Response<Body> {
|
||||
let bytes = self.0.into();
|
||||
let mut res = Response::new(Body::from(bytes));
|
||||
res.headers_mut()
|
||||
.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html"));
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
impl<B> IntoResponse<B> for StatusCode
|
||||
where
|
||||
B: Default,
|
||||
@@ -195,3 +154,57 @@ where
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Html<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse<Body> for Html<T>
|
||||
where
|
||||
T: Into<Body>,
|
||||
{
|
||||
fn into_response(self) -> Response<Body> {
|
||||
let mut res = Response::new(self.0.into());
|
||||
res.headers_mut()
|
||||
.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html"));
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Json<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse<Body> for Json<T>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
fn into_response(self) -> Response<Body> {
|
||||
let bytes = match serde_json::to_vec(&self.0) {
|
||||
Ok(res) => res,
|
||||
Err(err) => {
|
||||
return Response::builder()
|
||||
.header(header::CONTENT_TYPE, "text/plain")
|
||||
.body(Body::from(err.to_string()))
|
||||
.unwrap();
|
||||
}
|
||||
};
|
||||
|
||||
let mut res = Response::new(Body::from(bytes));
|
||||
res.headers_mut().insert(
|
||||
header::CONTENT_TYPE,
|
||||
HeaderValue::from_static("application/json"),
|
||||
);
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Text<T>(pub T);
|
||||
|
||||
impl<T> IntoResponse<Body> for Text<T>
|
||||
where
|
||||
T: Into<Body>,
|
||||
{
|
||||
fn into_response(self) -> Response<Body> {
|
||||
let mut res = Response::new(self.0.into());
|
||||
res.headers_mut()
|
||||
.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
|
||||
res
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user