Struct 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>

Source

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?
examples/simple.rs (line 16)
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}
Source

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?
examples/simple.rs (line 10)
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}

Trait Implementations§

Source§

impl<'t> Debug for Template<'t>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'t> PartialEq for Template<'t>

Source§

fn eq(&self, other: &Template<'t>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'t> Eq for Template<'t>

Source§

impl<'t> StructuralPartialEq for Template<'t>

Auto Trait Implementations§

§

impl<'t> Freeze for Template<'t>

§

impl<'t> RefUnwindSafe for Template<'t>

§

impl<'t> Send for Template<'t>

§

impl<'t> Sync for Template<'t>

§

impl<'t> Unpin for Template<'t>

§

impl<'t> UnwindSafe for Template<'t>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.