Skip to content

Commit f607f73

Browse files
authored
Fix suffix flag and prefix check (#86)
Resolves #83 and #84.
1 parent 8448782 commit f607f73

3 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/tree.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ impl<T> Node<T> {
199199
let next = remaining[0];
200200

201201
// For parameters with a suffix, we have to find the matching suffix or create a new child node.
202-
if matches!(state.node().node_type, NodeType::Param { .. }) {
202+
if let NodeType::Param { suffix: has_suffix } = state.node().node_type {
203203
let terminator = remaining
204204
.iter()
205205
.position(|&b| b == b'/')
@@ -248,8 +248,10 @@ impl<T> Node<T> {
248248
priority: 1,
249249
..Node::default()
250250
});
251-
let has_suffix = !matches!(*suffix, b"" | b"/");
251+
252+
let has_suffix = has_suffix || !matches!(*suffix, b"" | b"/");
252253
state.node_mut().node_type = NodeType::Param { suffix: has_suffix };
254+
253255
state = state.set_child(child);
254256

255257
// If this is the final route segment, insert the value.
@@ -409,6 +411,10 @@ impl<T> Node<T> {
409411
/// Returns `true` if there is a wildcard node that contains a prefix within the current route segment,
410412
/// i.e. before the next trailing slash
411413
fn prefix_wild_child_in_segment(&self) -> bool {
414+
if matches!(self.node_type, NodeType::Root) && self.prefix.is_empty() {
415+
return false;
416+
}
417+
412418
if self.prefix.ends_with(b"/") {
413419
self.children.iter().any(Node::prefix_wild_child_in_segment)
414420
} else {

tests/insert.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ fn conflict(with: &'static str) -> InsertError {
1616
InsertError::Conflict { with: with.into() }
1717
}
1818

19+
// https://github.com/ibraheemdev/matchit/issues/84
20+
#[test]
21+
fn root_prefix_issue() {
22+
InsertTest(vec![("{foo}", Ok(())), ("{foo}suffix", Ok(()))]).run()
23+
}
24+
1925
#[test]
2026
fn wildcard_conflict() {
2127
InsertTest(vec![

tests/match.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ fn bare_catchall() {
134134
.run()
135135
}
136136

137+
// https://github.com/ibraheemdev/matchit/issues/83
138+
#[test]
139+
fn param_suffix_flag_issue() {
140+
MatchTest {
141+
routes: vec!["/foo/{foo}suffix", "/foo/{foo}/bar"],
142+
matches: vec![("/foo/barsuffix", "/foo/{foo}suffix", p! { "foo" => "bar" })],
143+
}
144+
.run()
145+
}
146+
137147
#[test]
138148
fn normalized() {
139149
MatchTest {

0 commit comments

Comments
 (0)