4 releases

0.2.0 Jul 30, 2023
0.1.0 Oct 20, 2022
0.1.0-beta.1 Oct 16, 2022

#16 in #url-path


Used in lieweb

MIT license

40KB
1K SLoC

pathrouter

Crates.io Documentation Crates.io Rust

Overview

pathrouter is a simple router.

Usage

use pathrouter::{Router, Params};

let mut router = Router::new();

router.add("/posts", "posts");
router.add("/posts/:post_id", "post");

let (endpoint, params) = router.route("/posts/1").unwrap();

License

This project is licensed under the MIT license.


lib.rs:

Recognizes URL path patterns with support for dynamic and wildcard segments

Examples

use pathrouter::{Router, Params};

let mut router = Router::new();

router.add("/posts", "posts");
router.add("/posts/:post_id", "post");

let (endpoint, params) = router.route("/posts/1").unwrap();

assert_eq!(*endpoint, "post");
let mut path_params = Params::new();
path_params.insert("post_id", "1");
assert_eq!(params, path_params);

Routing params

The router supports four kinds of route segments:

  • segments: these are of the format /a/b.
  • params: these are of the format /a/:b.
  • wildcards: these are of the format /a/*b.

No runtime deps