From 11a9b0b0180c7cb56b1a16d0a82b369de0e6b953 Mon Sep 17 00:00:00 2001 From: Gameldar Date: Mon, 19 Aug 2019 13:23:53 +0800 Subject: [PATCH] Change add to return a Result to indicate failures when parsing the route --- src/lib.rs | 62 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 28 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6fbb9ee..47f4c54 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -147,7 +147,7 @@ impl Router { } } - pub fn add(&mut self, mut route: &str, dest: T) { + pub fn add(&mut self, mut route: &str, dest: T) -> Result<(), String> { if !route.is_empty() && route.as_bytes()[0] == b'/' { route = &route[1..]; } @@ -177,13 +177,18 @@ impl Router { let mut hashes = HashSet::new(); for name in metadata.param_names.iter() { if !hashes.insert(name.to_string()) { - panic!("Duplicate name '{}' in route {}", name.to_string(), &route); + return Err(format!( + "Duplicate name '{}' in route {}", + name.to_string(), + &route + )); } } nfa.acceptance(state); nfa.metadata(state, metadata); self.handlers.insert(state, dest); + Ok(()) } pub fn recognize<'a>(&'a self, mut path: &str) -> Result, String> { @@ -251,9 +256,9 @@ fn process_star_state(nfa: &mut NFA, mut state: usize) -> usize { fn basic_router() { let mut router = Router::new(); - router.add("/thomas", "Thomas".to_string()); - router.add("/tom", "Tom".to_string()); - router.add("/wycats", "Yehuda".to_string()); + router.add("/thomas", "Thomas".to_string()).unwrap(); + router.add("/tom", "Tom".to_string()).unwrap(); + router.add("/wycats", "Yehuda".to_string()).unwrap(); let m = router.recognize("/thomas").unwrap(); @@ -264,21 +269,21 @@ fn basic_router() { #[test] fn root_router() { let mut router = Router::new(); - router.add("/", 10); + router.add("/", 10).unwrap(); assert_eq!(*router.recognize("/").unwrap().handler, 10) } #[test] fn empty_path() { let mut router = Router::new(); - router.add("/", 12); + router.add("/", 12).unwrap(); assert_eq!(*router.recognize("").unwrap().handler, 12) } #[test] fn empty_route() { let mut router = Router::new(); - router.add("", 12); + router.add("", 12).unwrap(); assert_eq!(*router.recognize("/").unwrap().handler, 12) } @@ -286,8 +291,8 @@ fn empty_route() { fn ambiguous_router() { let mut router = Router::new(); - router.add("/posts/new", "new".to_string()); - router.add("/posts/:id", "id".to_string()); + router.add("/posts/new", "new".to_string()).unwrap(); + router.add("/posts/:id", "id".to_string()).unwrap(); let id = router.recognize("/posts/1").unwrap(); @@ -303,8 +308,8 @@ fn ambiguous_router() { fn ambiguous_router_b() { let mut router = Router::new(); - router.add("/posts/:id", "id".to_string()); - router.add("/posts/new", "new".to_string()); + router.add("/posts/:id", "id".to_string()).unwrap(); + router.add("/posts/new", "new".to_string()).unwrap(); let id = router.recognize("/posts/1").unwrap(); @@ -320,8 +325,12 @@ fn ambiguous_router_b() { fn multiple_params() { let mut router = Router::new(); - router.add("/posts/:post_id/comments/:id", "comment".to_string()); - router.add("/posts/:post_id/comments", "comments".to_string()); + router + .add("/posts/:post_id/comments/:id", "comment".to_string()) + .unwrap(); + router + .add("/posts/:post_id/comments", "comments".to_string()) + .unwrap(); let com = router.recognize("/posts/12/comments/100").unwrap(); let coms = router.recognize("/posts/12/comments").unwrap(); @@ -338,8 +347,8 @@ fn multiple_params() { fn star() { let mut router = Router::new(); - router.add("*foo", "test".to_string()); - router.add("/bar/*foo", "test2".to_string()); + router.add("*foo", "test".to_string()).unwrap(); + router.add("/bar/*foo", "test2".to_string()).unwrap(); let m = router.recognize("/test").unwrap(); assert_eq!(*m.handler, "test".to_string()); @@ -358,8 +367,8 @@ fn star() { fn unnamed_parameters() { let mut router = Router::new(); - router.add("/foo/:/bar", "test".to_string()); - router.add("/foo/:bar/*", "test2".to_string()); + router.add("/foo/:/bar", "test".to_string()).unwrap(); + router.add("/foo/:bar/*", "test2".to_string()).unwrap(); let m = router.recognize("/foo/test/bar").unwrap(); assert_eq!(*m.handler, "test"); assert_eq!(m.params, Params::new()); @@ -370,38 +379,35 @@ fn unnamed_parameters() { } #[test] -#[should_panic] fn duplicate_named_parameter() { let mut router = Router::new(); - router.add("/foo/:bar/:bar", "test".to_string()); + assert!(router.add("/foo/:bar/:bar", "test".to_string()).is_err()); } #[test] -#[should_panic] fn duplicate_star_parameter() { let mut router = Router::new(); - router.add("/foo/*bar/*bar", "test".to_string()); + assert!(router.add("/foo/*bar/*bar", "test".to_string()).is_err()); } #[test] -#[should_panic] fn duplicate_mixed_parameter() { let mut router = Router::new(); - router.add("/foo/*bar/:bar", "test".to_string()); + assert!(router.add("/foo/*bar/:bar", "test".to_string()).is_err()); } #[test] -#[should_panic] fn duplicate_mixed_reversed_parameter() { let mut router = Router::new(); - router.add("/foo/:bar/*bar", "test".to_string()); + assert!(router.add("/foo/:bar/*bar", "test".to_string()).is_err()); } #[test] -#[should_panic] fn duplicate_separated_parameter() { let mut router = Router::new(); - router.add("/foo/:bar/bleg/:bar", "test".to_string()); + assert!(router + .add("/foo/:bar/bleg/:bar", "test".to_string()) + .is_err()); } #[allow(dead_code)]