teaql_tool_extra/
template.rs1use tera::{Tera, Context};
2use teaql_tool_core::{Result, TeaQLToolError};
3
4#[derive(Debug, Clone)]
5pub struct TemplateTool;
6
7impl TemplateTool {
8 pub fn new() -> Self { Self }
9
10 pub fn render(&self, template: &str, ctx_json: &str) -> Result<String> {
11 let mut context = Context::new();
12 let value: serde_json::Value = serde_json::from_str(ctx_json).map_err(|e| TeaQLToolError::ParseError(e.to_string()))?;
13 if let serde_json::Value::Object(map) = value {
14 for (k, v) in map {
15 context.insert(k, &v);
16 }
17 }
18 Tera::one_off(template, &context, true).map_err(|e| TeaQLToolError::ExecutionError(e.to_string()))
19 }
20}
21
22impl Default for TemplateTool {
23 fn default() -> Self { Self::new() }
24}