1- <<<<<<< HEAD
21use std:: {
32 cmp:: Ordering ,
4- collections:: { btree_map , BTreeMap } ,
3+ collections:: { btree_map, BTreeMap , HashSet } ,
54 ops:: Index ,
65} ;
76
87use 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
188
199pub mod nfa;
2010
@@ -157,7 +147,7 @@ impl<T> Router<T> {
157147 }
158148 }
159149
160- pub fn add ( & mut self , mut route : & str , dest : T ) {
150+ pub fn add ( & mut self , mut route : & str , dest : T ) -> Result < ( ) , String > {
161151 if !route. is_empty ( ) && route. as_bytes ( ) [ 0 ] == b'/' {
162152 route = & route[ 1 ..] ;
163153 }
@@ -187,13 +177,18 @@ impl<T> Router<T> {
187177 let mut hashes = HashSet :: new ( ) ;
188178 for name in metadata. param_names . iter ( ) {
189179 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+ ) ) ;
191185 }
192186 }
193187
194188 nfa. acceptance ( state) ;
195189 nfa. metadata ( state, metadata) ;
196190 self . handlers . insert ( state, dest) ;
191+ Ok ( ( ) )
197192 }
198193
199194 pub fn recognize ( & self , mut path : & str ) -> Result < Match < & T > , String > {
@@ -376,7 +371,6 @@ mod tests {
376371 router. add ( "/a/*b/c" , "abc" . to_string ( ) ) ;
377372 router. add ( "/a/*b/c/:d" , "abcd" . to_string ( ) ) ;
378373
379- <<<<<<< HEAD
380374 let m = router. recognize ( "/a/foo" ) . unwrap ( ) ;
381375 assert_eq ! ( * m. handler, "ab" . to_string( ) ) ;
382376 assert_eq ! ( m. params, params( "b" , "foo" ) ) ;
@@ -396,49 +390,6 @@ mod tests {
396390 let m = router. recognize ( "/a/foo/c/baz" ) . unwrap ( ) ;
397391 assert_eq ! ( * m. handler, "abcd" . to_string( ) ) ;
398392 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
442393
443394 let m = router. recognize ( "/a/foo/bar/c/baz" ) . unwrap ( ) ;
444395 assert_eq ! ( * m. handler, "abcd" . to_string( ) ) ;
0 commit comments