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?
9fn main() -> Result<(), Box<dyn Error>> {
10 let template = Template::parse("/hello/{name}{/library*}")?;
11 let values = Values::default()
12 .add("name", Value::item("world"))
13 .add("library", Value::list(["uri", "template", "system"]));
14
15 assert_eq!(
16 template.expand(&values)?,
17 "/hello/world/uri/template/system"
18 );
19
20 Ok(())
21}
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?
9fn main() -> Result<(), Box<dyn Error>> {
10 let template = Template::parse("/hello/{name}{/library*}")?;
11 let values = Values::default()
12 .add("name", Value::item("world"))
13 .add("library", Value::list(["uri", "template", "system"]));
14
15 assert_eq!(
16 template.expand(&values)?,
17 "/hello/world/uri/template/system"
18 );
19
20 Ok(())
21}