VarjMap

Struct VarjMap 

Source
pub struct VarjMap { /* private fields */ }
Expand description

A map of variables to replace placeholders in a string.

Implementations§

Source§

impl VarjMap

Source

pub fn new() -> Self

Create an empty VarjMap.

Examples found in repository?
examples/basic.rs (line 5)
4fn main() -> Result<(), Box<dyn Error>> {
5    let mut map = VarjMap::new();
6    map.insert("key", "value");
7
8    let expected = "value";
9    let actual = map.render("{{ key }}")?;
10
11    assert_eq!(expected, actual);
12    Ok(())
13}
More examples
Hide additional examples
examples/json.rs (line 5)
4fn main() -> Result<(), Box<dyn Error>> {
5    let mut variables = VarjMap::new();
6
7    variables.insert("name", "Christopher");
8    variables.insert("age", "30");
9
10    let json = r#"{
11    "name" = "{{ name }}",
12    "age" = {{ age }}
13}"#;
14
15    let expected = r#"{
16    "name" = "Christopher",
17    "age" = 30
18}"#;
19
20    let actual = variables.render(json)?;
21
22    assert_eq!(expected, actual);
23    Ok(())
24}
Source

pub fn with_capacity(capacity: usize) -> Self

Creates an empty VarjMap with the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash map will not allocate.

Source

pub fn insert<K, V>(&mut self, key: K, value: V)
where K: Into<String>, V: Into<String>,

Insert a key value pair into the VarjMap.

Use any type so long as it can be converted into a string.

Examples found in repository?
examples/basic.rs (line 6)
4fn main() -> Result<(), Box<dyn Error>> {
5    let mut map = VarjMap::new();
6    map.insert("key", "value");
7
8    let expected = "value";
9    let actual = map.render("{{ key }}")?;
10
11    assert_eq!(expected, actual);
12    Ok(())
13}
More examples
Hide additional examples
examples/json.rs (line 7)
4fn main() -> Result<(), Box<dyn Error>> {
5    let mut variables = VarjMap::new();
6
7    variables.insert("name", "Christopher");
8    variables.insert("age", "30");
9
10    let json = r#"{
11    "name" = "{{ name }}",
12    "age" = {{ age }}
13}"#;
14
15    let expected = r#"{
16    "name" = "Christopher",
17    "age" = 30
18}"#;
19
20    let actual = variables.render(json)?;
21
22    assert_eq!(expected, actual);
23    Ok(())
24}
Source

pub fn get<K: AsRef<str>>(&self, key: K) -> Option<&str>

Get a value from the VarjMap by key.

Source

pub fn render(&self, template: &str) -> Result<String, Error>

Render a template with its placeholder blocks replaced by set values.

If no placeholder blocks({{ key }}) are present in the template, returns a cloned String.

Whitespace surrounding the key is ignored: {{key}} and {{ key }} are equal.

§Errors

Will return an Error if the template contains a key that is not set.

§Example
let mut map = varj::VarjMap::new();

// add variables to VarjMap
let key = "name";
let value = "Christopher";
map.insert(key, value);

// template to render
let template = "name: {{name}}";

// test result
let expected = "name: Christopher";
let actual = map.render(template)?;

assert_eq!(expected, actual);
Examples found in repository?
examples/basic.rs (line 9)
4fn main() -> Result<(), Box<dyn Error>> {
5    let mut map = VarjMap::new();
6    map.insert("key", "value");
7
8    let expected = "value";
9    let actual = map.render("{{ key }}")?;
10
11    assert_eq!(expected, actual);
12    Ok(())
13}
More examples
Hide additional examples
examples/json.rs (line 20)
4fn main() -> Result<(), Box<dyn Error>> {
5    let mut variables = VarjMap::new();
6
7    variables.insert("name", "Christopher");
8    variables.insert("age", "30");
9
10    let json = r#"{
11    "name" = "{{ name }}",
12    "age" = {{ age }}
13}"#;
14
15    let expected = r#"{
16    "name" = "Christopher",
17    "age" = 30
18}"#;
19
20    let actual = variables.render(json)?;
21
22    assert_eq!(expected, actual);
23    Ok(())
24}
examples/conversion.rs (line 19)
4fn main() -> Result<(), Box<dyn Error>> {
5    // create a vector of string pairs
6    let pairs = vec![
7        ("key1".to_owned(), "value1".to_owned()),
8        ("key2".to_owned(), "value2".to_owned()),
9    ];
10
11    // create a HashMap by iterating over a vector or string pairs
12    let hash_map: HashMap<String, String> = pairs.into_iter().collect();
13
14    // convert the HashMap into a VarjMap
15    let map = VarjMap::from(hash_map);
16
17    // use it to render a template
18    let expected = "value1";
19    let actual = map.render("{{ key1 }}")?;
20    assert_eq!(expected, actual);
21
22    // convert it back into a HashMap
23    let hash_map: HashMap<String, String> = map.into();
24    let expected = "value2";
25    let actual = hash_map.get("key2").unwrap();
26    assert_eq!(expected, actual);
27
28    Ok(())
29}
Source

pub fn parse(&self, template: &str) -> Result<String, Error>

👎Deprecated since 1.1.0: please use render instead

Trait Implementations§

Source§

impl Clone for VarjMap

Source§

fn clone(&self) -> VarjMap

Returns a duplicate 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 VarjMap

Source§

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

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

impl Default for VarjMap

Source§

fn default() -> VarjMap

Returns the “default value” for a type. Read more
Source§

impl From<HashMap<String, String>> for VarjMap

Source§

fn from(map: HashMap<String, String>) -> Self

Converts to this type from the input type.
Source§

impl From<VarjMap> for HashMap<String, String>

Source§

fn from(map: VarjMap) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for VarjMap

Source§

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

Source§

impl StructuralPartialEq for VarjMap

Auto Trait Implementations§

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.