Skip to content

Commit

Permalink
Change add to return a Result to indicate failures when parsing the r…
Browse files Browse the repository at this point in the history
…oute
  • Loading branch information
Gameldar committed Aug 19, 2019
1 parent 32c73d2 commit 11a9b0b
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ impl<T> Router<T> {
}
}

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..];
}
Expand Down Expand Up @@ -177,13 +177,18 @@ impl<T> Router<T> {
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<Match<&'a T>, String> {
Expand Down Expand Up @@ -251,9 +256,9 @@ fn process_star_state<T>(nfa: &mut NFA<T>, 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();

Expand All @@ -264,30 +269,30 @@ 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)
}

#[test]
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();

Expand All @@ -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();

Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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());
Expand All @@ -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)]
Expand Down

0 comments on commit 11a9b0b

Please sign in to comment.