use crate::body::{empty_body, full_body, Response};
use bytes::Bytes;
use http::StatusCode;
pub trait IntoResponse {
fn into_response(self) -> Response;
}
impl IntoResponse for Response {
fn into_response(self) -> Response {
self
}
}
impl IntoResponse for StatusCode {
fn into_response(self) -> Response {
let mut response = Response::new(empty_body());
*response.status_mut() = self;
response
}
}
impl IntoResponse for &'static str {
fn into_response(self) -> Response {
Response::new(full_body(Bytes::from_static(self.as_bytes())))
}
}
impl IntoResponse for String {
fn into_response(self) -> Response {
Response::new(full_body(Bytes::from(self)))
}
}
impl<T: IntoResponse> IntoResponse for (StatusCode, T) {
fn into_response(self) -> Response {
let (status, inner) = self;
let mut response = inner.into_response();
*response.status_mut() = status;
response
}
}
impl IntoResponse for () {
fn into_response(self) -> Response {
let mut response = Response::new(empty_body());
*response.status_mut() = StatusCode::NO_CONTENT;
response
}
}
impl IntoResponse for std::convert::Infallible {
fn into_response(self) -> Response {
match self {}
}
}
impl<T: IntoResponse, E: IntoResponse> IntoResponse for Result<T, E> {
fn into_response(self) -> Response {
match self {
Ok(value) => value.into_response(),
Err(error) => error.into_response(),
}
}
}