requestr_core/template.rs
1extern crate regex;
2
3use regex::Regex;
4use std::collections::{HashMap, HashSet};
5
6#[derive(Debug, PartialEq)]
7pub struct Template {
8 pub src: String,
9 pub matches: Vec<(usize, usize)>,
10 pub names: HashSet<String>,
11}
12
13impl Template {
14 pub fn new(template: &str) -> Self {
15 let regex = Regex::new(r"\{\{\s*([^}\s]*)\s*\}\}").unwrap();
16
17 Template {
18 src: template.to_owned(),
19 matches: regex
20 .find_iter(template)
21 .map(|m| (m.start(), m.end()))
22 .collect(),
23 names: regex
24 .captures_iter(template)
25 .map(|cap| cap[1].to_string())
26 // .into_iter()
27 .collect(),
28 }
29 }
30
31 /// # Examples
32 /// ```
33 /// use std::collections::HashMap;
34 /// use requestr_core::Template;
35 ///
36 /// let template = Template::new("Hi, my name is {{ name }} and I'm a {{ lang }} developer.");
37 ///
38 /// let mut args: HashMap<String, String> = HashMap::new();
39 /// args.insert("name".to_string(), "Stefan".to_string());
40 /// args.insert("lang".to_string(), "Scala".to_string());
41 /// let s = template.render(&args);
42 ///
43 /// assert_eq!(s, "Hi, my name is Stefan and I'm a Scala developer.");
44 /// ```
45 ///
46 /// ```
47 /// use std::collections::HashMap;
48 /// use requestr_core::Template;
49 ///
50 /// let template = Template::new("Hi, my name is {{ name }} and I'm a {{lang}} developer.");
51
52 /// let mut args: HashMap<String, String> = HashMap::new();
53 /// args.insert("name".to_string(), "undefined".to_string());
54 /// args.insert("lang".to_string(), "JavaScript".to_string());
55 /// let s = template.render(&args);
56 ///
57 /// assert_eq!(s, "Hi, my name is undefined and I'm a JavaScript developer.");
58 /// ```
59 pub fn render(&self, vals: &HashMap<String, String>) -> String {
60 let mut parts: Vec<&str> = vec![];
61 let template_str = &self.src;
62
63 // get index of first arg match or return a copy of the template if no args matched
64 let first = match self.matches.first() {
65 Some((start, _)) => *start,
66 _ => return template_str.clone(),
67 };
68
69 // copy from template start to first arg
70 if first > 0 {
71 parts.push(&template_str[0..first])
72 }
73
74 // keeps the index of the previous argument end
75 let mut prev_end: Option<usize> = None;
76
77 for (start, end) in self.matches.iter() {
78 // copy from previous argument end till current argument start
79 if let Some(last_end) = prev_end {
80 parts.push(&template_str[last_end..*start])
81 }
82
83 // argument name with braces
84 let arg = &template_str[*start..*end];
85 // just the argument name
86 let arg_name = arg[2..arg.len() - 2].trim();
87
88 // if value passed for argument then append it, otherwise append original argument
89 // name with braces
90 match vals.get(arg_name) {
91 Some(s) => parts.push(s),
92 _ => parts.push(arg),
93 }
94
95 prev_end = Some(*end);
96 }
97
98 let template_len = template_str.len();
99 // if last arg end index isn't the end of the string then copy
100 // from last arg end till end of template string
101 if let Some(last_pos) = prev_end {
102 if last_pos < template_len {
103 parts.push(&template_str[last_pos..template_len])
104 }
105 }
106
107 parts.join("")
108 }
109}