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
use std::fmt;
use std::convert::From;
use super::Collider;
use term_painter::ToStyle;
use term_painter::Color::*;
use codegen::StaticRouteInfo;
use handler::Handler;
use request::Request;
use http::{Method, ContentType};
use http::uri::URI;
pub struct Route {
pub method: Method,
pub handler: Handler,
pub path: URI<'static>,
pub rank: isize,
pub format: ContentType,
}
fn default_rank(path: &str) -> isize {
path.contains('<') as isize
}
impl Route {
pub fn new<S>(m: Method, path: S, handler: Handler) -> Route
where S: AsRef<str>
{
Route {
method: m,
handler: handler,
rank: default_rank(path.as_ref()),
path: URI::from(path.as_ref().to_string()),
format: ContentType::Any,
}
}
pub fn ranked<S>(rank: isize, m: Method, path: S, handler: Handler) -> Route
where S: AsRef<str>
{
Route {
method: m,
path: URI::from(path.as_ref().to_string()),
handler: handler,
rank: rank,
format: ContentType::Any,
}
}
pub fn set_path<S>(&mut self, path: S) where S: AsRef<str> {
self.path = URI::from(path.as_ref().to_string());
}
#[doc(hidden)]
pub fn get_param_indexes(&self, uri: &URI) -> Vec<(usize, usize)> {
let route_segs = self.path.segments();
let uri_segs = uri.segments();
let start_addr = uri.path().as_ptr() as usize;
let mut result = Vec::with_capacity(self.path.segment_count());
for (route_seg, uri_seg) in route_segs.zip(uri_segs) {
let i = (uri_seg.as_ptr() as usize) - start_addr;
if route_seg.ends_with("..>") {
result.push((i, uri.path().len()));
break;
} else if route_seg.ends_with('>') {
let j = i + uri_seg.len();
result.push((i, j));
}
}
result
}
}
impl Clone for Route {
fn clone(&self) -> Route {
Route {
method: self.method,
handler: self.handler,
rank: self.rank,
path: self.path.clone(),
format: self.format.clone(),
}
}
}
impl fmt::Display for Route {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", Green.paint(&self.method), Blue.paint(&self.path))?;
if self.rank > 1 {
write!(f, " [{}]", White.paint(&self.rank))?;
}
if !self.format.is_any() {
write!(f, " {}", Yellow.paint(&self.format))
} else {
Ok(())
}
}
}
impl fmt::Debug for Route {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
<Route as fmt::Display>::fmt(self, f)
}
}
#[doc(hidden)]
impl<'a> From<&'a StaticRouteInfo> for Route {
fn from(info: &'a StaticRouteInfo) -> Route {
let mut route = Route::new(info.method, info.path, info.handler);
route.format = info.format.clone().unwrap_or(ContentType::Any);
if let Some(rank) = info.rank {
route.rank = rank;
}
route
}
}
impl Collider for Route {
fn collides_with(&self, b: &Route) -> bool {
self.method == b.method
&& self.rank == b.rank
&& self.format.collides_with(&b.format)
&& self.path.collides_with(&b.path)
}
}
impl<'r> Collider<Request<'r>> for Route {
fn collides_with(&self, req: &Request<'r>) -> bool {
self.method == req.method()
&& req.uri().collides_with(&self.path)
&& req.content_type().collides_with(&self.format)
}
}