summaryrefslogtreecommitdiffstats
path: root/crates/rebel/src/template.rs
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2024-04-20 14:28:05 +0200
committerMatthias Schiffer <mschiffer@universe-factory.net>2024-04-20 14:38:17 +0200
commite9bf0fc40c0eb7e9d4228b804d62f31b0a136528 (patch)
tree7872f587782d5635eadbf82ae861d474d4da2efe /crates/rebel/src/template.rs
parent35e68444dd5e9d3d5fc39409c48be6eb3fa05e07 (diff)
downloadrebel-e9bf0fc40c0eb7e9d4228b804d62f31b0a136528.tar
rebel-e9bf0fc40c0eb7e9d4228b804d62f31b0a136528.zip
Rename directories to match crate names
Diffstat (limited to 'crates/rebel/src/template.rs')
-rw-r--r--crates/rebel/src/template.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/crates/rebel/src/template.rs b/crates/rebel/src/template.rs
new file mode 100644
index 0000000..1a091ed
--- /dev/null
+++ b/crates/rebel/src/template.rs
@@ -0,0 +1,42 @@
+use handlebars::Handlebars;
+use lazy_static::lazy_static;
+
+use common::error::*;
+
+use crate::args::TaskArgs;
+
+fn escape_sh(s: &str) -> String {
+ format!("'{}'", s.replace('\'', "'\\''"))
+}
+
+#[derive(Debug)]
+pub struct TemplateEngine {
+ tpl: Handlebars<'static>,
+ tpl_sh: Handlebars<'static>,
+}
+
+impl TemplateEngine {
+ pub fn new() -> Self {
+ let mut tpl = Handlebars::new();
+ tpl.set_strict_mode(true);
+ tpl.register_escape_fn(handlebars::no_escape);
+
+ let mut tpl_sh = Handlebars::new();
+ tpl_sh.set_strict_mode(true);
+ tpl_sh.register_escape_fn(escape_sh);
+
+ TemplateEngine { tpl, tpl_sh }
+ }
+
+ pub fn eval(&self, input: &str, args: &TaskArgs) -> Result<String> {
+ self.tpl.render_template(input, args).map_err(Error::new)
+ }
+
+ pub fn eval_sh(&self, input: &str, args: &TaskArgs) -> Result<String> {
+ self.tpl_sh.render_template(input, args).map_err(Error::new)
+ }
+}
+
+lazy_static! {
+ pub static ref ENGINE: TemplateEngine = TemplateEngine::new();
+}