@@ -147,7 +147,7 @@ impl<T> Router<T> {
147147 }
148148 }
149149
150- pub fn add ( & mut self , mut route : & str , dest : T ) {
150+ pub fn add ( & mut self , mut route : & str , dest : T ) -> Result < ( ) , String > {
151151 if !route. is_empty ( ) && route. as_bytes ( ) [ 0 ] == b'/' {
152152 route = & route[ 1 ..] ;
153153 }
@@ -177,13 +177,18 @@ impl<T> Router<T> {
177177 let mut hashes = HashSet :: new ( ) ;
178178 for name in metadata. param_names . iter ( ) {
179179 if !hashes. insert ( name. to_string ( ) ) {
180- 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+ ) ) ;
181185 }
182186 }
183187
184188 nfa. acceptance ( state) ;
185189 nfa. metadata ( state, metadata) ;
186190 self . handlers . insert ( state, dest) ;
191+ Ok ( ( ) )
187192 }
188193
189194 pub fn recognize < ' a > ( & ' a self , mut path : & str ) -> Result < Match < & ' a T > , String > {
@@ -251,9 +256,9 @@ fn process_star_state<T>(nfa: &mut NFA<T>, mut state: usize) -> usize {
251256fn basic_router ( ) {
252257 let mut router = Router :: new ( ) ;
253258
254- router. add ( "/thomas" , "Thomas" . to_string ( ) ) ;
255- router. add ( "/tom" , "Tom" . to_string ( ) ) ;
256- router. add ( "/wycats" , "Yehuda" . to_string ( ) ) ;
259+ router. add ( "/thomas" , "Thomas" . to_string ( ) ) . unwrap ( ) ;
260+ router. add ( "/tom" , "Tom" . to_string ( ) ) . unwrap ( ) ;
261+ router. add ( "/wycats" , "Yehuda" . to_string ( ) ) . unwrap ( ) ;
257262
258263 let m = router. recognize ( "/thomas" ) . unwrap ( ) ;
259264
@@ -264,30 +269,30 @@ fn basic_router() {
264269#[ test]
265270fn root_router ( ) {
266271 let mut router = Router :: new ( ) ;
267- router. add ( "/" , 10 ) ;
272+ router. add ( "/" , 10 ) . unwrap ( ) ;
268273 assert_eq ! ( * router. recognize( "/" ) . unwrap( ) . handler, 10 )
269274}
270275
271276#[ test]
272277fn empty_path ( ) {
273278 let mut router = Router :: new ( ) ;
274- router. add ( "/" , 12 ) ;
279+ router. add ( "/" , 12 ) . unwrap ( ) ;
275280 assert_eq ! ( * router. recognize( "" ) . unwrap( ) . handler, 12 )
276281}
277282
278283#[ test]
279284fn empty_route ( ) {
280285 let mut router = Router :: new ( ) ;
281- router. add ( "" , 12 ) ;
286+ router. add ( "" , 12 ) . unwrap ( ) ;
282287 assert_eq ! ( * router. recognize( "/" ) . unwrap( ) . handler, 12 )
283288}
284289
285290#[ test]
286291fn ambiguous_router ( ) {
287292 let mut router = Router :: new ( ) ;
288293
289- router. add ( "/posts/new" , "new" . to_string ( ) ) ;
290- router. add ( "/posts/:id" , "id" . to_string ( ) ) ;
294+ router. add ( "/posts/new" , "new" . to_string ( ) ) . unwrap ( ) ;
295+ router. add ( "/posts/:id" , "id" . to_string ( ) ) . unwrap ( ) ;
291296
292297 let id = router. recognize ( "/posts/1" ) . unwrap ( ) ;
293298
@@ -303,8 +308,8 @@ fn ambiguous_router() {
303308fn ambiguous_router_b ( ) {
304309 let mut router = Router :: new ( ) ;
305310
306- router. add ( "/posts/:id" , "id" . to_string ( ) ) ;
307- router. add ( "/posts/new" , "new" . to_string ( ) ) ;
311+ router. add ( "/posts/:id" , "id" . to_string ( ) ) . unwrap ( ) ;
312+ router. add ( "/posts/new" , "new" . to_string ( ) ) . unwrap ( ) ;
308313
309314 let id = router. recognize ( "/posts/1" ) . unwrap ( ) ;
310315
@@ -320,8 +325,12 @@ fn ambiguous_router_b() {
320325fn multiple_params ( ) {
321326 let mut router = Router :: new ( ) ;
322327
323- router. add ( "/posts/:post_id/comments/:id" , "comment" . to_string ( ) ) ;
324- router. add ( "/posts/:post_id/comments" , "comments" . to_string ( ) ) ;
328+ router
329+ . add ( "/posts/:post_id/comments/:id" , "comment" . to_string ( ) )
330+ . unwrap ( ) ;
331+ router
332+ . add ( "/posts/:post_id/comments" , "comments" . to_string ( ) )
333+ . unwrap ( ) ;
325334
326335 let com = router. recognize ( "/posts/12/comments/100" ) . unwrap ( ) ;
327336 let coms = router. recognize ( "/posts/12/comments" ) . unwrap ( ) ;
@@ -338,8 +347,8 @@ fn multiple_params() {
338347fn star ( ) {
339348 let mut router = Router :: new ( ) ;
340349
341- router. add ( "*foo" , "test" . to_string ( ) ) ;
342- router. add ( "/bar/*foo" , "test2" . to_string ( ) ) ;
350+ router. add ( "*foo" , "test" . to_string ( ) ) . unwrap ( ) ;
351+ router. add ( "/bar/*foo" , "test2" . to_string ( ) ) . unwrap ( ) ;
343352
344353 let m = router. recognize ( "/test" ) . unwrap ( ) ;
345354 assert_eq ! ( * m. handler, "test" . to_string( ) ) ;
@@ -358,8 +367,8 @@ fn star() {
358367fn unnamed_parameters ( ) {
359368 let mut router = Router :: new ( ) ;
360369
361- router. add ( "/foo/:/bar" , "test" . to_string ( ) ) ;
362- router. add ( "/foo/:bar/*" , "test2" . to_string ( ) ) ;
370+ router. add ( "/foo/:/bar" , "test" . to_string ( ) ) . unwrap ( ) ;
371+ router. add ( "/foo/:bar/*" , "test2" . to_string ( ) ) . unwrap ( ) ;
363372 let m = router. recognize ( "/foo/test/bar" ) . unwrap ( ) ;
364373 assert_eq ! ( * m. handler, "test" ) ;
365374 assert_eq ! ( m. params, Params :: new( ) ) ;
@@ -370,38 +379,35 @@ fn unnamed_parameters() {
370379}
371380
372381#[ test]
373- #[ should_panic]
374382fn duplicate_named_parameter ( ) {
375383 let mut router = Router :: new ( ) ;
376- router. add ( "/foo/:bar/:bar" , "test" . to_string ( ) ) ;
384+ assert ! ( router. add( "/foo/:bar/:bar" , "test" . to_string( ) ) . is_err ( ) ) ;
377385}
378386
379387#[ test]
380- #[ should_panic]
381388fn duplicate_star_parameter ( ) {
382389 let mut router = Router :: new ( ) ;
383- router. add ( "/foo/*bar/*bar" , "test" . to_string ( ) ) ;
390+ assert ! ( router. add( "/foo/*bar/*bar" , "test" . to_string( ) ) . is_err ( ) ) ;
384391}
385392
386393#[ test]
387- #[ should_panic]
388394fn duplicate_mixed_parameter ( ) {
389395 let mut router = Router :: new ( ) ;
390- router. add ( "/foo/*bar/:bar" , "test" . to_string ( ) ) ;
396+ assert ! ( router. add( "/foo/*bar/:bar" , "test" . to_string( ) ) . is_err ( ) ) ;
391397}
392398
393399#[ test]
394- #[ should_panic]
395400fn duplicate_mixed_reversed_parameter ( ) {
396401 let mut router = Router :: new ( ) ;
397- router. add ( "/foo/:bar/*bar" , "test" . to_string ( ) ) ;
402+ assert ! ( router. add( "/foo/:bar/*bar" , "test" . to_string( ) ) . is_err ( ) ) ;
398403}
399404
400405#[ test]
401- #[ should_panic]
402406fn duplicate_separated_parameter ( ) {
403407 let mut router = Router :: new ( ) ;
404- router. add ( "/foo/:bar/bleg/:bar" , "test" . to_string ( ) ) ;
408+ assert ! ( router
409+ . add( "/foo/:bar/bleg/:bar" , "test" . to_string( ) )
410+ . is_err( ) ) ;
405411}
406412
407413#[ allow( dead_code) ]
0 commit comments