nuclear_router/hyper_service/
handler.rs1use super::{BoxError, BoxFuture, Future, Request, Response, StdError};
2use crate::router::OwnedCaptures;
3
4pub trait Handler {
5 fn call(&self, req: Request, params: OwnedCaptures) -> BoxFuture<'static, Result<Response, BoxError>>;
6}
7
8pub type BoxHandler = Box<dyn Handler + Send + Sync>;
9
10impl Handler for BoxHandler {
11 fn call(&self, req: Request, params: OwnedCaptures) -> BoxFuture<'static, Result<Response, BoxError>> {
12 Handler::call(&**self, req, params)
13 }
14}
15
16impl<F, E, Fut> Handler for F
17where
18 F: Fn(Request, OwnedCaptures) -> Fut,
19 E: StdError + Send + Sync + 'static,
20 Fut: Future<Output = Result<Response, E>> + Send + 'static,
21{
22 fn call(&self, req: Request, params: OwnedCaptures) -> BoxFuture<'static, Result<Response, BoxError>> {
23 let fut = (self)(req, params);
24 Box::pin(async move {
25 let ret = fut.await;
26 match ret {
27 Ok(r) => Ok(r),
28 Err(e) => Err(Box::new(e) as BoxError),
29 }
30 })
31 }
32}