A lightweight high performance HTTP request router for Rust
Fast: See benchmark
Micro: The src/lib.rs file is ~405 lines of code (Includes comments)
Flexible:
Static segment. e.g. /users
.
Named parameters. e.g. :name
.
Catch-All parameters. e.g. *any
, it must always be at the end of the pattern.
Supports multiple naming for the same path segment. e.g. /users/:id
and /users/:user_id/repos
.
Don't care about routes orders, recursive lookup, Static
-> Named
-> Catch-All
.
```rust use path_tree::PathTree;
let mut tree = PathTree::
tree.insert("/", 0); tree.insert("/users", 1); tree.insert("/users/:id", 2); tree.insert("/users/:id/:org", 3); tree.insert("/users/:userid/repos", 4); tree.insert("/users/:userid/repos/:id", 5); tree.insert("/users/:user_id/repos/:id/any", 6); tree.insert("/:username", 7); tree.insert("/any", 8); tree.insert("/about", 9); tree.insert("/about/", 10); tree.insert("/about/us", 11); tree.insert("/users/repos/*any", 12);
// Matched "/" let node = tree.find("/"); asserteq!(node.issome(), true); let res = node.unwrap(); asserteq!(*res.0, 0); asserteq!(res.1, []); // Params
// Matched "/:username" let node = tree.find("/username"); asserteq!(node.issome(), true); let res = node.unwrap(); asserteq!(*res.0, 7); asserteq!(res.1, [("username", "username")]); // Params
// Matched "/any" let node = tree.find("/user/s"); let res = node.unwrap(); assert_eq!(res.0, 8); assert_eq!(res.1, [("any", "user/s")]);
// Matched "/users/:id" let node = tree.find("/users/fundon"); let res = node.unwrap(); asserteq!(*res.0, 2); asserteq!(res.1, [("id", "fundon")]); // Params
// Matched "/users/:userid/repos/:id" let node = tree.find("/users/fundon/repos/viz-rs"); let res = node.unwrap(); asserteq!(*res.0, 5); asserteq!(res.1, [("userid", "fundon"), ("id", "viz-rs")]); // Params
// Matched "/users/:userid/repos/:id/*any" let node = tree.find("/users/fundon/repos/viz-rs/noder/issues"); let res = node.unwrap(); asserteq!(*res.0, 6); asserteq!( res.1, [ ("userid", "fundon"), ("id", "viz-rs"), ("any", "noder/issues"), ] ); // Params
// Matched "/users/repos/any" let node = tree.find("/users/repos/"); let res = node.unwrap(); assert_eq!(res.0, 12); assert_eq!(res.1, []); ```
shell
$ cargo bench
It is inspired by the:
Wrappers for path-tree in other languages:
Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.