Skip to content

Commit

Permalink
some more string helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
jrouaix committed Oct 12, 2023
1 parent bb8de92 commit adedf79
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
4 changes: 3 additions & 1 deletion codegenr/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ pub fn handlebars_stateless_setup(handlebars: &mut Handlebars) {
handlebars.register_helper(LOWERCASE_FIRST_LETTER_HELPER, Box::new(LowercaseFirstLetterHelper));
handlebars.register_helper(SPLIT_HELPER, Box::new(SplitHelper));
handlebars.register_helper(START_WITH_HELPER, Box::new(StartWithHelper));
handlebars.register_helper(END_WITH_HELPER, Box::new(EndWithHelper));
handlebars.register_helper(CONTAINS_HELPER, Box::new(ContainsHelper));
handlebars.register_helper(WITH_MATCHING_HELPER, Box::new(WithMatchingHelper));
handlebars.register_helper(TRIM_BLOCK_HELPER, Box::new(TrimBlockHelper));
handlebars.register_helper(TRIM_BLOCK_START_HELPER, Box::new(TrimBlockStartHelper));
Expand All @@ -58,7 +60,7 @@ pub fn handlebars_stateless_setup(handlebars: &mut Handlebars) {
}

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

let map = Default::default();
handlebars.register_helper(GET_HELPER, Box::new(GetHelper::new(&map)));
Expand Down
83 changes: 80 additions & 3 deletions codegenr/src/helpers/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ pub const TRIM_CHAR_HELPER: &str = "trim_char";
pub const TRIM_CHAR_START_HELPER: &str = "trim_char_start";
pub const TRIM_CHAR_END_HELPER: &str = "trim_char_end";
pub const START_WITH_HELPER: &str = "start_with";
pub const END_WITH_HELPER: &str = "end_with";
pub const CONTAINS_HELPER: &str = "contains";
pub const WITH_MATCHING_HELPER: &str = "with_matching";
pub const EACH_WITH_SORT_HELPER: &str = "each_with_sort";
pub const TRIM_BLOCK_HELPER: &str = "trim_block";
Expand Down Expand Up @@ -85,8 +87,7 @@ impl HelperDef for SplitHelper {
let splitter = h
.get_param_as_str(1)
.map(|s| s.to_string())
.map(|s| s.chars().next())
.flatten()
.and_then(|s| s.chars().next())
.unwrap_or('/');

let values = to_split
Expand Down Expand Up @@ -164,7 +165,7 @@ impl HelperDef for TrimCharEndHelper {
}
}

/// Determines whether the beginning of the second argumentmatches the second one
/// Determines whether the beginning of the first argument matches the first one
///```
/// # use codegenr_lib::helpers::*;
/// # use serde_json::json;
Expand Down Expand Up @@ -200,6 +201,82 @@ impl HelperDef for StartWithHelper {
}
}

/// Determines whether the ending of the second argument matches the first one
///```
/// # use codegenr_lib::helpers::*;
/// # use serde_json::json;
/// assert_eq!(
/// exec_template(json!({"one": "test-one", "two": "one-test"}), r#"{{#end_with "test" one}}OK{{else}}NOK{{/end_with}}"#),
/// "NOK"
/// );
/// assert_eq!(
/// exec_template(json!({"one": "test-one", "two": "one-test"}), r#"{{#end_with "test" two}}OK{{else}}NOK{{/end_with}}"#),
/// "OK"
/// );
///```
pub struct EndWithHelper;

impl HelperDef for EndWithHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &handlebars::Helper<'reg, 'rc>,
handle: &'reg handlebars::Handlebars<'reg>,
ctx: &'rc handlebars::Context,
render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
h.ensure_arguments_count(2, END_WITH_HELPER)?;
let end = h.get_param_as_str_or_fail(0, END_WITH_HELPER)?;
let with = h.get_param_as_str_or_fail(1, END_WITH_HELPER)?;

let temp = if with.ends_with(end) { h.template() } else { h.inverse() };
if let Some(t) = temp {
t.render(handle, ctx, render_ctx, out)?
};
Ok(())
}
}

/// Determines whether the second parameter contains the first one
///```
/// # use codegenr_lib::helpers::*;
/// # use serde_json::json;
/// assert_eq!(
/// exec_template(json!({"one": "test-one", "two": "one-test"}), r#"{{#contains "test" one}}OK{{else}}{{/contains}}"#),
/// "OK"
/// );
/// assert_eq!(
/// exec_template(json!({"one": "test-one", "two": "one-test"}), r#"{{#contains "test" two}}OK{{else}}NOK{{/contains}}"#),
/// "OK"
/// );
/// assert_eq!(
/// exec_template(json!({"one": "test-one", "two": "one-test"}), r#"{{#contains "plop" one}}OK{{else}}NOK{{/contains}}"#),
/// "NOK"
/// );
///```
pub struct ContainsHelper;

impl HelperDef for ContainsHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &handlebars::Helper<'reg, 'rc>,
handle: &'reg handlebars::Handlebars<'reg>,
ctx: &'rc handlebars::Context,
render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
h.ensure_arguments_count(2, END_WITH_HELPER)?;
let start = h.get_param_as_str_or_fail(0, END_WITH_HELPER)?;
let with = h.get_param_as_str_or_fail(1, END_WITH_HELPER)?;

let temp = if with.contains(start) { h.template() } else { h.inverse() };
if let Some(t) = temp {
t.render(handle, ctx, render_ctx, out)?
};
Ok(())
}
}

/// Execute the inner template with the matching parameter, when matching key is equal to the first parameter
/// {{#with_matching some_value matching_key1 context1 mateching_key2 context2 ... }}
/// Render the inverse template if no matching key was found
Expand Down

0 comments on commit adedf79

Please sign in to comment.