Skip to main content

rama_http/layer/
into_response.rs

1use rama_core::{Layer, Service};
2use rama_http_types::{Request, Response};
3
4use crate::service::web::response::IntoResponse;
5
6/// A [`Service`] that maps Output of an inner service using [`IntoResponse`].
7#[derive(Debug, Clone)]
8pub struct IntoResponseService<S>(S);
9
10impl<S> IntoResponseService<S>
11where
12    S: Service<Request, Output: IntoResponse>,
13{
14    /// Create a new [`IntoResponseService`] with the given service.
15    #[inline(always)]
16    pub fn new(svc: S) -> Self {
17        Self(svc)
18    }
19}
20
21impl<S, I> Service<I> for IntoResponseService<S>
22where
23    S: Service<I, Output: IntoResponse>,
24    I: Send + 'static,
25{
26    type Output = Response;
27    type Error = S::Error;
28
29    async fn serve(&self, req: I) -> Result<Self::Output, Self::Error> {
30        self.0.serve(req).await.map(IntoResponse::into_response)
31    }
32}
33
34/// A [`Layer`] that maps Output of an inner service using [`IntoResponse`].
35#[derive(Debug, Clone, Default)]
36#[non_exhaustive]
37pub struct IntoResponseLayer;
38
39impl IntoResponseLayer {
40    pub fn new() -> Self {
41        Self
42    }
43}
44
45impl<S> Layer<S> for IntoResponseLayer {
46    type Service = IntoResponseService<S>;
47
48    fn layer(&self, inner: S) -> Self::Service {
49        IntoResponseService(inner)
50    }
51}