Struct uri_template_system_core::Template
source · pub struct Template<'t> { /* private fields */ }Expand description
The Template type is the basis for most simple tasks. Parsing and
expansion are both template functions.
Implementations§
source§impl<'t> Template<'t>
impl<'t> Template<'t>
sourcepub fn expand(&self, values: &Values) -> Result<String, ExpandError>
pub fn expand(&self, values: &Values) -> Result<String, ExpandError>
Expands the template using the given Values, returning a String
if expansion was successful.
Errors
This function may fail due to internal formatting errors
(std::fmt::Write is an abstraction which allows for underlying
failures) though this is very unlikely given String output.
let template = Template::parse("hello/{name}!").unwrap();
let values = Values::default().add("name", Value::item("world"));
assert_eq!("hello/world!", template.expand(&values).unwrap());Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() -> Result<(), Box<dyn Error>> {
let template = Template::parse("/hello/{name}{/library*}")?;
let values = Values::default()
.add("name", Value::item("world"))
.add("library", Value::list(["uri", "template", "system"]));
assert_eq!(
template.expand(&values)?,
"/hello/world/uri/template/system"
);
Ok(())
}sourcepub fn parse(raw: &'t str) -> Result<Self, ParseError>
pub fn parse(raw: &'t str) -> Result<Self, ParseError>
Parses a &str representing a potential template, and returns a new
Template instance if valid. See RFC6570
for the grammar of a valid URI Template. uri-template-system supports
all operators and modifiers up-to and including Level 4.
Errors
This function may fail when the given input is not a valid URI Template
according the RFC-defined grammar. The resultant ParseError
should give useful information about where the parser failed.
let template = Template::parse("my/valid/{template}");
assert!(template.is_ok());Examples found in repository?
9 10 11 12 13 14 15 16 17 18 19 20 21
fn main() -> Result<(), Box<dyn Error>> {
let template = Template::parse("/hello/{name}{/library*}")?;
let values = Values::default()
.add("name", Value::item("world"))
.add("library", Value::list(["uri", "template", "system"]));
assert_eq!(
template.expand(&values)?,
"/hello/world/uri/template/system"
);
Ok(())
}