pub enum Value {
    AssociativeArray(Vec<(String, String)>),
    Item(String),
    List(Vec<String>),
}
Expand description

The Value type is used as the source of content during template expansion, as part of a Values collection. It maps to the three valid shapes of data defined by the RFC (a single item, a list of items, or a list of key/value pairs).

All values are of type String for simplicity of ownership, etc.

Variants§

§

AssociativeArray(Vec<(String, String)>)

The Value::AssociativeArray variant allows for input data to be treated as a logical map (with implicit ordering), complying with the requirements for Level 4 templates defined in RFC6570 2.3.

§

Item(String)

The Value::Item variant allows for simple input data, complying with the requirements for sub-Level 4 templates defined in RFC6570 2.3.

§

List(Vec<String>)

The Value::List variant allows for input data to be treated as a logical list (with implicit ordering), complying with the requirements for Level 4 templates defined in RFC6570 2.3.

Implementations§

source§

impl Value

source

pub fn associative_array<T, U, V>(value: T) -> Selfwhere T: IntoIterator<Item = (U, V)>, U: Into<String>, V: Into<String>,

Constructs a new Value from any iterator which produces pairs (tuples) where both items implement Into<String>. This may be a simple array or vec, or a more complex type such as an IndexMap.

let expected = Value::AssociativeArray(Vec::from_iter([
    (String::from("a"), String::from("1")),
    (String::from("b"), String::from("2")),
]));

let array = [("a", "1"), ("b", "2")];
assert_eq!(expected, Value::associative_array(array));

let vec = Vec::from_iter(array);
assert_eq!(expected, Value::associative_array(vec));
source

pub fn item<T>(value: T) -> Selfwhere T: Into<String>,

Constructs a new Value from any type which implements Into<String>.

let expected = Value::Item(String::from("a"));

let str = "a";
assert_eq!(expected, Value::item(str));

let string = String::from(str);
assert_eq!(expected, Value::item(string));
Examples found in repository?
examples/simple.rs (line 12)
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(())
}
source

pub fn list<T, U>(value: T) -> Selfwhere T: IntoIterator<Item = U>, U: Into<String>,

Constructs a new Value from any iterator which produces items which implement Into<String>, such as arrays, vecs, etc.

let expected = Value::List(Vec::from_iter([String::from("a"), String::from("b")]));

let array = ["a", "b"];
assert_eq!(expected, Value::list(array));

let vec = Vec::from_iter(array);
assert_eq!(expected, Value::list(vec));
Examples found in repository?
examples/simple.rs (line 13)
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(())
}

Trait Implementations§

source§

impl Clone for Value

source§

fn clone(&self) -> Value

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Value

source§

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

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

impl PartialEq<Value> for Value

source§

fn eq(&self, other: &Value) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Value

source§

impl StructuralEq for Value

source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl RefUnwindSafe for Value

§

impl Send for Value

§

impl Sync for Value

§

impl Unpin for Value

§

impl UnwindSafe for Value

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

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 Twhere U: TryFrom<T>,

§

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.