codegenr_lib/helpers/
equals.rs

1use super::handlebars_ext::HandlebarsExt;
2use handlebars::{HelperDef, Renderable};
3
4pub const IF_EQUALS_HELPER: &str = "if_equals";
5pub const IF_NOT_EQUALS_HELPER: &str = "if_not_equals";
6
7/// Execute template if the first argument is equal to any other argument, otherwise execute the inverse
8/// (all arguments are converted to string and case insensitive compared)
9/// ```
10/// # use codegenr_lib::helpers::*;
11/// # use serde_json::json;
12/// //assert_eq!(
13///   //exec_template(json!({}), r#"{{#if_equals "test" "teSt"}}OK{{else}}{{/if_equals}}"#),
14///   //"OK"
15/// //);
16/// assert_eq!(
17///   exec_template(json!({ "a": "42", "b": "42" }), r#"{{#if_equals a ./b }}OK{{else}}{{/if_equals}}"#),
18///   "OK"
19/// );
20/// assert_eq!(
21///   exec_template(json!({}), r#"{{#if_equals "test" "NO"}}OK{{else}}NOK{{/if_equals}}"#),
22///   "NOK"
23/// );
24/// assert_eq!(
25///   exec_template(json!({}), r#"{{#if_equals "test" "NO" "NO" "test"}}OK{{else}}NOK{{/if_equals}}"#),
26///   "OK"
27/// );
28/// assert_eq!(
29///   exec_template(json!({}), r#"{{#if_equals "test" "NO" "NOPE"}}OK{{else}}NOK{{/if_equals}}"#),
30///   "NOK"
31/// );
32/// ```
33pub struct IfEqualsHelper;
34
35impl HelperDef for IfEqualsHelper {
36  fn call<'reg: 'rc, 'rc>(
37    &self,
38    h: &handlebars::Helper<'reg, 'rc>,
39    handle: &'reg handlebars::Handlebars<'reg>,
40    ctx: &'rc handlebars::Context,
41    render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
42    out: &mut dyn handlebars::Output,
43  ) -> handlebars::HelperResult {
44    h.ensure_arguments_count_min(2, IF_EQUALS_HELPER)?;
45    let value = h.get_param_as_json_or_fail(0, IF_EQUALS_HELPER)?;
46
47    // todo: insensitive strings compare (when both strings)
48    let is_value_found = h.params().iter().skip(1).any(|p| p.value() == value);
49    let temp = if is_value_found { h.template() } else { h.inverse() };
50
51    match temp {
52      Some(t) => t.render(handle, ctx, render_ctx, out),
53      None => Ok(()),
54    }
55  }
56}
57
58/// Execute template if the first argument is not equal to all other arguments, otherwise execute the inverse
59/// (all arguments are converted to string and case insensitive compared)
60/// ```
61/// # use codegenr_lib::helpers::*;
62/// # use serde_json::json;
63/// //assert_eq!(
64///   //exec_template(json!({}), r#"{{#if_not_equals "test" "teSt"}}{{else}}NOK{{/if_not_equals}}"#),
65///   //"NOK"
66/// //);
67/// assert_eq!(
68///   exec_template(json!({ "a": "42", "b": "42" }), r#"{{#if_not_equals a ./b }}{{else}}NOK{{/if_not_equals}}"#),
69///   "NOK"
70/// );
71/// assert_eq!(
72///   exec_template(json!({}), r#"{{#if_not_equals "test" "NO"}}OK{{else}}NOK{{/if_not_equals}}"#),
73///   "OK"
74/// );
75/// assert_eq!(
76///   exec_template(json!({}), r#"{{#if_not_equals "test" "NO" "NO" "test"}}OK{{else}}NOK{{/if_not_equals}}"#),
77///   "NOK"
78/// );
79/// assert_eq!(
80///   exec_template(json!({}), r#"{{#if_not_equals "test" "NO" "NOPE"}}OK{{else}}NOK{{/if_not_equals}}"#),
81///   "OK"
82/// );
83/// ```
84pub struct IfNotEqualsHelper;
85
86impl HelperDef for IfNotEqualsHelper {
87  fn call<'reg: 'rc, 'rc>(
88    &self,
89    h: &handlebars::Helper<'reg, 'rc>,
90    handle: &'reg handlebars::Handlebars<'reg>,
91    ctx: &'rc handlebars::Context,
92    render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
93    out: &mut dyn handlebars::Output,
94  ) -> handlebars::HelperResult {
95    h.ensure_arguments_count_min(2, IF_NOT_EQUALS_HELPER)?;
96    let value = h.get_param_as_json_or_fail(0, IF_NOT_EQUALS_HELPER)?;
97
98    // todo: insensitive strings compare (when both strings)
99    let is_value_found = h.params().iter().skip(1).any(|p| p.value() == value);
100    let temp = if !is_value_found { h.template() } else { h.inverse() };
101
102    match temp {
103      Some(t) => t.render(handle, ctx, render_ctx, out),
104      None => Ok(()),
105    }
106  }
107}