1mod 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#[crate::async_trait]
60pub trait Handler<Input>: Send + Sync + 'static {
61 type Output;
63
64 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
82pub trait HandlerExt<I>: Handler<I> {
89 fn before<F>(self, f: F) -> Before<Self, F>
91 where
92 Self: Sized,
93 {
94 Before::new(self, f)
95 }
96
97 fn after<F>(self, f: F) -> After<Self, F>
99 where
100 Self: Sized,
101 {
102 After::new(self, f)
103 }
104
105 fn around<F>(self, f: F) -> Around<Self, F>
107 where
108 Self: Sized,
109 {
110 Around::new(self, f)
111 }
112
113 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 fn map<F>(self, f: F) -> Map<Self, F>
130 where
131 Self: Sized,
132 {
133 Map::new(self, f)
134 }
135
136 fn map_into_response(self) -> MapInToResponse<Self>
140 where
141 Self: Sized,
142 {
143 MapInToResponse::new(self)
144 }
145
146 fn and_then<F>(self, f: F) -> AndThen<Self, F>
148 where
149 Self: Sized,
150 {
151 AndThen::new(self, f)
152 }
153
154 fn map_err<F>(self, f: F) -> MapErr<Self, F>
156 where
157 Self: Sized,
158 {
159 MapErr::new(self, f)
160 }
161
162 fn or_else<F>(self, f: F) -> OrElse<Self, F>
164 where
165 Self: Sized,
166 {
167 OrElse::new(self, f)
168 }
169
170 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 fn catch_unwind<F>(self, f: F) -> CatchUnwind<Self, F>
180 where
181 Self: Sized,
182 {
183 CatchUnwind::new(self, f)
184 }
185
186 fn boxed(self) -> BoxHandler<I, Self::Output>
188 where
189 Self: Sized + Clone,
190 {
191 BoxHandler::new(self)
192 }
193
194 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 #[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> {}