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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
pub mod builder;
pub mod service;
mod recognizer;
#[macro_use]
mod scoped_map;
mod uri;
#[cfg(test)]
mod tests;
use http::Method;
use indexmap::IndexMap;
use std::fmt;
use std::sync::Arc;
use error::handler::ErrorHandler;
use error::Error;
use handler::Handler;
use modifier::Modifier;
use self::builder::AppBuilder;
use self::recognizer::Recognizer;
use self::scoped_map::ScopedMap;
use self::uri::Uri;
#[derive(Debug)]
struct Config {
fallback_head: bool,
fallback_options: bool,
_priv: (),
}
impl Default for Config {
fn default() -> Self {
Config {
fallback_head: true,
fallback_options: true,
_priv: (),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ScopeId {
Global,
Local(usize),
}
impl ScopeId {
fn local_id(self) -> Option<usize> {
match self {
ScopeId::Global => None,
ScopeId::Local(id) => Some(id),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ModifierId {
Global(usize),
Scope(usize, usize),
Route(usize, usize),
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub(crate) struct RouteId(ScopeId, usize);
struct ScopeData {
id: ScopeId,
parent: ScopeId,
prefix: Option<Uri>,
modifiers: Vec<Box<dyn Modifier + Send + Sync + 'static>>,
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for ScopeData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ScopeData")
.field("id", &self.id)
.field("parent", &self.parent)
.field("prefix", &self.prefix)
.finish()
}
}
struct RouteData {
id: RouteId,
uri: Uri,
method: Method,
modifiers: Vec<Box<dyn Modifier + Send + Sync + 'static>>,
handler: Box<dyn Handler + Send + Sync + 'static>,
modifier_ids: Vec<ModifierId>,
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for RouteData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("RouteData")
.field("id", &self.id)
.field("uri", &self.uri)
.field("method", &self.method)
.field("modifier_ids", &self.modifier_ids)
.finish()
}
}
struct AppState {
routes: Vec<RouteData>,
scopes: Vec<ScopeData>,
recognizer: Recognizer,
route_ids: Vec<IndexMap<Method, usize>>,
config: Config,
globals: ScopedMap,
error_handler: Box<dyn ErrorHandler + Send + Sync + 'static>,
modifiers: Vec<Box<dyn Modifier + Send + Sync + 'static>>,
}
#[cfg_attr(tarpaulin, skip)]
impl fmt::Debug for AppState {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("AppState")
.field("routes", &self.routes)
.field("scopes", &self.scopes)
.field("recognizer", &self.recognizer)
.field("route_ids", &self.route_ids)
.field("config", &self.config)
.field("globals", &self.globals)
.finish()
}
}
#[derive(Debug, Clone)]
pub struct App {
inner: Arc<AppState>,
}
impl App {
pub fn builder() -> AppBuilder {
AppBuilder::new()
}
fn route(&self, id: RouteId) -> Option<&RouteData> {
let RouteId(_, pos) = id;
self.inner.routes.get(pos)
}
fn error_handler(&self) -> &(dyn ErrorHandler + Send + Sync + 'static) {
&*self.inner.error_handler
}
fn modifier(&self, id: ModifierId) -> Option<&(dyn Modifier + Send + Sync + 'static)> {
match id {
ModifierId::Global(pos) => self.inner.modifiers.get(pos).map(|m| &**m),
ModifierId::Scope(id, pos) => self.inner.scopes.get(id)?.modifiers.get(pos).map(|m| &**m),
ModifierId::Route(id, pos) => self.inner.routes.get(id)?.modifiers.get(pos).map(|m| &**m),
}
}
pub(crate) fn get<T>(&self, id: RouteId) -> Option<&T>
where
T: Send + Sync + 'static,
{
self.inner.globals.get(id.0)
}
fn recognize(&self, path: &str, method: &Method) -> Result<(usize, Vec<(usize, usize)>), Error> {
let (i, params) = self.inner.recognizer.recognize(path).ok_or_else(Error::not_found)?;
let methods = &self.inner.route_ids[i];
match methods.get(method) {
Some(&i) => Ok((i, params)),
None if self.inner.config.fallback_head && *method == Method::HEAD => match methods.get(&Method::GET) {
Some(&i) => Ok((i, params)),
None => Err(Error::method_not_allowed()),
},
None => Err(Error::method_not_allowed()),
}
}
}