use std::collections::HashMap;
#[derive(Debug)]
pub struct SourceTemplate {
items: HashMap<String, String>,
templates: Vec<String>,
}
impl SourceTemplate {
pub fn new<S>(template: S) -> Self
where
S: Into<String>,
{
Self {
items: HashMap::new(),
templates: vec![template.into()],
}
}
pub fn register<Name, Value>(mut self, name: Name, value: Value) -> Self
where
Name: Into<String>,
Value: Into<String>,
{
self.items.insert(name.into(), value.into());
self
}
pub fn add_template<S>(mut self, template: S) -> Self
where
S: Into<String>,
{
self.templates.push(template.into());
self
}
pub fn complete(mut self) -> String {
let mut source = self.templates.remove(0);
for s in self.templates.into_iter() {
source.push_str(&s);
}
let template = text_placeholder::Template::new(&source);
let mut context = HashMap::new();
for (key, value) in self.items.iter() {
context.insert(key.as_str(), value.as_str());
}
template.fill_with_hashmap(&context)
}
}