1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use crate::body::AsyncBody;
use http::{Request, Response};
use std::{error::Error, fmt, future::Future, pin::Pin};
mod context;
mod obj;
pub use self::context::Context;
pub(crate) use self::obj::InterceptorObj;
type InterceptorResult<E> = Result<Response<AsyncBody>, E>;
#[cfg(feature = "unstable-interceptors")]
#[macro_export]
macro_rules! interceptor {
($request:ident, $ctx:ident, $body:expr) => {{
async fn interceptor(
mut $request: $crate::http::Request<$crate::AsyncBody>,
$ctx: $crate::interceptor::Context,
) -> Result<$crate::http::Response<$crate::AsyncBody>, $crate::Error> {
(move || async move { $body })().await.map_err(Into::into)
}
$crate::interceptor::from_fn(interceptor)
}};
}
pub trait Interceptor: Send + Sync {
type Err: Error + Send + Sync + 'static;
fn intercept(&self, request: Request<AsyncBody>, ctx: Context) -> InterceptorFuture<Self::Err>;
}
pub type InterceptorFuture<E> =
Pin<Box<dyn Future<Output = InterceptorResult<E>> + Send + 'static>>;
pub fn from_fn<F, E>(f: F) -> InterceptorFn<F>
where
F: for<'a> private::AsyncFn2<Request<AsyncBody>, Context, Output = InterceptorResult<E>>
+ Send
+ Sync
+ 'static,
E: Error + Send + Sync + 'static,
{
InterceptorFn(f)
}
pub struct InterceptorFn<F>(F);
impl<E, F> Interceptor for InterceptorFn<F>
where
E: Error + Send + Sync + 'static,
F: for<'a> private::AsyncFn2<Request<AsyncBody>, Context, Output = InterceptorResult<E>>
+ Send
+ Sync
+ 'static,
{
type Err = E;
fn intercept(&self, request: Request<AsyncBody>, ctx: Context) -> InterceptorFuture<Self::Err> {
Box::pin(self.0.call(request, ctx))
}
}
impl<F: fmt::Debug> fmt::Debug for InterceptorFn<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
#[allow(unreachable_pub)]
mod private {
use std::future::Future;
macro_rules! impl_async_fn {
($(($FnOnce:ident, $FnMut:ident, $Fn:ident, ($($arg:ident: $arg_ty:ident,)*)),)*) => {
$(
pub trait $FnOnce<$($arg_ty,)*> {
type Output;
type Future: Future<Output = Self::Output> + Send;
fn call_once(self, $($arg: $arg_ty,)*) -> Self::Future;
}
pub trait $FnMut<$($arg_ty,)*>: $FnOnce<$($arg_ty,)*> {
fn call_mut(&mut self, $($arg: $arg_ty,)*) -> Self::Future;
}
pub trait $Fn<$($arg_ty,)*>: $FnMut<$($arg_ty,)*> {
fn call(&self, $($arg: $arg_ty,)*) -> Self::Future;
}
impl<$($arg_ty,)* F, Fut> $FnOnce<$($arg_ty,)*> for F
where
F: FnOnce($($arg_ty,)*) -> Fut,
Fut: Future + Send,
{
type Output = Fut::Output;
type Future = Fut;
fn call_once(self, $($arg: $arg_ty,)*) -> Self::Future {
self($($arg,)*)
}
}
impl<$($arg_ty,)* F, Fut> $FnMut<$($arg_ty,)*> for F
where
F: FnMut($($arg_ty,)*) -> Fut,
Fut: Future + Send,
{
fn call_mut(&mut self, $($arg: $arg_ty,)*) -> Self::Future {
self($($arg,)*)
}
}
impl<$($arg_ty,)* F, Fut> $Fn<$($arg_ty,)*> for F
where
F: Fn($($arg_ty,)*) -> Fut,
Fut: Future + Send,
{
fn call(&self, $($arg: $arg_ty,)*) -> Self::Future {
self($($arg,)*)
}
}
)*
}
}
impl_async_fn! {
(AsyncFnOnce0, AsyncFnMut0, AsyncFn0, ()),
(AsyncFnOnce1, AsyncFnMut1, AsyncFn1, (a0:A0, )),
(AsyncFnOnce2, AsyncFnMut2, AsyncFn2, (a0:A0, a1:A1, )),
}
}