Skip to content

Commit

Permalink
support catch-all routes without leading slash
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheemdev committed Mar 10, 2024
1 parent b2cb4ac commit f534ddc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 15 deletions.
18 changes: 4 additions & 14 deletions src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<T> Node<T> {

// the tree is empty
if self.prefix.is_empty() && self.children.is_empty() {
let last = self.insert_child(prefix, &route, val)?;
let last = self.insert_child(prefix, val)?;
last.param_remapping = param_remapping;
self.node_type = NodeType::Root;
return Ok(());
Expand Down Expand Up @@ -115,7 +115,7 @@ impl<T> Node<T> {
child = current.update_child_priority(child);

// insert into the new node
let last = current.children[child].insert_child(prefix, &route, val)?;
let last = current.children[child].insert_child(prefix, val)?;
last.param_remapping = param_remapping;
return Ok(());
}
Expand All @@ -142,7 +142,7 @@ impl<T> Node<T> {
}

// otherwise, create the wildcard node
let last = current.insert_child(prefix, &route, val)?;
let last = current.insert_child(prefix, val)?;
last.param_remapping = param_remapping;
return Ok(());
}
Expand Down Expand Up @@ -197,12 +197,7 @@ impl<T> Node<T> {
}

// insert a child node at this node
fn insert_child(
&mut self,
mut prefix: &[u8],
route: &[u8],
val: T,
) -> Result<&mut Node<T>, InsertError> {
fn insert_child(&mut self, mut prefix: &[u8], val: T) -> Result<&mut Node<T>, InsertError> {
let mut current = self;

loop {
Expand All @@ -224,11 +219,6 @@ impl<T> Node<T> {
return Err(InsertError::InvalidCatchAll);
}

// "*x" without leading `/`
if prefix == route && route[0] != b'/' {
return Err(InsertError::InvalidCatchAll);
}

// insert prefix before the current wildcard
if wildcard_index > 0 {
current.prefix = prefix[..wildcard_index].to_owned();
Expand Down
6 changes: 5 additions & 1 deletion tests/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ fn invalid_catchall() {
("/src/{*filepath}/x", Err(InsertError::InvalidCatchAll)),
("/src2/", Ok(())),
("/src2/{*filepath}/x", Err(InsertError::InvalidCatchAll)),
("{*x}", Err(InsertError::InvalidCatchAll)),
])
.run()
}
Expand Down Expand Up @@ -203,3 +202,8 @@ fn duplicate_conflict() {
])
.run()
}

#[test]
fn bare_catchall() {
InsertTest(vec![("{*foo}", Ok(())), ("foo/{*bar}", Ok(()))]).run()
}
15 changes: 15 additions & 0 deletions tests/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ macro_rules! p {
};
}

// https://github.com/ibraheemdev/matchit/issues/42
#[test]
fn bare_catchall() {
MatchTest {
routes: vec!["{*foo}", "foo/{*bar}"],
matches: vec![
("x/y", "{*foo}", p! { "foo" => "x/y" }),
("/x/y", "{*foo}", p! { "foo" => "/x/y" }),
("/foo/x/y", "{*foo}", p! { "foo" => "/foo/x/y" }),
("foo/x/y", "foo/{*bar}", p! { "bar" => "x/y" }),
],
}
.run()
}

#[test]
fn normalized() {
MatchTest {
Expand Down

0 comments on commit f534ddc

Please sign in to comment.