Skip to content

Commit

Permalink
split stateless & statefull helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouaix committed Feb 2, 2022
1 parent dcb7c6f commit e133c71
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
5 changes: 3 additions & 2 deletions codegenr/examples/handlebars_conf.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use codegenr_lib::helpers::handlebars_setup;
use codegenr_lib::helpers::*;

fn main() {
let mut h = handlebars::Handlebars::new();
println!("===================================================================");
println!("Default handlebars configuration :\n{:#?}", h);

handlebars_setup(&mut h, Default::default());
handlebars_stateless_setup(&mut h);
handlebars_statefull_setup(&mut h, Default::default());
println!("===================================================================");
println!("codegenr handlebars configuration :\n{:#?}", h);
}
19 changes: 12 additions & 7 deletions codegenr/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ pub enum HelpersError {
Regex(#[from] ::regex::Error),
}

pub fn handlebars_setup(handlebars: &mut Handlebars, global_params: HashMap<String, Value>) {
pub fn handlebars_stateless_setup(handlebars: &mut Handlebars) {
#[cfg(debug_assertions)]
{
handlebars.set_dev_mode(true);
}
handlebars.register_escape_fn(handlebars::no_escape);
handlebars.register_helper(DEBUG, Box::new(DebugHelper));
handlebars.register_helper(DEBUG_CTX, Box::new(DebugCtxHelper));
handlebars.register_helper(DISTINCTIVE, Box::new(DistinctiveHelper::default()));
handlebars.register_helper(IF_EMPTY_HELPER, Box::new(IfEmptyHelper));
handlebars.register_helper(IF_NOT_EMPTY_HELPER, Box::new(IfNotEmptyHelper));
handlebars.register_helper(IF_EQUALS_HELPER, Box::new(IfEqualsHelper));
Expand All @@ -59,29 +58,35 @@ pub fn handlebars_setup(handlebars: &mut Handlebars, global_params: HashMap<Stri
handlebars.register_helper(REGEX_TRANSFORM_HELPER, Box::new(RegexTransformHelper));
//handlebars.register_helper(EACH_WITH_SORT_HELPER, Box::new(EachWithSortHelper));

handlebars.register_helper(IS_OAPI3_PARAM_REQUIRED, Box::new(IsOApi3ParamRequiredHelper));
handlebars.register_helper(IS_OAPI3_PROP_REQUIRED, Box::new(IsOApi3PropRequiredHelper));
}

pub fn handlebars_statefull_setup(handlebars: &mut Handlebars, global_params: HashMap<String, Value>) {
handlebars.register_helper(DISTINCTIVE, Box::new(DistinctiveHelper::default()));

let map = Default::default();
handlebars.register_helper(GET_HELPER, Box::new(GetHelper::new(&map)));
handlebars.register_helper(SET_HELPER, Box::new(SetHelper::new(&map)));
handlebars.register_helper(WITH_SET_HELPER, Box::new(WithSetHelper::new(&map)));
handlebars.register_helper(IF_SET_HELPER, Box::new(IfSetHelper::new(&map)));
handlebars.register_helper(CLEAR_HELPER, Box::new(ClearHelper::new(&map)));

handlebars.register_helper(IS_OAPI3_PARAM_REQUIRED, Box::new(IsOApi3ParamRequiredHelper));
handlebars.register_helper(IS_OAPI3_PROP_REQUIRED, Box::new(IsOApi3PropRequiredHelper));

handlebars.register_helper(GLOBAL_PARAMETERS_HELPER, Box::new(GlobalParameterHelper::new(global_params)));
}

pub fn exec_template(json: serde_json::Value, template: &str) -> String {
let mut h = Handlebars::new();
handlebars_setup(&mut h, Default::default());
handlebars_stateless_setup(&mut h);
handlebars_statefull_setup(&mut h, Default::default());
h.register_template_string("test", template).expect("Could not register template.");
h.render("test", &json).expect("Template render returned an error.")
}

pub fn exec_template_with_global_params(json: serde_json::Value, template: &str, global_params: HashMap<String, Value>) -> String {
let mut h = Handlebars::new();
handlebars_setup(&mut h, global_params);
handlebars_stateless_setup(&mut h);
handlebars_statefull_setup(&mut h, global_params);
h.register_template_string("test", template).expect("Could not register template.");
h.render("test", &json).expect("Template render returned an error.")
}
5 changes: 3 additions & 2 deletions codegenr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,15 @@ fn run_codegenr(
let templates = render::TemplateCollection::from_list(all_templates).unwrap(); //?;

let mut handlebars = Handlebars::new();
helpers::handlebars_stateless_setup(&mut handlebars);

templates.setup_handlebars(&mut handlebars).unwrap(); // todo?;
custom::handlebars_setup(&mut handlebars, &conf.custom_helpers).unwrap(); //?;
(templates.main_template_name().to_owned(), handlebars)
})
.clone();

helpers::handlebars_setup(&mut handlebars, options.global_parameters);

helpers::handlebars_statefull_setup(&mut handlebars, options.global_parameters);
handlebars_misc_helpers::register(&mut handlebars);

let rendered = handlebars.render(&main_template_name, &(*json))?;
Expand Down
5 changes: 3 additions & 2 deletions codegenr/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,15 @@ pub fn get_templates_from_directory(dir_path: &str) -> Result<Vec<Template>, Ren
#[cfg(test)]
mod test {
use super::*;
use crate::helpers::handlebars_setup;
use crate::helpers::*;

#[test]
fn handlebars_loading_test() -> Result<(), anyhow::Error> {
let list = get_templates_from_directory("_samples/render/test_denis")?;
let collection = TemplateCollection::from_list(list)?;
let mut h = Handlebars::new();
handlebars_setup(&mut h, Default::default());
handlebars_stateless_setup(&mut h);
handlebars_statefull_setup(&mut h, Default::default());
let result = collection.setup_handlebars(&mut h)?;
dbg!(result);
Ok(())
Expand Down

0 comments on commit e133c71

Please sign in to comment.