micro_web/extract/
extract_tuple.rs1use 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 pub enum $either<$($param,)*> {
24 $(
25 $param($param),
26 )*
27 }
28
29 impl<$($param,)*> $either<$($param,)*> {
30 $(
31 #[allow(dead_code)]
32 #[allow(non_snake_case)]
33 fn $param($param: $param) -> Self {
34 $either::$param($param)
35 }
36 )*
37 }
38
39 impl<$($param,)*> Responder for $either<$($param,)*>
40 where
41 $(
42 $param: Responder,
43 )*
44 {
45 #[allow(non_snake_case)]
46 fn response_to(self, req: &RequestContext) -> Response<ResponseBody> {
47 match self {
48 $(
49 $either::$param($param) => $param.response_to(req),
50 )*
51 }
52 }
53 }
54 }
55}
56
57impl_from_request_for_fn! { EitherA, A }
58impl_from_request_for_fn! { EitherAB, A B}
59impl_from_request_for_fn! { EitherABC, A B C}
60impl_from_request_for_fn! { EitherABCD, A B C D }
61impl_from_request_for_fn! { EitherABCDE, A B C D E }
62impl_from_request_for_fn! { EitherABCDEF, A B C D E F }
63impl_from_request_for_fn! { EitherABCDEFG, A B C D E F G }
64impl_from_request_for_fn! { EitherABCDEFGH, A B C D E F G H }
65impl_from_request_for_fn! { EitherABCDEFGHI, A B C D E F G H I }
66impl_from_request_for_fn! { EitherABCDEFGHIJ, A B C D E F G H I J }
67impl_from_request_for_fn! { EitherABCDEFGHIJK, A B C D E F G H I J K }
68impl_from_request_for_fn! { EitherABCDEFGHIJKL, A B C D E F G H I J K L }