Expand description
This crate can be used to parse URIs like the ones we can found in OpenApi spec for paths (/foo/{bar}). Once the pattern is parsed, you can check if any string matches against it. You can also compare two patterns to find the more specific.
For now it doesn’t handle any other pattern than {pattern}. Feel free to open an issue if you have a need for a specific usecase. Can probably be used for paths on filesystems as well if one can find a usecase for this.
§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"));
Structs§
- Struct used to parse strings as patterns - Check if an incoming string matches a pattern - Pattern Comparison