Enum Value

Source
pub enum Value {
    AssociativeArray(Vec<(String, String)>),
    Item(String),
    List(Vec<String>),
    Undefined,
}
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.

§

Undefined

The Value::Undefined variant allows for input data to be explicitly given as undefined. This is sometimes useful rather than simply omitting the data.

Implementations§

Source§

impl Value

Source

pub fn associative_array<T, U, V>(value: T) -> Self
where 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) -> Self
where 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)
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 list<T, U>(value: T) -> Self
where 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)
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 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 for Value

Source§

fn eq(&self, other: &Value) -> 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 Eq for Value

Source§

impl StructuralPartialEq for Value

Auto Trait Implementations§

§

impl Freeze for Value

§

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 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

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