matchit
matchit copied to clipboard
Match fails depending on order of insertion
On 0.9.0 The following code fails:
#[test]
fn zendesk_debug() {
let mut router = Router::new();
router.insert("/api/v2/tickets/{id}.json", ()).unwrap();
router
.insert("/api/v2/tickets/{id}/comments.json", ())
.unwrap();
router.at("/api/v2/tickets/101.json").unwrap();
}
But if the routes are inserted in the other order, it succeeds:
#[test]
fn zendesk_debug() {
let mut router = Router::new();
router
.insert("/api/v2/tickets/{id}/comments.json", ())
.unwrap();
router.insert("/api/v2/tickets/{id}.json", ()).unwrap();
router.at("/api/v2/tickets/101.json").unwrap();
}
The cause of the error is that when inserting a param without a suffix, the code did not check whether the node already contained a suffix. It looks like this can be fixed as follows:
// tree.rs 251
let pre_has_suffix =
matches!(state.node().node_type, NodeType::Param { suffix: true });
let has_suffix = !matches!(*suffix, b"" | b"/") || pre_has_suffix;