Files
axum/src/response.rs
T

195 lines
4.6 KiB
Rust
Raw Normal View History

2021-05-31 22:54:21 +02:00
use crate::Body;
2021-05-30 13:24:03 +02:00
use bytes::Bytes;
2021-06-01 08:32:58 +02:00
use http::{header, HeaderMap, HeaderValue, Response, StatusCode};
2021-05-30 13:24:03 +02:00
use serde::Serialize;
2021-06-06 15:19:54 +02:00
use std::{borrow::Cow, convert::Infallible};
2021-06-01 08:32:58 +02:00
use tower::util::Either;
2021-05-30 13:24:03 +02:00
2021-06-06 22:41:52 +02:00
pub trait IntoResponse {
fn into_response(self) -> Response<Body>;
2021-05-31 22:54:21 +02:00
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for () {
2021-06-06 15:19:54 +02:00
fn into_response(self) -> Response<Body> {
Response::new(Body::empty())
2021-05-31 22:54:21 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for Infallible {
fn into_response(self) -> Response<Body> {
2021-05-31 22:54:21 +02:00
match self {}
}
2021-05-30 13:24:03 +02:00
}
2021-06-06 22:41:52 +02:00
impl<T, K> IntoResponse for Either<T, K>
2021-05-31 20:42:57 +02:00
where
2021-06-06 22:41:52 +02:00
T: IntoResponse,
K: IntoResponse,
2021-05-31 20:42:57 +02:00
{
2021-06-06 22:41:52 +02:00
fn into_response(self) -> Response<Body> {
2021-05-31 22:54:21 +02:00
match self {
Either::A(inner) => inner.into_response(),
Either::B(inner) => inner.into_response(),
}
}
}
2021-06-06 22:41:52 +02:00
impl<T, E> IntoResponse for Result<T, E>
2021-05-31 22:54:21 +02:00
where
2021-06-06 22:41:52 +02:00
T: IntoResponse,
E: IntoResponse,
2021-05-31 22:54:21 +02:00
{
2021-06-06 22:41:52 +02:00
fn into_response(self) -> Response<Body> {
2021-05-31 22:54:21 +02:00
match self {
Ok(value) => value.into_response(),
Err(err) => err.into_response(),
}
2021-05-31 20:42:57 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for Response<Body> {
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Self {
self
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for &'static str {
2021-06-06 15:19:54 +02:00
#[inline]
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-06-06 15:19:54 +02:00
Cow::Borrowed(self).into_response()
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for String {
2021-06-06 15:19:54 +02:00
#[inline]
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-06-06 15:19:54 +02:00
Cow::<'static, str>::Owned(self).into_response()
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for std::borrow::Cow<'static, str> {
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-06-06 15:19:54 +02:00
let mut res = Response::new(Body::from(self));
res.headers_mut()
.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/plain"));
res
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for Bytes {
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-05-30 14:33:20 +02:00
let mut res = Response::new(Body::from(self));
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
2021-05-31 22:54:21 +02:00
res
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for &'static [u8] {
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-05-30 14:33:20 +02:00
let mut res = Response::new(Body::from(self));
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
2021-05-31 22:54:21 +02:00
res
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for Vec<u8> {
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-05-30 14:33:20 +02:00
let mut res = Response::new(Body::from(self));
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
2021-05-31 22:54:21 +02:00
res
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for std::borrow::Cow<'static, [u8]> {
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-05-30 14:33:20 +02:00
let mut res = Response::new(Body::from(self));
res.headers_mut().insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/octet-stream"),
);
2021-05-31 22:54:21 +02:00
res
2021-05-30 13:24:03 +02:00
}
}
2021-06-06 22:41:52 +02:00
impl IntoResponse for StatusCode {
2021-06-06 15:19:54 +02:00
fn into_response(self) -> Response<Body> {
Response::builder()
.status(self)
.body(Body::empty())
.unwrap()
2021-06-06 11:37:08 +02:00
}
}
2021-05-30 13:24:03 +02:00
2021-06-06 22:41:52 +02:00
impl<T> IntoResponse for (StatusCode, T)
2021-05-30 13:24:03 +02:00
where
2021-06-06 11:37:08 +02:00
T: Into<Body>,
2021-05-30 13:24:03 +02:00
{
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-06-06 11:37:08 +02:00
Response::builder()
.status(self.0)
.body(self.1.into())
.unwrap()
}
}
2021-05-31 22:54:21 +02:00
2021-06-06 22:41:52 +02:00
impl<T> IntoResponse for (StatusCode, HeaderMap, T)
2021-06-06 11:37:08 +02:00
where
T: Into<Body>,
{
fn into_response(self) -> Response<Body> {
let mut res = Response::new(self.2.into());
*res.status_mut() = self.0;
*res.headers_mut() = self.1;
2021-05-31 22:54:21 +02:00
res
2021-05-30 14:33:20 +02:00
}
}
2021-05-30 13:24:03 +02:00
2021-05-31 10:20:07 +02:00
pub struct Html<T>(pub T);
2021-06-06 22:41:52 +02:00
impl<T> IntoResponse for Html<T>
2021-05-31 10:20:07 +02:00
where
2021-06-06 11:37:08 +02:00
T: Into<Body>,
2021-05-31 10:20:07 +02:00
{
2021-05-31 22:54:21 +02:00
fn into_response(self) -> Response<Body> {
2021-06-06 11:37:08 +02:00
let mut res = Response::new(self.0.into());
2021-05-31 10:20:07 +02:00
res.headers_mut()
.insert(header::CONTENT_TYPE, HeaderValue::from_static("text/html"));
2021-05-31 22:54:21 +02:00
res
2021-05-31 10:20:07 +02:00
}
}
2021-06-06 11:37:08 +02:00
pub struct Json<T>(pub T);
2021-06-01 00:34:09 +02:00
2021-06-06 22:41:52 +02:00
impl<T> IntoResponse for Json<T>
2021-06-01 00:34:09 +02:00
where
2021-06-06 11:37:08 +02:00
T: Serialize,
2021-06-01 00:34:09 +02:00
{
fn into_response(self) -> Response<Body> {
2021-06-06 11:37:08 +02:00
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
2021-06-01 00:34:09 +02:00
}
}