| Crates.io | uri-template-ex |
| lib.rs | uri-template-ex |
| version | 0.0.2 |
| created_at | 2025-03-08 10:51:53.973912+00 |
| updated_at | 2025-04-01 01:36:20.268869+00 |
| description | RFC6570 URI Template Level 2 implementation |
| homepage | |
| repository | https://github.com/frozenlib/uri-template-ex |
| max_upload_size | |
| id | 1584295 |
| size | 52,633 |
Implementation of RFC6570 URI Template Level 2
uri-template-ex is a crate that implements URL expansion and variable extraction (capture) using URI Template Level 2 as defined in RFC6570.
Since RFC6570 only defines variable expansion and not extraction, most existing URI Template implementations only support variable expansion and do not support extraction. This crate supports variable extraction using the same syntax as URI Template.
URI Template Level 2 supports the following 3 types of variables:
{var}{+var}{#var}Add the following to your Cargo.toml:
[dependencies]
uri-template-ex = "0.0.2"
Example of generating a URI using a URI template:
use std::collections::BTreeMap;
use uri_template_ex::UriTemplate;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let template = UriTemplate::new("/users/{a}/files/{b}")?;
let mut vars = BTreeMap::new();
vars.insert("a", "xxx");
vars.insert("b", "hello-world");
let uri = template.expand(&vars);
assert_eq!(uri, "/users/xxx/files/hello-world");
Ok(())
}
Example of extracting values that match a template from a URI:
use uri_template_ex::UriTemplate;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let template = UriTemplate::new("/users/{a}/files/{b}")?;
let uri = "/users/xxx/files/hello-world";
if let Some(captures) = template.captures(uri) {
if let Some(a) = captures.name("a") {
println!("a: {}", a.value()?); // "xxx"
}
if let Some(b) = captures.name("b") {
println!("b: {}", b.value()?); // "hello-world"
}
}
Ok(())
}
This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.