#uri #template #url #rfc6570 #matching

uri-template-ex

RFC6570 URI Template Level 2 implementation

1 unstable release

new 0.0.1 Mar 8, 2025

#5 in #rfc6570


Used in 2 crates

MIT/Apache

25KB
573 lines

uri-template-ex

Crates.io Docs.rs Actions Status

Implementation of RFC6570 URI Template Level 2

Overview

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.

Features

  • Variable expansion using RFC6570 URI Template Level 2
  • Variable value extraction from URI templates

URI Template Level 2 supports the following 3 types of variables:

  • {var}
  • {+var}
  • {#var}

Installation

Add the following to your Cargo.toml:

[dependencies]
uri-template-ex = "0.0.1"

Usage

Basic Usage Example

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(())
}

Value Extraction from URI

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(())
}

License

This project is dual licensed under Apache-2.0/MIT. See the two LICENSE-* files for details.

Contribution

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.

Dependencies

~2.8–4.5MB
~87K SLoC