rama_http/service/web/endpoint/response/
headers.rs

1use super::{IntoResponse, IntoResponseParts, ResponseParts};
2use crate::Response;
3use crate::headers::{Header, HeaderMapExt};
4use rama_utils::macros::all_the_tuples_no_last_special_case;
5
6/// Use typed [`Header`]s i a response.
7pub struct Headers<T>(pub T);
8
9impl<H: Header> Headers<(H,)> {
10    /// Create a Header singleton tuple.
11    pub fn single(h: H) -> Self {
12        Self((h,))
13    }
14}
15
16macro_rules! headers_into_response {
17    ( $($ty:ident),* $(,)? ) => {
18        #[allow(non_snake_case)]
19        impl<$($ty),+> IntoResponse for Headers<($($ty),+,)>
20        where
21            $(
22                $ty: $crate::headers::Header,
23            )+
24        {
25            fn into_response(self) -> Response {
26                (self, ()).into_response()
27            }
28        }
29    }
30}
31
32all_the_tuples_no_last_special_case!(headers_into_response);
33
34macro_rules! headers_into_response_parts {
35    ( $($ty:ident),* $(,)? ) => {
36        #[allow(non_snake_case)]
37        impl<$($ty),+> IntoResponseParts for Headers<($($ty),+,)>
38        where
39            $(
40                $ty: $crate::headers::Header,
41            )+
42        {
43            type Error = std::convert::Infallible;
44
45            fn into_response_parts(self, mut res: ResponseParts) -> Result<ResponseParts, Self::Error> {
46                let Headers((
47                    $($ty),+
48                    ,
49                )) = self;
50                $(
51                    res.headers_mut().typed_insert($ty);
52                )+
53                Ok(res)
54            }
55        }
56    }
57}
58
59all_the_tuples_no_last_special_case!(headers_into_response_parts);