summaryrefslogtreecommitdiffstats
path: root/src/template.rs
blob: 035e09adca996861c4276ff41e19e5a6e795b6c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
use handlebars::Handlebars;

use crate::{task::*, util::error::*};

fn escape(s: &str) -> String {
	format!("'{}'", s.replace("'", "'\\''"))
}

#[derive(Debug)]
pub struct TemplateEngine(Handlebars<'static>);

impl TemplateEngine {
	pub fn new() -> Self {
		let mut tpl = Handlebars::new();

		tpl.set_strict_mode(true);
		tpl.register_escape_fn(escape);

		TemplateEngine(tpl)
	}

	pub fn eval(&self, task: &TaskDef, args: &TaskArgs) -> Result<String> {
		self.0
			.render_template(&task.action.run, args)
			.map_err(Error::new)
	}
}