2 unstable releases

0.2.0 Jan 19, 2024
0.1.0 Dec 29, 2023

#6 in #xitca-web

Download history 64/week @ 2023-12-31 15/week @ 2024-01-07 114/week @ 2024-01-14 43/week @ 2024-01-21 17/week @ 2024-01-28 78/week @ 2024-02-04 50/week @ 2024-02-11 43/week @ 2024-02-18 91/week @ 2024-02-25 79/week @ 2024-03-03 52/week @ 2024-03-10 10/week @ 2024-03-17 70/week @ 2024-03-24 247/week @ 2024-03-31 36/week @ 2024-04-07 11/week @ 2024-04-14

366 downloads per month
Used in 3 crates (via xitca-http)

MIT license

105KB
2.5K SLoC

A fork of matchit

Compare to matchit

  • Pros
    • clean public types with no lifetime pollution. (easier to pass params around)
    • 100% safe Rust. (unsafe code still used through dependencies)
  • Cons
    • immutable router value.
    • potentially slower in micro benchmark.

lib.rs:

A fork of matchit using small string type for params lifetime elision.

let mut router = xitca_router::Router::new();
router.insert("/home", "Welcome!")?;
router.insert("/users/:id", "A User")?;

let matched = router.at("/users/978")?;
assert_eq!(*matched.value, "A User");

// params is owned value that can be sent between threads.
let params = matched.params;
std::thread::spawn(move || {
    assert_eq!(params.get("id"), Some("978"));
})
.join()
.unwrap();

Parameters

Along with static routes, the router also supports dynamic route segments. These can either be named or catch-all parameters:

Named Parameters

Named parameters like /:id match anything until the next / or the end of the path:

let mut m = xitca_router::Router::new();
m.insert("/users/:id", true)?;

assert_eq!(m.at("/users/1")?.params.get("id"), Some("1"));
assert_eq!(m.at("/users/23")?.params.get("id"), Some("23"));
assert!(m.at("/users").is_err());

Catch-all Parameters

Catch-all parameters start with * and match everything after the /. They must always be at the end of the route:

let mut m = xitca_router::Router::new();
m.insert("/*p", true)?;

assert_eq!(m.at("/foo.js")?.params.get("p"), Some("foo.js"));
assert_eq!(m.at("/c/bar.css")?.params.get("p"), Some("c/bar.css"));

Relaxed Catch-all Parameters

Relaxed Catch-all parameters with a single * and match everything after the /(Including / itself). Since there is no identifier for Params key associated they are left empty. They must always be at the end of the route:

let mut m = xitca_router::Router::new();
m.insert("/*", true)?;

assert!(m.at("/")?.value);
assert!(m.at("/foo")?.value);

Routing Priority

Static and dynamic route segments are allowed to overlap. If they do, static segments will be given higher priority:

let mut m = xitca_router::Router::new();
m.insert("/", "Welcome!").unwrap()    ;  // priority: 1
m.insert("/about", "About Me").unwrap(); // priority: 1
m.insert("/*filepath", "...").unwrap();  // priority: 2

Dependencies