micro_web/extract/
extract_tuple.rs

1use crate::body::OptionReqBody;
2use crate::extract::from_request::FromRequest;
3use crate::responder::Responder;
4use crate::{RequestContext, ResponseBody};
5use http::Response;
6
7macro_rules! impl_from_request_for_fn {
8    ($either:ident, $($param:ident)*) => {
9        impl<$($param,)*> FromRequest for ($($param,)*)
10        where
11            $($param: FromRequest,)*
12            $(for <'any> $param::Output<'any>: Send,)*
13        {
14            type Output<'r> = ($($param::Output<'r>,)*);
15            type Error = $either<$($param::Error,)*>;
16
17            #[allow(non_snake_case)]
18            async fn from_request<'r>(req: &'r RequestContext<'_, '_>, body: OptionReqBody) -> Result<Self::Output<'r>, Self::Error> {
19                Ok(($($param::from_request(req, body.clone()).await.map_err($either::$param)?,)*))
20            }
21        }
22
23        #[derive(Debug)]
24        pub enum $either<$($param,)*> {
25            $(
26            $param($param),
27            )*
28        }
29
30        impl<$($param,)*> $either<$($param,)*> {
31            $(
32            #[allow(dead_code)]
33            #[allow(non_snake_case)]
34            fn $param($param: $param) -> Self {
35                $either::$param($param)
36            }
37            )*
38        }
39
40        impl<$($param,)*> Responder for $either<$($param,)*>
41            where
42                $(
43                $param: Responder,
44                )*
45        {
46            #[allow(non_snake_case)]
47            fn response_to(self, req: &RequestContext) -> Response<ResponseBody> {
48                match self {
49                    $(
50                        $either::$param($param) => $param.response_to(req),
51                    )*
52                }
53            }
54        }
55    }
56}
57
58impl_from_request_for_fn! { EitherA, A }
59impl_from_request_for_fn! { EitherAB, A B}
60impl_from_request_for_fn! { EitherABC, A B C}
61impl_from_request_for_fn! { EitherABCD, A B C D }
62impl_from_request_for_fn! { EitherABCDE, A B C D E }
63impl_from_request_for_fn! { EitherABCDEF, A B C D E F }
64impl_from_request_for_fn! { EitherABCDEFG, A B C D E F G }
65impl_from_request_for_fn! { EitherABCDEFGH, A B C D E F G H }
66impl_from_request_for_fn! { EitherABCDEFGHI, A B C D E F G H I }
67impl_from_request_for_fn! { EitherABCDEFGHIJ, A B C D E F G H I J }
68impl_from_request_for_fn! { EitherABCDEFGHIJK, A B C D E F G H I J K }
69impl_from_request_for_fn! { EitherABCDEFGHIJKL, A B C D E F G H I J K L }