diff --git a/src/template.rs b/src/template.rs index 9e14cd1..947d35a 100644 --- a/src/template.rs +++ b/src/template.rs @@ -24,8 +24,56 @@ pub fn populate_env( // === Custom functions === env.add_function("vec", crate::cli_v2::value_vec::new_value_vec); + env.add_function( + // allows overriding the summary filename + "set_summary_filename", + |state: &State, filename: Cow<'_, str>| { + state.set_temp("summary_filename", filename.into()); + }, + ); + + env.add_function( + // For java lib we need to create extra files. + "generate_extra_file", + |state: &State, filename: Cow<'_, str>, file_contents: Cow<'_, str>| { + fs::write(&*filename, file_contents.as_bytes()).unwrap(); + state.set_temp("extra_generated_file", filename.into()); + }, + ); + + env.add_function( + "panic", + |message: Cow<'_, str>| -> Result { + Err(minijinja::Error::new( + minijinja::ErrorKind::InvalidOperation, + message.to_string(), + )) + }, + ); + // === Custom filters === + // --- String utilities --- + env.add_filter("strip_trailing_comma", |s: Cow<'_, str>| { + match s.trim_end().strip_suffix(",") { + Some(stripped) => stripped.to_string(), + None => s.into_owned(), + } + }); + env.add_filter( + "strip_trailing_str", + |s: Cow<'_, str>, trailing_str: Cow<'_, str>| match s + .trim_end() + .strip_suffix(&trailing_str.to_string()) + { + Some(stripped) => stripped.to_string(), + None => s.into_owned(), + }, + ); + env.add_filter("remove_suffix", |s: Cow<'_, str>, suffix: Cow<'_, str>| { + Value::from(s.strip_suffix(&*suffix)) + }); + // --- Case conversion --- env.add_filter("to_upper_snake_case", |s: Cow<'_, str>| { s.to_shouty_snake_case() @@ -132,22 +180,6 @@ pub fn populate_env( ); // --- Miscellaneous --- - env.add_filter("strip_trailing_comma", |s: Cow<'_, str>| { - match s.trim_end().strip_suffix(",") { - Some(stripped) => stripped.to_string(), - None => s.into_owned(), - } - }); - env.add_filter( - "strip_trailing_str", - |s: Cow<'_, str>, trailing_str: Cow<'_, str>| match s - .trim_end() - .strip_suffix(&trailing_str.to_string()) - { - Some(stripped) => stripped.to_string(), - None => s.into_owned(), - }, - ); env.add_filter( "generate_kt_path_str", |s: Cow<'_, str>, path_params: &Vec| -> Result { @@ -202,23 +234,6 @@ pub fn populate_env( }, ); - env.add_function( - // allows overriding the summary filename - "set_summary_filename", - |state: &State, filename: Cow<'_, str>| { - state.set_temp("summary_filename", filename.into()); - }, - ); - - env.add_function( - // For java lib we need to create extra files. - "generate_extra_file", - |state: &State, filename: Cow<'_, str>, file_contents: Cow<'_, str>| { - fs::write(&*filename, file_contents.as_bytes()).unwrap(); - state.set_temp("extra_generated_file", filename.into()); - }, - ); - env.add_filter( "struct_enum_ref_variants", |variants: Vec| -> Result, minijinja::Error> { @@ -255,16 +270,6 @@ pub fn populate_env( }, ); - env.add_function( - "panic", - |message: Cow<'_, str>| -> Result { - Err(minijinja::Error::new( - minijinja::ErrorKind::InvalidOperation, - message.to_string(), - )) - }, - ); - env.add_filter( // TODO: fix this ugly ass code :( // TLDR: If I have a number serde_json::Value, it does not get passed the the template correctly