1use crate::{Conn, Headers, Info, Status, Upgrade};
2use std::{borrow::Cow, future::Future};
3
4pub trait Handler: Send + Sync + 'static {
69 fn run(&self, conn: Conn) -> impl Future<Output = Conn> + Send {
71 async { conn }
72 }
73
74 fn init(&mut self, info: &mut Info) -> impl Future<Output = ()> + Send {
81 let _ = info;
82 std::future::ready(())
83 }
84
85 fn before_send(&self, conn: Conn) -> impl Future<Output = Conn> + Send {
102 std::future::ready(conn)
103 }
104
105 fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
111 let _ = upgrade;
112 false
113 }
114
115 fn upgrade(&self, upgrade: Upgrade) -> impl Future<Output = ()> + Send {
124 let _ = upgrade;
125 async { unimplemented!("if has_upgrade returns true, you must also implement upgrade") }
126 }
127
128 fn name(&self) -> Cow<'static, str> {
131 std::any::type_name::<Self>().into()
132 }
133}
134
135impl Handler for Status {
136 async fn run(&self, conn: Conn) -> Conn {
137 conn.with_status(*self)
138 }
139}
140
141impl<H: Handler> Handler for Vec<H> {
142 async fn run(&self, mut conn: Conn) -> Conn {
143 for handler in self {
144 log::debug!("running {}", handler.name());
145 conn = handler.run(conn).await;
146 if conn.is_halted() {
147 break;
148 }
149 }
150 conn
151 }
152
153 async fn init(&mut self, info: &mut Info) {
154 for handler in self {
155 handler.init(info).await;
156 }
157 }
158
159 async fn before_send(&self, mut conn: Conn) -> Conn {
160 for handler in self.iter().rev() {
161 conn = handler.before_send(conn).await;
162 }
163 conn
164 }
165
166 fn name(&self) -> Cow<'static, str> {
167 self.iter()
168 .map(Handler::name)
169 .collect::<Vec<_>>()
170 .join(",")
171 .into()
172 }
173
174 fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
175 self.iter().any(|g| g.has_upgrade(upgrade))
176 }
177
178 async fn upgrade(&self, upgrade: Upgrade) {
179 if let Some(handler) = self.iter().find(|g| g.has_upgrade(&upgrade)) {
180 handler.upgrade(upgrade).await;
181 }
182 }
183}
184
185impl<const L: usize, H: Handler> Handler for [H; L] {
186 async fn run(&self, mut conn: Conn) -> Conn {
187 for handler in self {
188 log::debug!("running {}", handler.name());
189 conn = handler.run(conn).await;
190 if conn.is_halted() {
191 break;
192 }
193 }
194 conn
195 }
196
197 async fn init(&mut self, info: &mut Info) {
198 for handler in self {
199 handler.init(info).await;
200 }
201 }
202
203 async fn before_send(&self, mut conn: Conn) -> Conn {
204 for handler in self.iter().rev() {
205 conn = handler.before_send(conn).await;
206 }
207 conn
208 }
209
210 fn name(&self) -> Cow<'static, str> {
211 self.iter()
212 .map(Handler::name)
213 .collect::<Vec<_>>()
214 .join(",")
215 .into()
216 }
217
218 fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
219 self.iter().any(|g| g.has_upgrade(upgrade))
220 }
221
222 async fn upgrade(&self, upgrade: Upgrade) {
223 if let Some(handler) = self.iter().find(|g| g.has_upgrade(&upgrade)) {
224 handler.upgrade(upgrade).await;
225 }
226 }
227}
228
229impl<Fun, Fut> Handler for Fun
230where
231 Fun: Fn(Conn) -> Fut + Send + Sync + 'static,
232 Fut: Future<Output = Conn> + Send + 'static,
233{
234 async fn run(&self, conn: Conn) -> Conn {
235 (self)(conn).await
236 }
237}
238
239impl Handler for &'static str {
240 async fn run(&self, conn: Conn) -> Conn {
241 conn.ok(*self)
242 }
243
244 fn name(&self) -> Cow<'static, str> {
245 format!("conn.ok({:?})", &self).into()
246 }
247}
248
249impl Handler for String {
250 async fn run(&self, conn: Conn) -> Conn {
251 conn.ok(self.clone())
252 }
253
254 fn name(&self) -> Cow<'static, str> {
255 format!("conn.ok({:?})", &self).into()
256 }
257}
258
259impl Handler for () {
260 async fn run(&self, conn: Conn) -> Conn {
261 conn
262 }
263}
264
265impl<H: Handler> Handler for Option<H> {
266 async fn run(&self, conn: Conn) -> Conn {
267 let handler = crate::conn_unwrap!(self, conn);
268 handler.run(conn).await
269 }
270
271 async fn init(&mut self, info: &mut Info) {
272 if let Some(handler) = self {
273 handler.init(info).await;
274 }
275 }
276
277 async fn before_send(&self, conn: Conn) -> Conn {
278 let handler = crate::conn_unwrap!(self, conn);
279 handler.before_send(conn).await
280 }
281
282 fn name(&self) -> Cow<'static, str> {
283 self.as_ref().map_or_else(|| "-".into(), Handler::name)
284 }
285
286 fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
287 self.as_ref().is_some_and(|h| h.has_upgrade(upgrade))
288 }
289
290 async fn upgrade(&self, upgrade: Upgrade) {
291 if let Some(handler) = self {
292 handler.upgrade(upgrade).await;
293 }
294 }
295}
296
297impl Handler for Headers {
298 async fn run(&self, mut conn: Conn) -> Conn {
299 conn.response_headers_mut().append_all(self.clone());
300 conn
301 }
302}
303
304impl<T, E> Handler for Result<T, E>
305where
306 T: Handler,
307 E: Handler,
308{
309 async fn run(&self, conn: Conn) -> Conn {
310 match self {
311 Ok(t) => t.run(conn).await,
312 Err(e) => e.run(conn).await,
313 }
314 }
315
316 async fn init(&mut self, info: &mut Info) {
317 match self {
318 Ok(t) => t.init(info).await,
319 Err(e) => e.init(info).await,
320 }
321 }
322
323 async fn before_send(&self, conn: Conn) -> Conn {
324 match self {
325 Ok(t) => t.before_send(conn).await,
326 Err(e) => e.before_send(conn).await,
327 }
328 }
329
330 fn name(&self) -> Cow<'static, str> {
331 match self {
332 Ok(t) => format!("Ok({})", t.name()).into(),
333 Err(e) => format!("Err({})", e.name()).into(),
334 }
335 }
336
337 fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
338 match self {
339 Ok(t) => t.has_upgrade(upgrade),
340 Err(e) => e.has_upgrade(upgrade),
341 }
342 }
343
344 async fn upgrade(&self, upgrade: Upgrade) {
345 match self {
346 Ok(t) => t.upgrade(upgrade).await,
347 Err(e) => e.upgrade(upgrade).await,
348 }
349 }
350}
351
352macro_rules! reverse_before_send {
353 ($conn:ident, $name:ident) => (
354 let $conn = ($name).before_send($conn).await;
355 );
356
357 ($conn:ident, $name:ident $($other_names:ident)+) => (
358 reverse_before_send!($conn, $($other_names)*);
359 reverse_before_send!($conn, $name);
360 );
361}
362
363macro_rules! impl_handler_tuple {
364 ($($name:ident)+) => (
365 impl<$($name),*> Handler for ($($name,)*) where $($name: Handler),* {
366 #[allow(non_snake_case)]
367 async fn run(&self, conn: Conn) -> Conn {
368 let ($(ref $name,)*) = *self;
369 $(
370 log::debug!("running {}", ($name).name());
371 let conn = ($name).run(conn).await;
372 if conn.is_halted() { return conn }
373 )*
374 conn
375 }
376
377 #[allow(non_snake_case)]
378 async fn init(&mut self, info: &mut Info) {
379 let ($(ref mut $name,)*) = *self;
380 $(
381 log::trace!("initializing {}", ($name).name());
382 ($name).init(info).await;
383 )*
384 }
385
386 #[allow(non_snake_case)]
387 async fn before_send(&self, conn: Conn) -> Conn {
388 let ($(ref $name,)*) = *self;
389 reverse_before_send!(conn, $($name)+);
390 conn
391 }
392
393 #[allow(non_snake_case)]
394 fn has_upgrade(&self, upgrade: &Upgrade) -> bool {
395 let ($(ref $name,)*) = *self;
396 $(if ($name).has_upgrade(upgrade) { return true })*
397 false
398 }
399
400 #[allow(non_snake_case)]
401 async fn upgrade(&self, upgrade: Upgrade) {
402 let ($(ref $name,)*) = *self;
403 $(if ($name).has_upgrade(&upgrade) {
404 return ($name).upgrade(upgrade).await;
405 })*
406 }
407
408 #[allow(non_snake_case)]
409 fn name(&self) -> Cow<'static, str> {
410 let ($(ref $name,)*) = *self;
411 format!(concat!("(\n", $(
412 concat!(" {",stringify!($name) ,":},\n")
413 ),*, ")"), $($name = ($name).name()),*).into()
414 }
415 }
416 );
417 }
418
419impl_handler_tuple! { A B }
420impl_handler_tuple! { A B C }
421impl_handler_tuple! { A B C D }
422impl_handler_tuple! { A B C D E }
423impl_handler_tuple! { A B C D E F }
424impl_handler_tuple! { A B C D E F G }
425impl_handler_tuple! { A B C D E F G H }
426impl_handler_tuple! { A B C D E F G H I }
427impl_handler_tuple! { A B C D E F G H I J }
428impl_handler_tuple! { A B C D E F G H I J K }
429impl_handler_tuple! { A B C D E F G H I J K L }
430impl_handler_tuple! { A B C D E F G H I J K L M }
431impl_handler_tuple! { A B C D E F G H I J K L M N }
432impl_handler_tuple! { A B C D E F G H I J K L M N O }