1use std::future::Future;
18use std::pin::Pin;
19
20use crate::body::{BoxBody, Request, Response};
21use crate::extract::{FromRequest, FromRequestParts};
22use crate::into_response::IntoResponse;
23
24pub trait Handler<T, S>: Clone + Send + Sized + 'static {
30 type Future: Future<Output = Response> + Send + 'static;
32
33 fn call(self, req: Request, state: S) -> Self::Future;
35}
36
37impl<F, Fut, R, S> Handler<(), S> for F
46where
47 F: Fn() -> Fut + Clone + Send + 'static,
48 Fut: Future<Output = R> + Send + 'static,
49 R: IntoResponse,
50{
51 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
52
53 fn call(self, _req: Request, _state: S) -> Self::Future {
54 Box::pin(async move { (self)().await.into_response() })
55 }
56}
57
58impl<F, Fut, R, S, E> Handler<(E,), S> for F
78where
79 F: Fn(E) -> Fut + Clone + Send + 'static,
80 Fut: Future<Output = R> + Send + 'static,
81 R: IntoResponse,
82 S: Clone + Send + 'static,
83 E: FromRequest<S, BoxBody> + Send + 'static,
84{
85 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
86
87 fn call(self, req: Request, state: S) -> Self::Future {
88 Box::pin(async move {
89 let extracted = match E::from_request(req, &state).await {
90 Ok(v) => v,
91 Err(rejection) => return rejection.into_response(),
92 };
93 (self)(extracted).await.into_response()
94 })
95 }
96}
97
98impl<F, Fut, R, S, A, B> Handler<(A, B), S> for F
107where
108 F: Fn(A, B) -> Fut + Clone + Send + 'static,
109 Fut: Future<Output = R> + Send + 'static,
110 R: IntoResponse,
111 S: Clone + Send + 'static,
112 A: FromRequestParts<S> + Send + 'static,
113 B: FromRequest<S, BoxBody> + Send + 'static,
114{
115 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
116
117 fn call(self, req: Request, state: S) -> Self::Future {
118 Box::pin(async move {
119 let (mut parts, body) = req.into_parts();
120 let a = match A::from_request_parts(&mut parts, &state).await {
121 Ok(v) => v,
122 Err(e) => return e.into_response(),
123 };
124 let b = match B::from_request(Request::from_parts(parts, body), &state).await {
125 Ok(v) => v,
126 Err(e) => return e.into_response(),
127 };
128 (self)(a, b).await.into_response()
129 })
130 }
131}
132
133impl<F, Fut, R, S, A, B, C> Handler<(A, B, C), S> for F
139where
140 F: Fn(A, B, C) -> Fut + Clone + Send + 'static,
141 Fut: Future<Output = R> + Send + 'static,
142 R: IntoResponse,
143 S: Clone + Send + 'static,
144 A: FromRequestParts<S> + Send + 'static,
145 B: FromRequestParts<S> + Send + 'static,
146 C: FromRequest<S, BoxBody> + Send + 'static,
147{
148 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
149
150 fn call(self, req: Request, state: S) -> Self::Future {
151 Box::pin(async move {
152 let (mut parts, body) = req.into_parts();
153 let a = match A::from_request_parts(&mut parts, &state).await {
154 Ok(v) => v,
155 Err(e) => return e.into_response(),
156 };
157 let b = match B::from_request_parts(&mut parts, &state).await {
158 Ok(v) => v,
159 Err(e) => return e.into_response(),
160 };
161 let c = match C::from_request(Request::from_parts(parts, body), &state).await {
162 Ok(v) => v,
163 Err(e) => return e.into_response(),
164 };
165 (self)(a, b, c).await.into_response()
166 })
167 }
168}
169
170impl<F, Fut, R, S, A, B, C, D> Handler<(A, B, C, D), S> for F
175where
176 F: Fn(A, B, C, D) -> Fut + Clone + Send + 'static,
177 Fut: Future<Output = R> + Send + 'static,
178 R: IntoResponse,
179 S: Clone + Send + 'static,
180 A: FromRequestParts<S> + Send + 'static,
181 B: FromRequestParts<S> + Send + 'static,
182 C: FromRequestParts<S> + Send + 'static,
183 D: FromRequest<S, BoxBody> + Send + 'static,
184{
185 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
186
187 fn call(self, req: Request, state: S) -> Self::Future {
188 Box::pin(async move {
189 let (mut parts, body) = req.into_parts();
190 let a = match A::from_request_parts(&mut parts, &state).await {
191 Ok(v) => v,
192 Err(e) => return e.into_response(),
193 };
194 let b = match B::from_request_parts(&mut parts, &state).await {
195 Ok(v) => v,
196 Err(e) => return e.into_response(),
197 };
198 let c = match C::from_request_parts(&mut parts, &state).await {
199 Ok(v) => v,
200 Err(e) => return e.into_response(),
201 };
202 let d = match D::from_request(Request::from_parts(parts, body), &state).await {
203 Ok(v) => v,
204 Err(e) => return e.into_response(),
205 };
206 (self)(a, b, c, d).await.into_response()
207 })
208 }
209}
210
211impl<F, Fut, R, S, A, B, C, D, E> Handler<(A, B, C, D, E), S> for F
216where
217 F: Fn(A, B, C, D, E) -> Fut + Clone + Send + 'static,
218 Fut: Future<Output = R> + Send + 'static,
219 R: IntoResponse,
220 S: Clone + Send + 'static,
221 A: FromRequestParts<S> + Send + 'static,
222 B: FromRequestParts<S> + Send + 'static,
223 C: FromRequestParts<S> + Send + 'static,
224 D: FromRequestParts<S> + Send + 'static,
225 E: FromRequest<S, BoxBody> + Send + 'static,
226{
227 type Future = Pin<Box<dyn Future<Output = Response> + Send>>;
228
229 fn call(self, req: Request, state: S) -> Self::Future {
230 Box::pin(async move {
231 let (mut parts, body) = req.into_parts();
232 let a = match A::from_request_parts(&mut parts, &state).await {
233 Ok(v) => v,
234 Err(e) => return e.into_response(),
235 };
236 let b = match B::from_request_parts(&mut parts, &state).await {
237 Ok(v) => v,
238 Err(e) => return e.into_response(),
239 };
240 let c = match C::from_request_parts(&mut parts, &state).await {
241 Ok(v) => v,
242 Err(e) => return e.into_response(),
243 };
244 let d = match D::from_request_parts(&mut parts, &state).await {
245 Ok(v) => v,
246 Err(e) => return e.into_response(),
247 };
248 let e = match E::from_request(Request::from_parts(parts, body), &state).await {
249 Ok(v) => v,
250 Err(e) => return e.into_response(),
251 };
252 (self)(a, b, c, d, e).await.into_response()
253 })
254 }
255}