1use std::future::Future;
2use futures_util::future::BoxFuture;
3use teo_result::Result;
4use crate::request::Request;
5use crate::request::extract::ExtractFromRequest;
6use crate::response::Response;
7
8pub trait HandlerCtxArgument<A>: Send + Sync {
9 fn call(&self, request: Request) -> BoxFuture<'static, Result<Response>>;
10}
11
12impl<F, Fut> HandlerCtxArgument<()> for F where
13 F: Fn() -> Fut + Sync + Send,
14 Fut: Future<Output = Result<Response>> + Send + 'static {
15 fn call(&self, _request: Request) -> BoxFuture<'static, Result<Response>> {
16 Box::pin(self())
17 }
18}
19
20impl<A0, F, Fut> HandlerCtxArgument<(A0,)> for F where
21 A0: ExtractFromRequest + Send + Sync + 'static,
22 F: Fn(A0) -> Fut + Sync + Send,
23 Fut: Future<Output = Result<Response>> + Send + 'static {
24 fn call(&self, request: Request) -> BoxFuture<'static, Result<Response>> {
25 let value: A0 = ExtractFromRequest::extract(&request);
26 Box::pin(self(value))
27 }
28}
29
30impl<A0, A1, F, Fut> HandlerCtxArgument<(A0, A1)> for F where
31 A0: ExtractFromRequest + Send + Sync + 'static,
32 A1: ExtractFromRequest + Send + Sync + 'static,
33 F: Fn(A0, A1) -> Fut + Sync + Send,
34 Fut: Future<Output = Result<Response>> + Send + 'static {
35 fn call(&self, request: Request) -> BoxFuture<'static, Result<Response>> {
36 let value0: A0 = ExtractFromRequest::extract(&request);
37 let value1: A1 = ExtractFromRequest::extract(&request);
38 Box::pin(self(value0, value1))
39 }
40}
41
42impl<A0, A1, A2, F, Fut> HandlerCtxArgument<(A0, A1, A2)> for F where
43 A0: ExtractFromRequest + Send + Sync + 'static,
44 A1: ExtractFromRequest + Send + Sync + 'static,
45 A2: ExtractFromRequest + Send + Sync + 'static,
46 F: Fn(A0, A1, A2) -> Fut + Sync + Send,
47 Fut: Future<Output = Result<Response>> + Send + 'static {
48 fn call(&self, request: Request) -> BoxFuture<'static, Result<Response>> {
49 let value0: A0 = ExtractFromRequest::extract(&request);
50 let value1: A1 = ExtractFromRequest::extract(&request);
51 let value2: A2 = ExtractFromRequest::extract(&request);
52 Box::pin(self(value0, value1, value2))
53 }
54}
55
56impl<A0, A1, A2, A3, F, Fut> HandlerCtxArgument<(A0, A1, A2, A3)> for F where
57 A0: ExtractFromRequest + Send + Sync + 'static,
58 A1: ExtractFromRequest + Send + Sync + 'static,
59 A2: ExtractFromRequest + Send + Sync + 'static,
60 A3: ExtractFromRequest + Send + Sync + 'static,
61 F: Fn(A0, A1, A2, A3) -> Fut + Sync + Send,
62 Fut: Future<Output = Result<Response>> + Send + 'static {
63 fn call(&self, request: Request) -> BoxFuture<'static, Result<Response>> {
64 let value0: A0 = ExtractFromRequest::extract(&request);
65 let value1: A1 = ExtractFromRequest::extract(&request);
66 let value2: A2 = ExtractFromRequest::extract(&request);
67 let value3: A3 = ExtractFromRequest::extract(&request);
68 Box::pin(self(value0, value1, value2, value3))
69 }
70}
71
72impl<A0, A1, A2, A3, A4, F, Fut> HandlerCtxArgument<(A0, A1, A2, A3, A4)> for F where
73 A0: ExtractFromRequest + Send + Sync + 'static,
74 A1: ExtractFromRequest + Send + Sync + 'static,
75 A2: ExtractFromRequest + Send + Sync + 'static,
76 A3: ExtractFromRequest + Send + Sync + 'static,
77 A4: ExtractFromRequest + Send + Sync + 'static,
78 F: Fn(A0, A1, A2, A3, A4) -> Fut + Sync + Send,
79 Fut: Future<Output = Result<Response>> + Send + 'static {
80 fn call(&self, request: Request) -> BoxFuture<'static, Result<Response>> {
81 let value0: A0 = ExtractFromRequest::extract(&request);
82 let value1: A1 = ExtractFromRequest::extract(&request);
83 let value2: A2 = ExtractFromRequest::extract(&request);
84 let value3: A3 = ExtractFromRequest::extract(&request);
85 let value4: A4 = ExtractFromRequest::extract(&request);
86 Box::pin(self(value0, value1, value2, value3, value4))
87 }
88}