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
use std::fmt;
use std::pin::Pin;
use std::future::Future;
pub trait Mapper<IN>: Send + fmt::Debug
where
IN: ?Sized,
{
type Out;
fn map(&mut self, input: &IN) -> Self::Out;
}
impl<K, V, KMapper, VMapper> Mapper<(K, V)> for (KMapper, VMapper)
where
KMapper: Mapper<K, Out = bool>,
VMapper: Mapper<V, Out = bool>,
{
type Out = bool;
fn map(&mut self, input: &(K, V)) -> bool {
self.0.map(&input.0) && self.1.map(&input.1)
}
}
pub trait Matcher<IN>: Send + fmt::Debug
where
IN: ?Sized,
{
fn matches(&mut self, input: &IN) -> bool;
}
impl<T, IN> Matcher<IN> for T
where
T: Mapper<IN, Out = bool>,
{
fn matches(&mut self, input: &IN) -> bool {
self.map(input)
}
}
pub trait Responder: Send + fmt::Debug {
fn respond(&mut self) -> Pin<Box<dyn Future<Output = http::Response<Vec<u8>>> + Send>>;
}
impl<B> Responder for http::Response<B>
where
B: Into<Vec<u8>> + Clone + Send + fmt::Debug,
{
fn respond(&mut self) -> Pin<Box<dyn Future<Output = http::Response<Vec<u8>>> + Send>> {
async fn _respond(resp: http::Response<Vec<u8>>) -> http::Response<Vec<u8>> {
resp
}
let mut builder = http::Response::builder();
builder = builder
.status(self.status().clone())
.version(self.version().clone());
*builder.headers_mut().unwrap() = self.headers().clone();
let resp = builder.body(self.body().clone().into()).unwrap();
Box::pin(_respond(resp))
}
}