Skip to content

URI (or path) pattern matching

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

gmiam/uri-pattern-matcher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Uri-pattern-matcher

A Rust library for parsing URI (path) patterns, matching against them and compare two patterns with each other.

This library is fast (1 to 10μs to parse common patterns) and don't use any additional allocation.

The library is considered immature (pre 1.0 release) because it hasn't been tested in production yet. However, considering how few features it has, it should be pretty safe to use.

Usage

Add this to your Cargo.toml:

[dependencies]
uri-pattern-matcher = "0.1"

Example

Here are examples for the common usages of this crate:

let pattern: UriPattern = "/api/{resource}/{id}/details".into();
assert!(pattern.is_match("/api/resource/id1/details"));
assert!(pattern.is_match("/api/customer/John/details"));
let pattern: UriPattern = "/api/{foo}/{bar}/zzz".into();
let pattern2: UriPattern = "/api/{foo}/bar/{zzz}".into();
assert_ne!(pattern, pattern2);
assert!(pattern > pattern2);

We are also able to combine all of this using Iterators. Here we'll retrieve the most specific pattern matching our candidate string:

let patterns: Vec<UriPattern> = vec![
    "/api/{foo}/{bar}/zzz".into(),
    "/api/{foo}/bar/{zzz}".into(),
    "/{api}/{foo}/foo/{zzz}".into()
    ];
let candidate = "/api/resource/bar/zzz";
let best_match = patterns.iter()
           .filter(|p| p.is_match(candidate))
           .max();
assert_eq!(best_match.unwrap(), &UriPattern::from("/api/{foo}/{bar}/zzz"));

License

The library is distributed under the terms of both the MIT license and the Apache License (Version 2.0).

See LICENSE-APACHE and LICENSE-MIT.

About

URI (or path) pattern matching

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages