1use crate::error::ServerErrorKind;
2use crate::header::Mime;
3use crate::{Body, Error, Response};
4
5use serde::Serialize;
6
7pub trait IntoRouteResult<T> {
8 fn into_route_result(self) -> crate::Result<T>;
9}
10
11impl<T> IntoRouteResult<T> for crate::Result<T>
12where
13 T: Serialize,
14{
15 fn into_route_result(self) -> crate::Result<T> {
16 self
17 }
18}
19
20impl<T> IntoRouteResult<T> for T
21where
22 T: Serialize,
23{
24 fn into_route_result(self) -> crate::Result<T> {
25 Ok(self)
26 }
27}
28
29pub fn serialize_to_response<T: ?Sized>(data: &T) -> crate::Result<Response>
30where
31 T: Serialize,
32{
33 let body = Body::serialize(data)
34 .map_err(|e| Error::new(ServerErrorKind::InternalServerError, e))?;
35
36 let resp = Response::builder()
37 .content_type(Mime::JSON)
38 .body(body)
39 .build();
40
41 Ok(resp)
42}