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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
use std::collections::hash_map::{HashMap, Entry};
use {Method, StatusCode};
use context::hypermedia::Link;
use handler::{HandleRequest, Environment, Build, FromHandler, BuilderContext, ApplyContext, Merge};
#[derive(Clone)]
pub struct MethodRouter<T> {
handlers: HashMap<Method, T>,
}
impl<T> MethodRouter<T> {
pub fn new() -> MethodRouter<T> {
MethodRouter::default()
}
pub fn build(&mut self) -> Builder<T> {
self.get_builder(BuilderContext::new())
}
pub fn insert(&mut self, method: Method, handler: T) {
self.handlers.insert(method, handler);
}
}
impl<T: HandleRequest> HandleRequest for MethodRouter<T> {
fn handle_request<'a, 'b, 'l, 'g>(&self, mut environment: Environment<'a, 'b, 'l, 'g>) -> Result<(), Environment<'a, 'b, 'l, 'g>> {
if let Some(handler) = self.handlers.get(&environment.context.method) {
handler.handle_request(environment)
} else {
environment.response.set_status(StatusCode::MethodNotAllowed);
Err(environment)
}
}
fn hyperlinks<'a>(&'a self, base: Link<'a>) -> Vec<Link<'a>> {
self.handlers.iter().flat_map(|(method, handler)| {
let mut link = base.clone();
link.method = Some(method.clone());
handler.hyperlinks(link)
}).collect()
}
}
impl<T> Default for MethodRouter<T> {
fn default() -> MethodRouter<T> {
MethodRouter {
handlers: HashMap::new(),
}
}
}
impl<'a, T: 'a> Build<'a> for MethodRouter<T> {
type Builder = Builder<'a, T>;
fn get_builder(&'a mut self, context: BuilderContext) -> Self::Builder {
Builder {
router: self,
context: context
}
}
}
impl<T: ApplyContext> ApplyContext for MethodRouter<T> {
fn apply_context(&mut self, context: BuilderContext) {
for (_, handler) in &mut self.handlers {
handler.apply_context(context.clone());
}
}
fn prepend_context(&mut self, context: BuilderContext) {
for (_, handler) in &mut self.handlers {
handler.prepend_context(context.clone());
}
}
}
impl<T: Merge> Merge for MethodRouter<T> {
fn merge(&mut self, other: MethodRouter<T>) {
println!("merging {:} methods with {:} methods", self.handlers.len(), other.handlers.len());
for (method, handler) in other.handlers {
println!("merging {:}", method);
match self.handlers.entry(method) {
Entry::Vacant(entry) => { entry.insert(handler); },
Entry::Occupied(mut entry) => entry.get_mut().merge(handler),
}
}
}
}
pub struct Builder<'a, T: 'a> {
router: &'a mut MethodRouter<T>,
context: BuilderContext
}
impl<'a, T> Builder<'a, T> {
pub fn many<F: FnOnce(&mut Builder<'a, T>)>(&mut self, build: F) -> &mut Builder<'a, T> {
build(self);
self
}
pub fn on_get<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Get, handler);
}
pub fn on_post<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Post, handler);
}
pub fn on_patch<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Patch, handler);
}
pub fn on_put<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Put, handler);
}
pub fn on_delete<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Delete, handler);
}
pub fn on_options<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Options, handler);
}
pub fn on_head<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Head, handler);
}
pub fn on_connect<H>(&mut self, handler: H) where T: FromHandler<H> {
self.on(Method::Connect, handler);
}
pub fn on<H>(&mut self, method: Method, handler: H) where T: FromHandler<H> {
self.router.handlers.insert(method, T::from_handler(self.context.clone(), handler));
}
}
impl<'a: 'b, 'b, T: Default + ApplyContext + Build<'b>> Builder<'a, T> {
pub fn get<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Get)
}
pub fn post<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Post)
}
pub fn patch<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Patch)
}
pub fn put<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Put)
}
pub fn delete<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Delete)
}
pub fn options<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Options)
}
pub fn head<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Head)
}
pub fn connect<H>(&'b mut self) -> T::Builder where T: FromHandler<H> {
self.method(Method::Connect)
}
pub fn method<H>(&'b mut self, method: Method) -> T::Builder where T: FromHandler<H> {
match self.router.handlers.entry(method) {
Entry::Occupied(entry) => entry.into_mut().get_builder(self.context.clone()),
Entry::Vacant(entry) => {
let mut handler = T::default();
handler.apply_context(self.context.clone());
entry.insert(handler).get_builder(self.context.clone())
}
}
}
}
impl<'a, T: Merge + ApplyContext> Builder<'a, T> {
pub fn merge(&mut self, mut other: MethodRouter<T>) -> &mut Builder<'a, T> {
other.apply_context(self.context.clone());
self.router.merge(other);
self
}
}