1
- extern crate route_recognizer as router;
2
1
extern crate conduit;
2
+ extern crate route_recognizer as router;
3
3
4
- use std:: collections:: hash_map:: { HashMap , Entry } ;
4
+ use std:: collections:: hash_map:: { Entry , HashMap } ;
5
5
use std:: error:: Error ;
6
6
use std:: fmt;
7
7
8
- use router :: { Router , Match } ;
9
- use conduit :: { Method , Handler , Request , Response } ;
8
+ use conduit :: { Handler , Method , Request , Response } ;
9
+ use router :: { Match , Router } ;
10
10
11
11
pub struct RouteBuilder {
12
12
routers : HashMap < Method , Router < Box < dyn Handler > > > ,
@@ -17,21 +17,29 @@ pub struct RouterError(String);
17
17
18
18
impl RouteBuilder {
19
19
pub fn new ( ) -> RouteBuilder {
20
- RouteBuilder { routers : HashMap :: new ( ) }
20
+ RouteBuilder {
21
+ routers : HashMap :: new ( ) ,
22
+ }
21
23
}
22
24
23
- pub fn recognize < ' a > ( & ' a self , method : & Method , path : & str )
24
- -> Result < Match < & ' a Box < dyn Handler > > ,
25
- RouterError >
26
- {
25
+ pub fn recognize < ' a > (
26
+ & ' a self ,
27
+ method : & Method ,
28
+ path : & str ,
29
+ ) -> Result < Match < & ' a Box < dyn Handler > > , RouterError > {
27
30
match self . routers . get ( method) {
28
31
Some ( router) => router. recognize ( path) ,
29
32
None => Err ( format ! ( "No router found for {:?}" , method) ) ,
30
- } . map_err ( RouterError )
33
+ }
34
+ . map_err ( RouterError )
31
35
}
32
36
33
- pub fn map < ' a , H : Handler > ( & ' a mut self , method : Method , pattern : & str ,
34
- handler : H ) -> & ' a mut RouteBuilder {
37
+ pub fn map < ' a , H : Handler > (
38
+ & ' a mut self ,
39
+ method : Method ,
40
+ pattern : & str ,
41
+ handler : H ,
42
+ ) -> & ' a mut RouteBuilder {
35
43
{
36
44
let router = match self . routers . entry ( method) {
37
45
Entry :: Occupied ( e) => e. into_mut ( ) ,
@@ -42,41 +50,36 @@ impl RouteBuilder {
42
50
self
43
51
}
44
52
45
- pub fn get < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H )
46
- -> & ' a mut RouteBuilder {
53
+ pub fn get < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H ) -> & ' a mut RouteBuilder {
47
54
self . map ( Method :: Get , pattern, handler)
48
55
}
49
56
50
- pub fn post < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H )
51
- -> & ' a mut RouteBuilder {
57
+ pub fn post < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H ) -> & ' a mut RouteBuilder {
52
58
self . map ( Method :: Post , pattern, handler)
53
59
}
54
60
55
- pub fn put < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H )
56
- -> & ' a mut RouteBuilder {
61
+ pub fn put < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H ) -> & ' a mut RouteBuilder {
57
62
self . map ( Method :: Put , pattern, handler)
58
63
}
59
64
60
- pub fn delete < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H )
61
- -> & ' a mut RouteBuilder {
65
+ pub fn delete < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H ) -> & ' a mut RouteBuilder {
62
66
self . map ( Method :: Delete , pattern, handler)
63
67
}
64
68
65
- pub fn head < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H )
66
- -> & ' a mut RouteBuilder {
69
+ pub fn head < ' a , H : Handler > ( & ' a mut self , pattern : & str , handler : H ) -> & ' a mut RouteBuilder {
67
70
self . map ( Method :: Head , pattern, handler)
68
71
}
69
72
}
70
73
71
74
impl conduit:: Handler for RouteBuilder {
72
- fn call ( & self , request : & mut dyn Request ) -> Result < Response , Box < dyn Error + Send > > {
75
+ fn call ( & self , request : & mut dyn Request ) -> Result < Response , Box < dyn Error + Send > > {
73
76
let m = {
74
77
let method = request. method ( ) ;
75
78
let path = request. path ( ) ;
76
79
77
80
match self . recognize ( & method, path) {
78
81
Ok ( m) => m,
79
- Err ( e) => return Err ( Box :: new ( e) as Box < dyn Error + Send > )
82
+ Err ( e) => return Err ( Box :: new ( e) as Box < dyn Error + Send > ) ,
80
83
}
81
84
} ;
82
85
@@ -90,7 +93,9 @@ impl conduit::Handler for RouteBuilder {
90
93
}
91
94
92
95
impl Error for RouterError {
93
- fn description ( & self ) -> & str { & self . 0 }
96
+ fn description ( & self ) -> & str {
97
+ & self . 0
98
+ }
94
99
}
95
100
96
101
impl fmt:: Display for RouterError {
@@ -104,7 +109,8 @@ pub trait RequestParams<'a> {
104
109
}
105
110
106
111
pub fn params < ' a > ( req : & ' a dyn Request ) -> & ' a router:: Params {
107
- req. extensions ( ) . find :: < router:: Params > ( )
112
+ req. extensions ( )
113
+ . find :: < router:: Params > ( )
108
114
. expect ( "Missing params" )
109
115
}
110
116
@@ -121,40 +127,64 @@ mod tests {
121
127
use std:: io;
122
128
use std:: net:: SocketAddr ;
123
129
124
- use { RouteBuilder , RequestParams } ;
130
+ use { RequestParams , RouteBuilder } ;
125
131
126
132
use conduit;
127
- use conduit:: { Handler , Method , Scheme , Host , Headers , Extensions , TypeMap } ;
133
+ use conduit:: { Extensions , Handler , Headers , Host , Method , Scheme , TypeMap } ;
128
134
129
135
struct RequestSentinel {
130
136
method : Method ,
131
137
path : String ,
132
- extensions : conduit:: Extensions
138
+ extensions : conduit:: Extensions ,
133
139
}
134
140
135
141
impl RequestSentinel {
136
142
fn new ( method : Method , path : & ' static str ) -> RequestSentinel {
137
143
RequestSentinel {
138
144
path : path. to_string ( ) ,
139
145
extensions : TypeMap :: new ( ) ,
140
- method : method
146
+ method : method,
141
147
}
142
148
}
143
149
}
144
150
145
151
impl conduit:: Request for RequestSentinel {
146
- fn http_version ( & self ) -> semver:: Version { unimplemented ! ( ) }
147
- fn conduit_version ( & self ) -> semver:: Version { unimplemented ! ( ) }
148
- fn method ( & self ) -> Method { self . method . clone ( ) }
149
- fn scheme ( & self ) -> Scheme { unimplemented ! ( ) }
150
- fn host < ' a > ( & ' a self ) -> Host < ' a > { unimplemented ! ( ) }
151
- fn virtual_root < ' a > ( & ' a self ) -> Option < & ' a str > { unimplemented ! ( ) }
152
- fn path < ' a > ( & ' a self ) -> & ' a str { & self . path }
153
- fn query_string < ' a > ( & ' a self ) -> Option < & ' a str > { unimplemented ! ( ) }
154
- fn remote_addr ( & self ) -> SocketAddr { unimplemented ! ( ) }
155
- fn content_length ( & self ) -> Option < u64 > { unimplemented ! ( ) }
156
- fn headers < ' a > ( & ' a self ) -> & ' a dyn Headers { unimplemented ! ( ) }
157
- fn body < ' a > ( & ' a mut self ) -> & ' a mut dyn io:: Read { unimplemented ! ( ) }
152
+ fn http_version ( & self ) -> semver:: Version {
153
+ unimplemented ! ( )
154
+ }
155
+ fn conduit_version ( & self ) -> semver:: Version {
156
+ unimplemented ! ( )
157
+ }
158
+ fn method ( & self ) -> Method {
159
+ self . method . clone ( )
160
+ }
161
+ fn scheme ( & self ) -> Scheme {
162
+ unimplemented ! ( )
163
+ }
164
+ fn host < ' a > ( & ' a self ) -> Host < ' a > {
165
+ unimplemented ! ( )
166
+ }
167
+ fn virtual_root < ' a > ( & ' a self ) -> Option < & ' a str > {
168
+ unimplemented ! ( )
169
+ }
170
+ fn path < ' a > ( & ' a self ) -> & ' a str {
171
+ & self . path
172
+ }
173
+ fn query_string < ' a > ( & ' a self ) -> Option < & ' a str > {
174
+ unimplemented ! ( )
175
+ }
176
+ fn remote_addr ( & self ) -> SocketAddr {
177
+ unimplemented ! ( )
178
+ }
179
+ fn content_length ( & self ) -> Option < u64 > {
180
+ unimplemented ! ( )
181
+ }
182
+ fn headers < ' a > ( & ' a self ) -> & ' a dyn Headers {
183
+ unimplemented ! ( )
184
+ }
185
+ fn body < ' a > ( & ' a mut self ) -> & ' a mut dyn io:: Read {
186
+ unimplemented ! ( )
187
+ }
158
188
fn extensions < ' a > ( & ' a self ) -> & ' a Extensions {
159
189
& self . extensions
160
190
}
@@ -202,14 +232,14 @@ mod tests {
202
232
}
203
233
204
234
fn test_handler ( req : & mut dyn conduit:: Request ) -> io:: Result < conduit:: Response > {
205
- let mut res = vec ! ( ) ;
235
+ let mut res = vec ! [ ] ;
206
236
res. push ( req. params ( ) [ "id" ] . clone ( ) ) ;
207
237
res. push ( format ! ( "{:?}" , req. method( ) ) ) ;
208
238
209
239
Ok ( conduit:: Response {
210
240
status : ( 200 , "OK" ) ,
211
241
headers : HashMap :: new ( ) ,
212
- body : Box :: new ( io:: Cursor :: new ( res. join ( ", " ) . into_bytes ( ) ) )
242
+ body : Box :: new ( io:: Cursor :: new ( res. join ( ", " ) . into_bytes ( ) ) ) ,
213
243
} )
214
244
}
215
245
}
0 commit comments