@@ -147,7 +147,7 @@ impl<T> Router<T> {
147
147
}
148
148
}
149
149
150
- pub fn add ( & mut self , mut route : & str , dest : T ) {
150
+ pub fn add ( & mut self , mut route : & str , dest : T ) -> Result < ( ) , String > {
151
151
if !route. is_empty ( ) && route. as_bytes ( ) [ 0 ] == b'/' {
152
152
route = & route[ 1 ..] ;
153
153
}
@@ -177,13 +177,18 @@ impl<T> Router<T> {
177
177
let mut hashes = HashSet :: new ( ) ;
178
178
for name in metadata. param_names . iter ( ) {
179
179
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
+ ) ) ;
181
185
}
182
186
}
183
187
184
188
nfa. acceptance ( state) ;
185
189
nfa. metadata ( state, metadata) ;
186
190
self . handlers . insert ( state, dest) ;
191
+ Ok ( ( ) )
187
192
}
188
193
189
194
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 {
251
256
fn basic_router ( ) {
252
257
let mut router = Router :: new ( ) ;
253
258
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 ( ) ;
257
262
258
263
let m = router. recognize ( "/thomas" ) . unwrap ( ) ;
259
264
@@ -264,30 +269,30 @@ fn basic_router() {
264
269
#[ test]
265
270
fn root_router ( ) {
266
271
let mut router = Router :: new ( ) ;
267
- router. add ( "/" , 10 ) ;
272
+ router. add ( "/" , 10 ) . unwrap ( ) ;
268
273
assert_eq ! ( * router. recognize( "/" ) . unwrap( ) . handler, 10 )
269
274
}
270
275
271
276
#[ test]
272
277
fn empty_path ( ) {
273
278
let mut router = Router :: new ( ) ;
274
- router. add ( "/" , 12 ) ;
279
+ router. add ( "/" , 12 ) . unwrap ( ) ;
275
280
assert_eq ! ( * router. recognize( "" ) . unwrap( ) . handler, 12 )
276
281
}
277
282
278
283
#[ test]
279
284
fn empty_route ( ) {
280
285
let mut router = Router :: new ( ) ;
281
- router. add ( "" , 12 ) ;
286
+ router. add ( "" , 12 ) . unwrap ( ) ;
282
287
assert_eq ! ( * router. recognize( "/" ) . unwrap( ) . handler, 12 )
283
288
}
284
289
285
290
#[ test]
286
291
fn ambiguous_router ( ) {
287
292
let mut router = Router :: new ( ) ;
288
293
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 ( ) ;
291
296
292
297
let id = router. recognize ( "/posts/1" ) . unwrap ( ) ;
293
298
@@ -303,8 +308,8 @@ fn ambiguous_router() {
303
308
fn ambiguous_router_b ( ) {
304
309
let mut router = Router :: new ( ) ;
305
310
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 ( ) ;
308
313
309
314
let id = router. recognize ( "/posts/1" ) . unwrap ( ) ;
310
315
@@ -320,8 +325,12 @@ fn ambiguous_router_b() {
320
325
fn multiple_params ( ) {
321
326
let mut router = Router :: new ( ) ;
322
327
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 ( ) ;
325
334
326
335
let com = router. recognize ( "/posts/12/comments/100" ) . unwrap ( ) ;
327
336
let coms = router. recognize ( "/posts/12/comments" ) . unwrap ( ) ;
@@ -338,8 +347,8 @@ fn multiple_params() {
338
347
fn star ( ) {
339
348
let mut router = Router :: new ( ) ;
340
349
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 ( ) ;
343
352
344
353
let m = router. recognize ( "/test" ) . unwrap ( ) ;
345
354
assert_eq ! ( * m. handler, "test" . to_string( ) ) ;
@@ -358,8 +367,8 @@ fn star() {
358
367
fn unnamed_parameters ( ) {
359
368
let mut router = Router :: new ( ) ;
360
369
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 ( ) ;
363
372
let m = router. recognize ( "/foo/test/bar" ) . unwrap ( ) ;
364
373
assert_eq ! ( * m. handler, "test" ) ;
365
374
assert_eq ! ( m. params, Params :: new( ) ) ;
@@ -370,38 +379,35 @@ fn unnamed_parameters() {
370
379
}
371
380
372
381
#[ test]
373
- #[ should_panic]
374
382
fn duplicate_named_parameter ( ) {
375
383
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 ( ) ) ;
377
385
}
378
386
379
387
#[ test]
380
- #[ should_panic]
381
388
fn duplicate_star_parameter ( ) {
382
389
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 ( ) ) ;
384
391
}
385
392
386
393
#[ test]
387
- #[ should_panic]
388
394
fn duplicate_mixed_parameter ( ) {
389
395
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 ( ) ) ;
391
397
}
392
398
393
399
#[ test]
394
- #[ should_panic]
395
400
fn duplicate_mixed_reversed_parameter ( ) {
396
401
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 ( ) ) ;
398
403
}
399
404
400
405
#[ test]
401
- #[ should_panic]
402
406
fn duplicate_separated_parameter ( ) {
403
407
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( ) ) ;
405
411
}
406
412
407
413
#[ allow( dead_code) ]
0 commit comments