fire_http/macros/
mod.rs

1#[macro_export]
2macro_rules! impl_res_extractor {
3	($ty:ty) => {
4		impl<'a, R> $crate::extractor::Extractor<'a, R> for &'a $ty {
5			type Error = std::convert::Infallible;
6			type Prepared = ();
7
8			$crate::extractor_validate!(|validate| {
9				assert!(
10					validate.resources.exists::<$ty>(),
11					"Resource {} does not exist",
12					stringify!($ty)
13				);
14			});
15
16			$crate::extractor_prepare!();
17
18			$crate::extractor_extract!(|extract| {
19				Ok(extract.resources.get::<$ty>().unwrap())
20			});
21		}
22	};
23}
24
25#[macro_export]
26macro_rules! impl_req_extractor {
27	($ty:ty) => {
28		impl<'a> $crate::extractor::Extractor<'a, $ty> for $ty {
29			type Error = std::convert::Infallible;
30			type Prepared = ();
31
32			$crate::extractor_validate!();
33
34			$crate::extractor_prepare!();
35
36			$crate::extractor_extract!(<$ty> |extract| {
37				Ok(extract.request.take().unwrap())
38			});
39		}
40	};
41}
42
43#[macro_export]
44macro_rules! extractor_validate {
45	() => {
46		$crate::extractor_validate!(|_validate| {});
47	};
48	(|$validate:ident| $block:block) => {
49		fn validate($validate: $crate::extractor::Validate<'_>) {
50			$block
51		}
52	};
53}
54
55#[macro_export]
56macro_rules! extractor_prepare {
57	() => {
58		$crate::extractor_prepare!(|_prepare| { Ok(()) });
59	};
60	(|$prepare:ident| $block:block) => {
61		fn prepare(
62			$prepare: $crate::extractor::Prepare<'_>,
63		) -> std::pin::Pin<
64			std::boxed::Box<
65				dyn std::future::Future<
66						Output = std::result::Result<
67							Self::Prepared,
68							Self::Error,
69						>,
70					> + Send
71					+ '_,
72			>,
73		> {
74			Box::pin(async move { $block })
75		}
76	};
77}
78
79#[macro_export]
80macro_rules! extractor_extract {
81	// () => {
82	// 	$crate::extractor_prepare!(|_prepare| { Ok(()) });
83	// };
84	(|$extract:ident| $block:block) => {
85		$crate::extractor_extract!(<R> |$extract| $block);
86	};
87	(<$r:ty> |$extract:ident| $block:block) => {
88		fn extract(
89			$extract: $crate::extractor::Extract<'a, '_, Self::Prepared, $r>,
90		) -> std::result::Result<Self, Self::Error>
91		where
92			Self: Sized,
93		{
94			$block
95		}
96	};
97}