extern crate regex;
use regex::Regex;
use std::collections::{HashMap, HashSet};
#[derive(Debug, PartialEq)]
pub struct Template {
pub src: String,
pub matches: Vec<(usize, usize)>,
pub names: HashSet<String>,
}
impl Template {
pub fn new(template: &str) -> Self {
let regex = Regex::new(r"\{\{\s*([^}\s]*)\s*\}\}").unwrap();
Template {
src: template.to_owned(),
matches: regex
.find_iter(template)
.map(|m| (m.start(), m.end()))
.collect(),
names: regex
.captures_iter(template)
.map(|cap| cap[1].to_string())
.collect(),
}
}
pub fn render(&self, vals: &HashMap<String, String>) -> String {
let mut parts: Vec<&str> = vec![];
let template_str = &self.src;
let first = match self.matches.first() {
Some((start, _)) => *start,
_ => return template_str.clone(),
};
if first > 0 {
parts.push(&template_str[0..first])
}
let mut prev_end: Option<usize> = None;
for (start, end) in self.matches.iter() {
if let Some(last_end) = prev_end {
parts.push(&template_str[last_end..*start])
}
let arg = &template_str[*start..*end];
let arg_name = arg[2..arg.len() - 2].trim();
match vals.get(arg_name) {
Some(s) => parts.push(s),
_ => parts.push(arg),
}
prev_end = Some(*end);
}
let template_len = template_str.len();
if let Some(last_pos) = prev_end {
if last_pos < template_len {
parts.push(&template_str[last_pos..template_len])
}
}
parts.join("")
}
}