rpc_router/handler/
impl_handlers.rs1use crate::Resources;
2
3#[macro_export]
6macro_rules! impl_handler_pair {
7 ($K:ty, $($T:ident),*) => {
8
9 impl<F, Fut, $($T,)* P, R, E> $crate::Handler<($($T,)*), (P,), R> for F
11 where
12 F: FnOnce($($T,)* P) -> Fut + Clone + Send + 'static,
13 $( $T: $crate::FromResources+ Clone + Send + Sync + 'static, )*
14 P: $crate::IntoParams + Send + Sync + 'static,
15 R: serde::Serialize + Send + Sync + 'static,
16 E: $crate::IntoHandlerError,
17 Fut: futures::Future<Output = core::result::Result<R, E>> + Send,
18 {
19 type Future = $crate::handler::PinFutureValue;
20
21 #[allow(unused)] fn call(
23 self,
24 resources: Resources,
25 params_value: Option<serde_json::Value>,
26 ) -> Self::Future {
27 Box::pin(async move {
28 let param = P::into_params(params_value)?;
29
30 let res = self(
31 $( $T::from_resources(&resources)?, )*
32 param,
33 ).await;
34
35 match res {
36 Ok(result) => Ok(serde_json::to_value(result).map_err($crate::Error::HandlerResultSerialize)?),
37 Err(ex) => {
38 let he = $crate::IntoHandlerError::into_handler_error(ex);
39 Err(he.into())
40 },
41 }
42 })
43 }
44 }
45
46 impl<F, Fut, $($T,)* R, E> $crate::Handler<($($T,)*), (), R> for F
48 where
49 F: FnOnce($($T,)*) -> Fut + Clone + Send + 'static,
50 $( $T: $crate::FromResources + Clone + Send + Sync + 'static, )*
51 R: serde::Serialize + Send + Sync + 'static,
52 E: $crate::IntoHandlerError,
53 Fut: futures::Future<Output = core::result::Result<R, E>> + Send,
54 {
55 type Future = $crate::handler::PinFutureValue;
56
57 #[allow(unused)] fn call(
59 self,
60 resources: Resources,
61 _params: Option<serde_json::Value>,
62 ) -> Self::Future {
63 Box::pin(async move {
64 let res = self(
65 $( $T::from_resources(&resources)?, )*
66 ).await;
67
68 match res {
69 Ok(result) => Ok(serde_json::to_value(result).map_err($crate::Error::HandlerResultSerialize)?),
70 Err(ex) => {
71 let he = $crate::IntoHandlerError::into_handler_error(ex);
72 Err(he.into())
73 },
74 }
75
76 })
77 }
78 }
79 };
80
81}
82
83impl_handler_pair!(Resources,);
84impl_handler_pair!(Resources, T1);
85impl_handler_pair!(Resources, T1, T2);
86impl_handler_pair!(Resources, T1, T2, T3);
87impl_handler_pair!(Resources, T1, T2, T3, T4);
88impl_handler_pair!(Resources, T1, T2, T3, T4, T5);
89impl_handler_pair!(Resources, T1, T2, T3, T4, T5, T6);
90impl_handler_pair!(Resources, T1, T2, T3, T4, T5, T6, T7);
91impl_handler_pair!(Resources, T1, T2, T3, T4, T5, T6, T7, T8);