#http #router #path #url #url-router

pathscheme

A basic path matching language

3 unstable releases

Uses new Rust 2021

0.2.1 May 11, 2022
0.2.0 May 4, 2022
0.1.0 Apr 28, 2022

#38 in #router

MIT license

15KB
355 lines

pathscheme

A Rust crate that supports describing and matching general path schemes.

License

This program is distributed under the MIT license.


lib.rs:

A basic path-matching language.

Examples

Path schemes may match literal paths:

# use pathscheme::*;
assert!("/users/new".parse::<PathScheme>()
    .unwrap()
    .matches("/users/new")
    .unwrap()
    .is_empty());

Literal path matching is case-sensitive:

# use pathscheme::*;
assert!("/users/new".parse::<PathScheme>()
    .unwrap()
    .matches("/users/New")
    .is_none());

Or they may capture path segments:

# use pathscheme::*;
let matches = "/users/:id".parse::<PathScheme>()
    .unwrap()
    .matches("/users/olix0r")
    .unwrap();
assert!(matches.get("id").unwrap() == "olix0r");

Path schemes must match the entire path:

# use pathscheme::*;
assert!("/users/:id".parse::<PathScheme>()
    .unwrap()
    .matches("/users/olix0r/dogs")
    .is_none());

A ** glob operator may be used to match path prefixes:

# use pathscheme::*;
assert!("/users/:id/**".parse::<PathScheme>()
    .unwrap()
    .matches("/users/olix0r")
    .is_some());
assert!("/users/:id/**".parse::<PathScheme>()
    .unwrap()
    .matches("/users/olix0r/dogs")
    .is_some());

Dependencies

~0.7–1.4MB
~29K SLoC