use handlebars::Handlebars;
use serde_json::Value;
use std::path::Path;
use std::fs;
use anyhow::{Result, anyhow};
pub fn render_template(template_path: &Path, variables: &Value) -> Result<String> {
if !template_path.exists() {
return Err(anyhow!("Template file not found: {}", template_path.display()));
}
let template_content = fs::read_to_string(template_path)?;
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
let rendered = handlebars.render_template(&template_content, variables)?;
Ok(rendered)
}
pub fn render_template_string(template_string: &str, variables: &Value) -> Result<String> {
let mut handlebars = Handlebars::new();
handlebars.register_escape_fn(handlebars::no_escape);
let rendered = handlebars.render_template(template_string, variables)?;
Ok(rendered)
}
pub fn register_helpers(handlebars: &mut Handlebars) {
handlebars.register_helper(
"lowercase",
Box::new(|h: &handlebars::Helper, _: &handlebars::Handlebars, _: &handlebars::Context, _: &mut handlebars::RenderContext, out: &mut dyn handlebars::Output| -> handlebars::HelperResult {
let param = h.param(0).ok_or_else(|| handlebars::RenderError::new("Missing parameter"))?;
if let Some(s) = param.value().as_str() {
out.write(&s.to_lowercase())?;
} else {
out.write(¶m.value().to_string().to_lowercase())?;
}
Ok(())
}),
);
handlebars.register_helper(
"uppercase",
Box::new(|h: &handlebars::Helper, _: &handlebars::Handlebars, _: &handlebars::Context, _: &mut handlebars::RenderContext, out: &mut dyn handlebars::Output| -> handlebars::HelperResult {
let param = h.param(0).ok_or_else(|| handlebars::RenderError::new("Missing parameter"))?;
if let Some(s) = param.value().as_str() {
out.write(&s.to_uppercase())?;
} else {
out.write(¶m.value().to_string().to_uppercase())?;
}
Ok(())
}),
);
handlebars.register_helper(
"pascal_case",
Box::new(|h: &handlebars::Helper, _: &handlebars::Handlebars, _: &handlebars::Context, _: &mut handlebars::RenderContext, out: &mut dyn handlebars::Output| -> handlebars::HelperResult {
let param = h.param(0).ok_or_else(|| handlebars::RenderError::new("Missing parameter"))?;
let value_str = if let Some(s) = param.value().as_str() {
s.to_string()
} else {
param.value().to_string()
};
let mut result = String::new();
let mut capitalize_next = true;
for c in value_str.chars() {
if c.is_alphanumeric() {
if capitalize_next {
result.push(c.to_ascii_uppercase());
capitalize_next = false;
} else {
result.push(c);
}
} else {
capitalize_next = true;
}
}
out.write(&result)?;
Ok(())
}),
);
handlebars.register_helper(
"snake_case",
Box::new(|h: &handlebars::Helper, _: &handlebars::Handlebars, _: &handlebars::Context, _: &mut handlebars::RenderContext, out: &mut dyn handlebars::Output| -> handlebars::HelperResult {
let param = h.param(0).ok_or_else(|| handlebars::RenderError::new("Missing parameter"))?;
let value_str = if let Some(s) = param.value().as_str() {
s.to_string()
} else {
param.value().to_string()
};
let mut result = String::new();
let mut last_was_underscore = false;
for c in value_str.chars() {
if c.is_alphanumeric() {
result.push(c.to_ascii_lowercase());
last_was_underscore = false;
} else if !last_was_underscore {
result.push('_');
last_was_underscore = true;
}
}
if result.ends_with('_') {
result.pop();
}
out.write(&result)?;
Ok(())
}),
);
}