Skip to content

Commit f745f5c

Browse files
committed
fix empty route being ignored on insert
When constructing the tree, if the first route is the empty string `""` the root would be overwritten due to the `self.prefix.is_empty()`. This adds an additional check to ensure the root is actually `None` before returning a root node. Resolves #75
1 parent d720199 commit f745f5c

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

src/tree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<T> Node<T> {
8181
self.priority += 1;
8282

8383
// If the tree is empty, insert the root node.
84-
if self.prefix.is_empty() && self.children.is_empty() {
84+
if self.value.is_none() && self.children.is_empty() {
8585
let last = self.insert_route(remaining, val)?;
8686
last.remapping = remapping;
8787
self.node_type = NodeType::Root;

tests/match.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ macro_rules! p {
108108
};
109109
}
110110

111+
// https://github.com/ibraheemdev/matchit/issues/75
112+
#[test]
113+
fn empty_route() {
114+
MatchTest {
115+
routes: vec!["", "/foo"],
116+
matches: vec![("", "", p! {}), ("/foo", "/foo", p! {})],
117+
}
118+
.run()
119+
}
120+
111121
// https://github.com/ibraheemdev/matchit/issues/42
112122
#[test]
113123
fn bare_catchall() {

0 commit comments

Comments
 (0)