codegenr_lib/helpers/empty.rs
1use super::handlebars_ext::HandlebarsExt;
2use super::string_ext::StringExt;
3use handlebars::{HelperDef, Renderable};
4use serde_json::Value;
5
6pub const IF_NOT_EMPTY_HELPER: &str = "if_not_empty";
7pub const IF_EMPTY_HELPER: &str = "if_empty";
8
9/// Call the template if a non empty or whitespaces string is passed as parameter, or any other non null value
10/// ```
11/// # use codegenr_lib::helpers::*;
12/// # use serde_json::json;
13/// assert_eq!(
14/// exec_template(json!({}), "{{#if_not_empty 42}}OK{{else}}NOK{{/if_not_empty}}"),
15/// "OK"
16/// );
17/// assert_eq!(
18/// exec_template(json!({}), "{{#if_not_empty \"42\"}}OK{{else}}NOK{{/if_not_empty}}"),
19/// "OK"
20/// );
21/// assert_eq!(
22/// exec_template(json!({}), "{{#if_not_empty \" \"}}OK{{else}}NOK{{/if_not_empty}}"),
23/// "NOK"
24/// );
25/// assert_eq!(
26/// exec_template(json!({}), "{{#if_not_empty not_existing}}OK{{else}}NOK{{/if_not_empty}}"),
27/// "NOK"
28/// );
29/// assert_eq!(
30/// exec_template(json!({"plop": "plop"}), "{{#if_not_empty plop}}OK{{else}}NOK{{/if_not_empty}}"),
31/// "OK"
32/// );
33/// assert_eq!(
34/// exec_template(json!({"plop": ""}), "{{#if_not_empty plop}}OK{{else}}NOK{{/if_not_empty}}"),
35/// "NOK"
36/// );
37/// assert_eq!(
38/// exec_template(json!({"plop": "plop"}), "{{#if_not_empty not_existing}}OK{{else}}NOK{{/if_not_empty}}"),
39/// "NOK"
40/// );
41/// ```
42pub struct IfNotEmptyHelper;
43
44impl HelperDef for IfNotEmptyHelper {
45 fn call<'reg: 'rc, 'rc>(
46 &self,
47 h: &handlebars::Helper<'reg, 'rc>,
48 handle: &'reg handlebars::Handlebars<'reg>,
49 ctx: &'rc handlebars::Context,
50 render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
51 out: &mut dyn handlebars::Output,
52 ) -> handlebars::HelperResult {
53 let param0 = h.get_param_as_json_or_fail(0, IF_NOT_EMPTY_HELPER)?;
54 let is_empty = is_json_empty(param0);
55 let temp = if !is_empty { h.template() } else { h.inverse() };
56 match temp {
57 Some(t) => t.render(handle, ctx, render_ctx, out),
58 None => Ok(()),
59 }
60 }
61}
62
63/// Call the template if an empty or whitespaces string is passed as parameter
64/// ```
65/// # use codegenr_lib::helpers::*;
66/// # use serde_json::json;
67/// assert_eq!(
68/// exec_template(json!({"a": 42}), "{{#if_empty a}}OK{{else}}NOK{{/if_empty}}"),
69/// "NOK"
70/// );
71/// assert_eq!(
72/// exec_template(json!({}), "{{#if_empty \"42\"}}OK{{else}}NOK{{/if_empty}}"),
73/// "NOK"
74/// );
75/// assert_eq!(
76/// exec_template(json!({}), "{{#if_empty \" \"}}OK{{else}}NOK{{/if_empty}}"),
77/// "OK"
78/// );
79/// assert_eq!(
80/// exec_template(json!({}), "{{#if_empty not_existing}}OK{{else}}NOK{{/if_empty}}"),
81/// "OK"
82/// );
83/// assert_eq!(
84/// exec_template(json!({"plop": "plop"}), "{{#if_empty plop}}OK{{else}}NOK{{/if_empty}}"),
85/// "NOK"
86/// );
87/// assert_eq!(
88/// exec_template(json!({"plop": ""}), "{{#if_empty plop}}OK{{else}}NOK{{/if_empty}}"),
89/// "OK"
90/// );
91/// assert_eq!(
92/// exec_template(json!({"plop": "plop"}), "{{#if_empty not_existing}}OK{{else}}NOK{{/if_empty}}"),
93/// "OK"
94/// );
95/// ```
96pub struct IfEmptyHelper;
97
98impl HelperDef for IfEmptyHelper {
99 fn call<'reg: 'rc, 'rc>(
100 &self,
101 h: &handlebars::Helper<'reg, 'rc>,
102 handle: &'reg handlebars::Handlebars<'reg>,
103 ctx: &'rc handlebars::Context,
104 render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
105 out: &mut dyn handlebars::Output,
106 ) -> handlebars::HelperResult {
107 let param0 = h.get_param_as_json_or_fail(0, IF_EMPTY_HELPER)?;
108 let is_empty = is_json_empty(param0);
109 let temp = if is_empty { h.template() } else { h.inverse() };
110 match temp {
111 Some(t) => t.render(handle, ctx, render_ctx, out),
112 None => Ok(()),
113 }
114 }
115}
116
117fn is_json_empty(param0: &Value) -> bool {
118 match param0 {
119 Value::Null => true,
120 Value::String(s) => s.is_empty_or_whitespaces(),
121 _ => false,
122 }
123}