viz_core/
handler.rs

1//! Traits and types for handling an HTTP.
2
3mod cloneable;
4
5mod after;
6pub use after::After;
7
8mod and_then;
9pub use and_then::AndThen;
10
11mod around;
12pub use around::{Around, Next};
13
14mod before;
15pub use before::Before;
16
17mod boxed;
18pub use boxed::BoxHandler;
19
20mod catch_error;
21pub use catch_error::CatchError;
22
23mod catch_unwind;
24pub use catch_unwind::CatchUnwind;
25
26mod either;
27pub use either::Either;
28
29mod fn_ext;
30pub use fn_ext::FnExt;
31
32mod fn_ext_hanlder;
33pub use fn_ext_hanlder::FnExtHandler;
34
35mod into_handler;
36pub use into_handler::IntoHandler;
37
38mod map;
39pub use map::Map;
40
41mod map_err;
42pub use map_err::MapErr;
43
44mod map_into_response;
45pub use map_into_response::MapInToResponse;
46
47mod or_else;
48pub use or_else::OrElse;
49
50mod transform;
51pub use transform::Transform;
52
53mod service;
54pub use service::ServiceHandler;
55
56/// A simplified asynchronous interface for handling input and output.
57///
58/// Composable request handlers.
59#[crate::async_trait]
60pub trait Handler<Input>: Send + Sync + 'static {
61    /// The returned type after the call operator is used.
62    type Output;
63
64    /// Performs the call operation.
65    async fn call(&self, input: Input) -> Self::Output;
66}
67
68#[crate::async_trait]
69impl<F, I, Fut, O> Handler<I> for F
70where
71    I: Send + 'static,
72    F: Fn(I) -> Fut + Clone + Send + Sync + 'static,
73    Fut: ::core::future::Future<Output = O> + Send,
74{
75    type Output = Fut::Output;
76
77    async fn call(&self, i: I) -> Self::Output {
78        (self)(i).await
79    }
80}
81
82/// The [`HandlerExt`] trait, which provides adapters for chaining and composing handlers.
83///
84/// Likes the [`FutureExt`] and [`StreamExt`] traits.
85///
86/// [`FutureExt`]: https://docs.rs/futures/latest/futures/future/trait.FutureExt.html
87/// [`StreamExt`]: https://docs.rs/futures/latest/futures/stream/trait.StreamExt.html
88pub trait HandlerExt<I>: Handler<I> {
89    /// Maps the input before the handler calls.
90    fn before<F>(self, f: F) -> Before<Self, F>
91    where
92        Self: Sized,
93    {
94        Before::new(self, f)
95    }
96
97    /// Maps the output `Result<T>` after the handler called.
98    fn after<F>(self, f: F) -> After<Self, F>
99    where
100        Self: Sized,
101    {
102        After::new(self, f)
103    }
104
105    /// Wraps around the remaining handler or middleware chain.
106    fn around<F>(self, f: F) -> Around<Self, F>
107    where
108        Self: Sized,
109    {
110        Around::new(self, f)
111    }
112
113    /// Wraps this handler in an Either handler, making it the left-hand variant of that Either.
114    ///
115    /// Returns the left-hand variant if `enable` is true, otherwise returns the right-hand
116    /// variant.
117    fn either<R>(self, r: R, enable: bool) -> Either<Self, R>
118    where
119        Self: Sized,
120    {
121        if enable {
122            Either::Left(self)
123        } else {
124            Either::Right(r)
125        }
126    }
127
128    /// Maps the `Ok` value of the output if after the handler called.
129    fn map<F>(self, f: F) -> Map<Self, F>
130    where
131        Self: Sized,
132    {
133        Map::new(self, f)
134    }
135
136    /// Maps the handler's output type to the [`Response`].
137    ///
138    /// [`Response`]: crate::Response
139    fn map_into_response(self) -> MapInToResponse<Self>
140    where
141        Self: Sized,
142    {
143        MapInToResponse::new(self)
144    }
145
146    /// Calls op if the result is Ok, otherwise returns the Err value of self.
147    fn and_then<F>(self, f: F) -> AndThen<Self, F>
148    where
149        Self: Sized,
150    {
151        AndThen::new(self, f)
152    }
153
154    /// Maps the `Err` value of the output if after the handler called.
155    fn map_err<F>(self, f: F) -> MapErr<Self, F>
156    where
157        Self: Sized,
158    {
159        MapErr::new(self, f)
160    }
161
162    /// Calls `op` if the output is `Err`, otherwise returns the `Ok` value of the output.
163    fn or_else<F>(self, f: F) -> OrElse<Self, F>
164    where
165        Self: Sized,
166    {
167        OrElse::new(self, f)
168    }
169
170    /// Catches rejected error while calling the handler.
171    fn catch_error<F, E, R>(self, f: F) -> CatchError<Self, F, E, R>
172    where
173        Self: Sized,
174    {
175        CatchError::new(self, f)
176    }
177
178    /// Catches unwinding panics while calling the handler.
179    fn catch_unwind<F>(self, f: F) -> CatchUnwind<Self, F>
180    where
181        Self: Sized,
182    {
183        CatchUnwind::new(self, f)
184    }
185
186    /// Converts this Handler into a [`BoxHandler`].
187    fn boxed(self) -> BoxHandler<I, Self::Output>
188    where
189        Self: Sized + Clone,
190    {
191        BoxHandler::new(self)
192    }
193
194    /// Returns a new [`Handler`] that wrapping the `Self` and a type implementing [`Transform`].
195    fn with<T>(self, t: T) -> T::Output
196    where
197        T: Transform<Self>,
198        Self: Sized,
199    {
200        t.transform(self)
201    }
202
203    /// Maps the handler.
204    #[must_use]
205    fn with_fn<F>(self, f: F) -> Self
206    where
207        F: Fn(Self) -> Self,
208        Self: Sized,
209    {
210        f(self)
211    }
212}
213
214impl<I, T> HandlerExt<I> for T where T: ?Sized + Handler<I> {}