#template #uri #url #rfc6570 #uritemplates

uritemplate-next

Rust implementation of RFC6570 - URI Template that can processURI Templates up and to including ones designated Level 4

1 unstable release

0.2.0 Nov 21, 2020

#254 in Template engine

Download history 105573/week @ 2024-07-21 99214/week @ 2024-07-28 99868/week @ 2024-08-04 121058/week @ 2024-08-11 135206/week @ 2024-08-18 128595/week @ 2024-08-25 111539/week @ 2024-09-01 150997/week @ 2024-09-08 119286/week @ 2024-09-15 154314/week @ 2024-09-22 150316/week @ 2024-09-29 186687/week @ 2024-10-06 150783/week @ 2024-10-13 176234/week @ 2024-10-20 177280/week @ 2024-10-27 179267/week @ 2024-11-03

690,307 downloads per month
Used in 41 crates (4 directly)

BSD-3-Clause

32KB
551 lines

rust-uritemplate

Build Status Cargo version License

Documentation

rust-uritemplate is a Rust implementation of RFC6570 - URI Template that can process URI Templates up to and including ones designated as Level 4 by the specification. It passes all of the tests in the uritemplate-test test suite.

Basic Usage

Variable setting can be chained for nice, clean code.

let uri = UriTemplate::new("/view/{object:1}/{/object,names}{?query*}")
    .set("object", "lakes")
    .set("names", &["Erie", "Superior", "Ontario"])
    .set("query", &[("size", "15"), ("lang", "en")])
    .build();

assert_eq!(uri, "/view/l/lakes/Erie,Superior,Ontario?size=15&lang=en");

It is not possible to set a variable to the value "undefined". Instead, simply delete the variable if you have already set it.

let mut t = UriTemplate::new("{hello}");
t.set("hello", "Hello World!");
assert_eq!(t.build(), "Hello%20World%21");

t.delete("hello");
assert_eq!(t.build(), "");

The delete function returns true if the variable existed and false otherwise.

Supported Types

Any type that implements IntoTemplateVar can be set as the value of a UriTemplate variable. The following implementations are provided by default for each type of variable:

  • Scalar Values: String, &str
  • Lists: Vec<String>, &[String], &[str]
  • Associative Arrays: Vec<(String, String)>, &[(String, String)], &[(&str, &str)], &HashMap<String, String>

In addition, you can implement IntoTemplateVar for your own types. View the documentation for IntoTemplateVar for information on how that works.

Dependencies

~2.2–3MB
~54K SLoC