1
- <<<<<<< HEAD
2
1
use std:: {
3
2
cmp:: Ordering ,
4
- collections:: { btree_map , BTreeMap } ,
3
+ collections:: { btree_map, BTreeMap , HashSet } ,
5
4
ops:: Index ,
6
5
} ;
7
6
8
7
use crate :: nfa:: { CharacterClass , NFA } ;
9
- =======
10
- use nfa:: CharacterClass ;
11
- use nfa:: NFA ;
12
- use std:: cmp:: Ordering ;
13
- use std:: collections:: btree_map ;
14
- use std:: collections:: BTreeMap ;
15
- use std:: collections:: HashSet ;
16
- use std:: ops:: Index ;
17
- >>>>>>> Add a test after parsing a route to ensure it doesn' t contain duplicate parameter names
18
8
19
9
pub mod nfa;
20
10
@@ -157,7 +147,7 @@ impl<T> Router<T> {
157
147
}
158
148
}
159
149
160
- pub fn add ( & mut self , mut route : & str , dest : T ) {
150
+ pub fn add ( & mut self , mut route : & str , dest : T ) -> Result < ( ) , String > {
161
151
if !route. is_empty ( ) && route. as_bytes ( ) [ 0 ] == b'/' {
162
152
route = & route[ 1 ..] ;
163
153
}
@@ -187,13 +177,18 @@ impl<T> Router<T> {
187
177
let mut hashes = HashSet :: new ( ) ;
188
178
for name in metadata. param_names . iter ( ) {
189
179
if !hashes. insert ( name. to_string ( ) ) {
190
- panic ! ( "Duplicate name '{}' in route {}" , name. to_string( ) , & route) ;
180
+ return Err ( format ! (
181
+ "Duplicate name '{}' in route {}" ,
182
+ name. to_string( ) ,
183
+ & route
184
+ ) ) ;
191
185
}
192
186
}
193
187
194
188
nfa. acceptance ( state) ;
195
189
nfa. metadata ( state, metadata) ;
196
190
self . handlers . insert ( state, dest) ;
191
+ Ok ( ( ) )
197
192
}
198
193
199
194
pub fn recognize ( & self , mut path : & str ) -> Result < Match < & T > , String > {
@@ -376,7 +371,6 @@ mod tests {
376
371
router. add ( "/a/*b/c" , "abc" . to_string ( ) ) ;
377
372
router. add ( "/a/*b/c/:d" , "abcd" . to_string ( ) ) ;
378
373
379
- <<<<<<< HEAD
380
374
let m = router. recognize ( "/a/foo" ) . unwrap ( ) ;
381
375
assert_eq ! ( * m. handler, "ab" . to_string( ) ) ;
382
376
assert_eq ! ( m. params, params( "b" , "foo" ) ) ;
@@ -396,49 +390,6 @@ mod tests {
396
390
let m = router. recognize ( "/a/foo/c/baz" ) . unwrap ( ) ;
397
391
assert_eq ! ( * m. handler, "abcd" . to_string( ) ) ;
398
392
assert_eq ! ( m. params, two_params( "b" , "foo" , "d" , "baz" ) ) ;
399
- =======
400
- #[ test]
401
- #[ should_panic]
402
- fn duplicate_named_parameter( ) {
403
- let mut router = Router :: new ( ) ;
404
- router. add( "/foo/:bar/:bar" , "test" . to_string( ) ) ;
405
- }
406
-
407
- #[ test]
408
- #[ should_panic]
409
- fn duplicate_star_parameter( ) {
410
- let mut router = Router :: new ( ) ;
411
- router. add( "/foo/*bar/*bar" , "test" . to_string( ) ) ;
412
- }
413
-
414
- #[ test]
415
- #[ should_panic]
416
- fn duplicate_mixed_parameter( ) {
417
- let mut router = Router :: new ( ) ;
418
- router. add( "/foo/*bar/:bar" , "test" . to_string( ) ) ;
419
- }
420
-
421
- #[ test]
422
- #[ should_panic]
423
- fn duplicate_mixed_reversed_parameter( ) {
424
- let mut router = Router :: new ( ) ;
425
- router. add( "/foo/:bar/*bar" , "test" . to_string( ) ) ;
426
- }
427
-
428
- #[ test]
429
- #[ should_panic]
430
- fn duplicate_separated_parameter( ) {
431
- let mut router = Router :: new ( ) ;
432
- router. add( "/foo/:bar/bleg/:bar" , "test" . to_string( ) ) ;
433
- }
434
-
435
- #[ allow( dead_code) ]
436
- fn params ( key : & str , val : & str ) -> Params {
437
- let mut map = Params :: new( ) ;
438
- map. insert( key. to_string( ) , val. to_string( ) ) ;
439
- map
440
- }
441
- >>>>>>> Add a test after parsing a route to ensure it doesn' t contain duplicate parameter names
442
393
443
394
let m = router. recognize ( "/a/foo/bar/c/baz" ) . unwrap ( ) ;
444
395
assert_eq ! ( * m. handler, "abcd" . to_string( ) ) ;
0 commit comments