codegenr_lib/helpers/
cases.rs

1use crate::helpers::handlebars_ext::HandlebarsExt;
2use crate::helpers::string_ext::StringExt;
3use handlebars::HelperDef;
4use serde_json::Value;
5
6pub const UPPERCASE_HELPER: &str = "upper_case";
7pub const LOWERCASE_HELPER: &str = "lower_case";
8pub const UPPERCASE_FIRST_LETTER_HELPER: &str = "uppercase_first_letter";
9pub const LOWERCASE_FIRST_LETTER_HELPER: &str = "lowercase_first_letter";
10pub const PASCAL_CASE_HELPER: &str = "pascal_case";
11pub const SNAKE_CASE_HELPER: &str = "snake_case";
12pub const CAMEL_CASE_HELPER: &str = "camel_case";
13
14/// Returns the uppercase version of the string in argument
15/// ```
16/// # use codegenr_lib::helpers::*;
17/// # use serde_json::json;
18/// assert_eq!(
19///   exec_template(json!({ "value": "tEsT" }), "{{upper_case value}}"),
20///   "TEST"
21/// );
22/// ```
23pub struct UppercaseHelper;
24
25impl HelperDef for UppercaseHelper {
26  fn call_inner<'reg: 'rc, 'rc>(
27    &self,
28    h: &handlebars::Helper<'reg, 'rc>,
29    _: &'reg handlebars::Handlebars<'reg>,
30    _: &'rc handlebars::Context,
31    _: &mut handlebars::RenderContext<'reg, 'rc>,
32  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
33    h.ensure_arguments_count(1, UPPERCASE_HELPER)?;
34    let to_case = h.get_param_as_str_or_fail(0, UPPERCASE_HELPER)?;
35    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.to_uppercase())))
36  }
37}
38
39/// Returns the lowercase version of the string in argument
40/// ```
41/// # use codegenr_lib::helpers::*;
42/// # use serde_json::json;
43/// assert_eq!(
44///   exec_template(json!({ "value": "TEsT" }), "{{lower_case value}}"),
45///   "test"
46/// );
47/// ```
48pub struct LowercaseHelper;
49
50impl HelperDef for LowercaseHelper {
51  fn call_inner<'reg: 'rc, 'rc>(
52    &self,
53    h: &handlebars::Helper<'reg, 'rc>,
54    _: &'reg handlebars::Handlebars<'reg>,
55    _: &'rc handlebars::Context,
56    _: &mut handlebars::RenderContext<'reg, 'rc>,
57  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
58    h.ensure_arguments_count(1, LOWERCASE_HELPER)?;
59    let to_case = h.get_param_as_str_or_fail(0, LOWERCASE_HELPER)?;
60    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.to_lowercase())))
61  }
62}
63
64/// Returns a string with the first letter in Uppercase
65/// ```
66/// # use codegenr_lib::helpers::*;
67/// # use serde_json::json;
68/// assert_eq!(
69///   exec_template(json!({ "value": "tEsT" }), "{{uppercase_first_letter value}}"),
70///   "TEsT"
71/// );
72/// ```
73pub struct UppercaseFirstLetterHelper;
74
75impl HelperDef for UppercaseFirstLetterHelper {
76  fn call_inner<'reg: 'rc, 'rc>(
77    &self,
78    h: &handlebars::Helper<'reg, 'rc>,
79    _: &'reg handlebars::Handlebars<'reg>,
80    _: &'rc handlebars::Context,
81    _: &mut handlebars::RenderContext<'reg, 'rc>,
82  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
83    h.ensure_arguments_count(1, UPPERCASE_FIRST_LETTER_HELPER)?;
84    let to_case = h.get_param_as_str_or_fail(0, UPPERCASE_FIRST_LETTER_HELPER)?;
85    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.uppercase_first_letter())))
86  }
87}
88
89/// Returns a string with the first letter in Lowercase
90/// ```
91/// # use codegenr_lib::helpers::*;
92/// # use serde_json::json;
93/// assert_eq!(
94///   exec_template(json!({ "value": "TEST" }), "{{lowercase_first_letter value}}"),
95///   "tEST"
96/// );
97/// ```
98pub struct LowercaseFirstLetterHelper;
99
100impl HelperDef for LowercaseFirstLetterHelper {
101  fn call_inner<'reg: 'rc, 'rc>(
102    &self,
103    h: &handlebars::Helper<'reg, 'rc>,
104    _: &'reg handlebars::Handlebars<'reg>,
105    _: &'rc handlebars::Context,
106    _: &mut handlebars::RenderContext<'reg, 'rc>,
107  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
108    h.ensure_arguments_count(1, LOWERCASE_FIRST_LETTER_HELPER)?;
109    let to_case = h.get_param_as_str_or_fail(0, LOWERCASE_FIRST_LETTER_HELPER)?;
110    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.lowercase_first_letter())))
111  }
112}
113
114/// Returns the pascal case version of the string
115/// ```
116/// # use codegenr_lib::helpers::*;
117/// # use serde_json::json;
118/// assert_eq!(
119///   exec_template(json!({ "value": "this should be a function name." }), "{{pascal_case value}}"),
120///   "ThisShouldBeAFunctionName"
121/// );
122/// ```
123pub struct PascalcaseHelper;
124
125impl HelperDef for PascalcaseHelper {
126  fn call_inner<'reg: 'rc, 'rc>(
127    &self,
128    h: &handlebars::Helper<'reg, 'rc>,
129    _: &'reg handlebars::Handlebars<'reg>,
130    _: &'rc handlebars::Context,
131    _: &mut handlebars::RenderContext<'reg, 'rc>,
132  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
133    h.ensure_arguments_count(1, PASCAL_CASE_HELPER)?;
134    let to_case = h.get_param_as_str_or_fail(0, PASCAL_CASE_HELPER)?;
135    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.pascal_case())))
136  }
137}
138
139/// Returns the snake case version of the string
140/// ```
141/// # use codegenr_lib::helpers::*;
142/// # use serde_json::json;
143/// assert_eq!(
144///   exec_template(json!({ "value": "this should be a function name" }), "{{snake_case value}}"),
145///   "this_should_be_a_function_name"
146/// );
147/// ```
148pub struct SnakecaseHelper;
149
150impl HelperDef for SnakecaseHelper {
151  fn call_inner<'reg: 'rc, 'rc>(
152    &self,
153    h: &handlebars::Helper<'reg, 'rc>,
154    _: &'reg handlebars::Handlebars<'reg>,
155    _: &'rc handlebars::Context,
156    _: &mut handlebars::RenderContext<'reg, 'rc>,
157  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
158    h.ensure_arguments_count(1, SNAKE_CASE_HELPER)?;
159    let to_case = h.get_param_as_str_or_fail(0, SNAKE_CASE_HELPER)?;
160    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.snake_case())))
161  }
162}
163
164/// Returns the camel case version of the string
165/// ```
166/// # use codegenr_lib::helpers::*;
167/// # use serde_json::json;
168/// assert_eq!(
169///   exec_template(json!({ "value": "this should be a function name." }), "{{camel_case value}}"),
170///   "thisShouldBeAFunctionName"
171/// );
172/// ```
173pub struct CamelcaseHelper;
174
175impl HelperDef for CamelcaseHelper {
176  fn call_inner<'reg: 'rc, 'rc>(
177    &self,
178    h: &handlebars::Helper<'reg, 'rc>,
179    _: &'reg handlebars::Handlebars<'reg>,
180    _: &'rc handlebars::Context,
181    _: &mut handlebars::RenderContext<'reg, 'rc>,
182  ) -> Result<handlebars::ScopedJson<'reg, 'rc>, handlebars::RenderError> {
183    h.ensure_arguments_count(1, CAMEL_CASE_HELPER)?;
184    let to_case = h.get_param_as_str_or_fail(0, CAMEL_CASE_HELPER)?;
185    Ok(handlebars::ScopedJson::Derived(Value::String(to_case.camel_case())))
186  }
187}