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
pub mod endpoint;
pub mod path;
pub mod prelude {
#[doc(no_inline)]
pub use crate::{chain, path};
#[doc(no_inline)]
pub use super::{mount, Config, ConfigExt};
pub mod endpoint {
#[doc(no_inline)]
pub use super::super::endpoint::{
allow_only, any, call, call_async, connect, delete, get, head, options, patch, post,
put, reply, trace,
};
}
}
#[doc(no_inline)]
pub use crate::app::config::{Config, Error, Result, Scope};
use {
crate::{
app::config::Concurrency,
handler::{Handler, ModifyHandler},
util::Chain,
},
std::borrow::Cow,
};
pub fn mount<P>(prefix: P) -> Mount<P, ()>
where
P: AsRef<str>,
{
Mount { prefix, config: () }
}
#[derive(Debug)]
pub struct Mount<P, T> {
prefix: P,
config: T,
}
impl<P, T> Mount<P, T>
where
P: AsRef<str>,
{
pub fn with<T2>(self, config: T2) -> Mount<P, Chain<T, T2>> {
Mount {
prefix: self.prefix,
config: Chain::new(self.config, config),
}
}
}
impl<P, T, M, C> Config<M, C> for Mount<P, T>
where
P: AsRef<str>,
T: Config<M, C>,
C: Concurrency,
{
type Error = Error;
fn configure(self, scope: &mut Scope<'_, M, C>) -> std::result::Result<(), Self::Error> {
scope.mount(self.prefix, self.config)
}
}
pub fn modify<M, T>(modifier: M, config: T) -> Modify<M, T> {
Modify { modifier, config }
}
#[derive(Debug)]
pub struct Modify<M, T> {
modifier: M,
config: T,
}
impl<M, T, M2, C> Config<M2, C> for Modify<M, T>
where
for<'a> T: Config<Chain<&'a M2, M>, C>,
C: Concurrency,
{
type Error = Error;
fn configure(self, cx: &mut Scope<'_, M2, C>) -> std::result::Result<(), Self::Error> {
cx.modify(self.modifier, self.config)
}
}
pub trait ConfigExt: Sized {
fn modify<M>(self, modifier: M) -> Modify<M, Self> {
modify(modifier, self)
}
}
impl<T> ConfigExt for T {}
#[derive(Debug)]
pub struct Route<H> {
path: Cow<'static, str>,
handler: H,
}
impl<H> Route<H>
where
H: Handler,
{
pub fn new(path: impl Into<Cow<'static, str>>, handler: H) -> Self {
Self {
path: path.into(),
handler,
}
}
}
impl<H, M, C> Config<M, C> for Route<H>
where
H: Handler,
M: ModifyHandler<H>,
M::Handler: Into<C::Handler>,
C: Concurrency,
{
type Error = Error;
fn configure(self, scope: &mut Scope<'_, M, C>) -> std::result::Result<(), Self::Error> {
scope.route(self.path, self.handler)
}
}