matchit icon indicating copy to clipboard operation
matchit copied to clipboard

Match fails depending on order of insertion

Open B-Lorentz opened this issue 1 month ago • 1 comments

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();
}

B-Lorentz avatar Dec 03 '25 14:12 B-Lorentz

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;

ck961018 avatar Dec 06 '25 05:12 ck961018