codegenr_lib/helpers/
json.rs1use crate::helpers::handlebars_ext::HandlebarsExt;
2use handlebars::HelperDef;
3use serde_json::Value;
4
5pub const JSON_HELPER: &str = "json";
6
7pub struct JsonHelper;
30
31impl HelperDef for JsonHelper {
32 fn call_inner<'reg: 'rc, 'rc>(
33 &self,
34 h: &handlebars::Helper<'reg, 'rc>,
35 _: &'reg handlebars::Handlebars<'reg>,
36 _: &'rc handlebars::Context,
37 _: &mut handlebars::RenderContext<'reg, 'rc>,
38 ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
39 h.ensure_arguments_count_min(1, JSON_HELPER)?;
40 h.ensure_arguments_count_max(2, JSON_HELPER)?;
41 let arg = h.get_param_as_json_or_fail(0, JSON_HELPER)?;
42 let beautified = h.get_param_as_bool(1).unwrap_or_default();
43 let json = if beautified { format!("{:#}", arg) } else { format!("{}", arg) };
44 Ok(handlebars::ScopedJson::Derived(Value::String(json)))
45 }
46}