use crate::{BoxBody, Error, error::RequestContext};
use bytes::Bytes;
use http::{Response, StatusCode, header};
use http_body_util::{BodyExt, Full};
pub trait IntoResponse {
fn into_response(self) -> Response<BoxBody>;
}
impl IntoResponse for Response<BoxBody> {
fn into_response(self) -> Response<BoxBody> {
self
}
}
impl IntoResponse for String {
fn into_response(self) -> Response<BoxBody> {
let body = Full::new(Bytes::from(self)).map_err(|e| match e {}).boxed();
Response::builder()
.status(StatusCode::OK)
.header(header::CONTENT_TYPE, "text/plain; charset=utf-8")
.body(body)
.unwrap()
}
}
impl IntoResponse for &'static str {
fn into_response(self) -> Response<BoxBody> {
self.to_string().into_response()
}
}
impl IntoResponse for Error {
fn into_response(self) -> Response<BoxBody> {
let temporary_context: RequestContext =
("/unknown", "unknown", uuid::Uuid::new_v4()).into();
self.into_response_with_context(&temporary_context)
}
}
impl<R, E> IntoResponse for Result<R, E>
where
R: IntoResponse,
E: IntoResponse,
{
fn into_response(self) -> Response<BoxBody> {
match self {
Ok(res) => res.into_response(),
Err(err) => err.into_response(),
}
}
}
impl IntoResponse for serde_json::Value {
fn into_response(self) -> Response<BoxBody> {
let json_string = self.to_string();
let body_bytes = Bytes::from(json_string);
let boxed_body = Full::new(body_bytes)
.map_err(|e| match e {}) .boxed();
Response::builder()
.status(StatusCode::OK)
.header("Content-Type", "application/json")
.body(boxed_body)
.unwrap()
}
}