1use super::Route;
2use crate::error::SilentResult;
3use crate::extractor::{FromRequest, handler_from_extractor, handler_from_extractor_with_request};
4use crate::handler::HandlerFn;
5use crate::{Handler, HandlerWrapper, Method, Request, Response, Result};
6use std::collections::HashMap;
7use std::future::Future;
8use std::sync::Arc;
9
10pub trait HandlerGetter {
11 fn get_handler_mut(&mut self) -> &mut HashMap<Method, Arc<dyn Handler>>;
12 fn insert_handler(self, method: Method, handler: Arc<dyn Handler>) -> Self;
13 fn handler(self, method: Method, handler: Arc<dyn Handler>) -> Self;
14}
15
16pub trait HandlerAppend<F, T, Fut>: HandlerGetter
17where
18 Fut: Future<Output = Result<T>> + Send + 'static,
19 F: Fn(Request) -> Fut + Send + Sync + 'static,
20 T: Into<Response>,
21{
22 fn get(self, handler: F) -> Self;
23 fn post(self, handler: F) -> Self;
24 fn put(self, handler: F) -> Self;
25 fn delete(self, handler: F) -> Self;
26 fn patch(self, handler: F) -> Self;
27 fn options(self, handler: F) -> Self;
28 fn handler_append(&mut self, method: Method, handler: F) {
29 let handler = Arc::new(HandlerWrapper::new(handler));
30 let handler_map = self.get_handler_mut();
31 handler_map.insert(method, handler);
32 }
33}
34
35impl HandlerGetter for Route {
36 fn get_handler_mut(&mut self) -> &mut HashMap<Method, Arc<dyn Handler>> {
37 if self.path == self.create_path {
38 &mut self.handler
39 } else {
40 let mut iter = self.create_path.splitn(2, '/');
41 let _local_url = iter.next().unwrap_or("");
42 let last_url = iter.next().unwrap_or("");
43 let route = self
44 .children
45 .iter_mut()
46 .find(|c| c.create_path == last_url)
47 .unwrap();
48 <Route as HandlerGetter>::get_handler_mut(route)
49 }
50 }
51 fn insert_handler(mut self, method: Method, handler: Arc<dyn Handler>) -> Self {
52 self.handler.insert(method, handler);
53 self
54 }
55
56 fn handler(mut self, method: Method, handler: Arc<dyn Handler>) -> Self {
57 self.get_handler_mut().insert(method, handler);
58 self
59 }
60}
61
62impl<F, T, Fut> HandlerAppend<F, T, Fut> for Route
63where
64 Fut: Future<Output = Result<T>> + Send + 'static,
65 F: Fn(Request) -> Fut + Send + Sync + 'static,
66 T: Into<Response>,
67{
68 fn get(mut self, handler: F) -> Self {
69 self.handler_append(Method::GET, handler);
70 self
71 }
72
73 fn post(mut self, handler: F) -> Self {
74 self.handler_append(Method::POST, handler);
75 self
76 }
77
78 fn put(mut self, handler: F) -> Self {
79 self.handler_append(Method::PUT, handler);
80 self
81 }
82
83 fn delete(mut self, handler: F) -> Self {
84 self.handler_append(Method::DELETE, handler);
85 self
86 }
87
88 fn patch(mut self, handler: F) -> Self {
89 self.handler_append(Method::PATCH, handler);
90 self
91 }
92
93 fn options(mut self, handler: F) -> Self {
94 self.handler_append(Method::OPTIONS, handler);
95 self
96 }
97}
98
99pub trait IntoRouteHandler<Args> {
101 fn into_handler(self) -> std::sync::Arc<dyn Handler>;
102}
103
104trait RouteDispatch: Sized {
105 fn into_arc_handler<F, Fut>(handler: F) -> std::sync::Arc<dyn Handler>
106 where
107 F: Fn(Request) -> Fut + Send + Sync + 'static,
108 Fut: Future<Output = Self> + Send + 'static;
109}
110
111impl RouteDispatch for Response {
112 fn into_arc_handler<F, Fut>(handler: F) -> std::sync::Arc<dyn Handler>
113 where
114 F: Fn(Request) -> Fut + Send + Sync + 'static,
115 Fut: Future<Output = Self> + Send + 'static,
116 {
117 HandlerFn::new(handler).arc()
118 }
119}
120
121impl<T> RouteDispatch for SilentResult<T>
122where
123 T: Into<Response> + Send + 'static,
124{
125 fn into_arc_handler<F, Fut>(handler: F) -> std::sync::Arc<dyn Handler>
126 where
127 F: Fn(Request) -> Fut + Send + Sync + 'static,
128 Fut: Future<Output = Self> + Send + 'static,
129 {
130 std::sync::Arc::new(HandlerWrapper::new(handler))
131 }
132}
133
134impl<F, Fut> IntoRouteHandler<crate::Request> for F
135where
136 F: Fn(Request) -> Fut + Send + Sync + 'static,
137 Fut: Future + Send + 'static,
138 Fut::Output: RouteDispatch,
139{
140 fn into_handler(self) -> std::sync::Arc<dyn Handler> {
141 <Fut::Output as RouteDispatch>::into_arc_handler(self)
142 }
143}
144
145impl<Args, F, Fut, T> IntoRouteHandler<Args> for F
146where
147 Args: FromRequest + Send + 'static,
148 <Args as FromRequest>::Rejection: Into<Response> + Send + 'static,
149 F: Fn(Args) -> Fut + Send + Sync + 'static,
150 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
151 T: Into<Response> + Send + 'static,
152{
153 fn into_handler(self) -> std::sync::Arc<dyn Handler> {
154 let adapted = handler_from_extractor::<Args, F, Fut, T>(self);
155 std::sync::Arc::new(HandlerWrapper::new(adapted))
156 }
157}
158
159impl<Args, F, Fut, T> IntoRouteHandler<(Request, Args)> for F
160where
161 Args: FromRequest + Send + 'static,
162 <Args as FromRequest>::Rejection: Into<Response> + Send + 'static,
163 F: Fn(Request, Args) -> Fut + Send + Sync + 'static,
164 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
165 T: Into<Response> + Send + 'static,
166{
167 fn into_handler(self) -> std::sync::Arc<dyn Handler> {
168 let adapted = handler_from_extractor_with_request::<Args, F, Fut, T>(self);
169 std::sync::Arc::new(HandlerWrapper::new(adapted))
170 }
171}
172
173impl Route {
174 pub fn get<H, Args>(self, handler: H) -> Self
175 where
176 H: IntoRouteHandler<Args>,
177 {
178 let handler = handler.into_handler();
179 <Route as HandlerGetter>::handler(self, Method::GET, handler)
180 }
181
182 pub fn post<H, Args>(self, handler: H) -> Self
183 where
184 H: IntoRouteHandler<Args>,
185 {
186 let handler = handler.into_handler();
187 <Route as HandlerGetter>::handler(self, Method::POST, handler)
188 }
189
190 pub fn put<H, Args>(self, handler: H) -> Self
191 where
192 H: IntoRouteHandler<Args>,
193 {
194 let handler = handler.into_handler();
195 <Route as HandlerGetter>::handler(self, Method::PUT, handler)
196 }
197
198 pub fn delete<H, Args>(self, handler: H) -> Self
199 where
200 H: IntoRouteHandler<Args>,
201 {
202 let handler = handler.into_handler();
203 <Route as HandlerGetter>::handler(self, Method::DELETE, handler)
204 }
205
206 pub fn patch<H, Args>(self, handler: H) -> Self
207 where
208 H: IntoRouteHandler<Args>,
209 {
210 let handler = handler.into_handler();
211 <Route as HandlerGetter>::handler(self, Method::PATCH, handler)
212 }
213
214 pub fn options<H, Args>(self, handler: H) -> Self
215 where
216 H: IntoRouteHandler<Args>,
217 {
218 let handler = handler.into_handler();
219 <Route as HandlerGetter>::handler(self, Method::OPTIONS, handler)
220 }
221}
222
223impl Route {
225 pub fn get_ex<Args, F, Fut, T>(mut self, f: F) -> Self
226 where
227 Args: crate::extractor::FromRequest + Send + 'static,
228 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
229 F: Fn(Args) -> Fut + Send + Sync + 'static,
230 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
231 T: Into<Response> + Send + 'static,
232 {
233 let adapted = handler_from_extractor::<Args, F, Fut, T>(f);
234 self.handler_append(Method::GET, adapted);
235 self
236 }
237
238 pub fn post_ex<Args, F, Fut, T>(mut self, f: F) -> Self
239 where
240 Args: crate::extractor::FromRequest + Send + 'static,
241 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
242 F: Fn(Args) -> Fut + Send + Sync + 'static,
243 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
244 T: Into<Response> + Send + 'static,
245 {
246 let adapted = handler_from_extractor::<Args, F, Fut, T>(f);
247 self.handler_append(Method::POST, adapted);
248 self
249 }
250
251 pub fn put_ex<Args, F, Fut, T>(mut self, f: F) -> Self
252 where
253 Args: crate::extractor::FromRequest + Send + 'static,
254 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
255 F: Fn(Args) -> Fut + Send + Sync + 'static,
256 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
257 T: Into<Response> + Send + 'static,
258 {
259 let adapted = handler_from_extractor::<Args, _, _, T>(f);
260 self.handler_append(Method::PUT, adapted);
261 self
262 }
263
264 pub fn delete_ex<Args, F, Fut, T>(mut self, f: F) -> Self
265 where
266 Args: crate::extractor::FromRequest + Send + 'static,
267 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
268 F: Fn(Args) -> Fut + Send + Sync + 'static,
269 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
270 T: Into<Response> + Send + 'static,
271 {
272 let adapted = handler_from_extractor::<Args, _, _, T>(f);
273 self.handler_append(Method::DELETE, adapted);
274 self
275 }
276
277 pub fn patch_ex<Args, F, Fut, T>(mut self, f: F) -> Self
278 where
279 Args: crate::extractor::FromRequest + Send + 'static,
280 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
281 F: Fn(Args) -> Fut + Send + Sync + 'static,
282 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
283 T: Into<Response> + Send + 'static,
284 {
285 let adapted = handler_from_extractor::<Args, _, _, T>(f);
286 self.handler_append(Method::PATCH, adapted);
287 self
288 }
289
290 pub fn options_ex<Args, F, Fut, T>(mut self, f: F) -> Self
291 where
292 Args: crate::extractor::FromRequest + Send + 'static,
293 <Args as crate::extractor::FromRequest>::Rejection: Into<Response> + Send + 'static,
294 F: Fn(Args) -> Fut + Send + Sync + 'static,
295 Fut: core::future::Future<Output = Result<T>> + Send + 'static,
296 T: Into<Response> + Send + 'static,
297 {
298 let adapted = handler_from_extractor::<Args, _, _, T>(f);
299 self.handler_append(Method::OPTIONS, adapted);
300 self
301 }
302}