pub struct VarjMap { /* private fields */ }Expand description
A map of variables to replace placeholders in a string.
Implementations§
Source§impl VarjMap
impl VarjMap
Sourcepub fn new() -> Self
pub fn new() -> Self
Create an empty VarjMap.
Examples found in repository?
More 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}Sourcepub fn with_capacity(capacity: usize) -> Self
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.
Sourcepub fn insert<K, V>(&mut self, key: K, value: V)
pub fn insert<K, V>(&mut self, key: K, value: V)
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?
More 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}Sourcepub fn render(&self, template: &str) -> Result<String, Error>
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?
More 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}pub fn parse(&self, template: &str) -> Result<String, Error>
👎Deprecated since 1.1.0: please use
render insteadTrait Implementations§
impl Eq for VarjMap
impl StructuralPartialEq for VarjMap
Auto Trait Implementations§
impl Freeze for VarjMap
impl RefUnwindSafe for VarjMap
impl Send for VarjMap
impl Sync for VarjMap
impl Unpin for VarjMap
impl UnwindSafe for VarjMap
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more