| 1 | /// Macro that allows you to quickly define a handlebars helper by passing a |
| 2 | /// name and a closure. |
| 3 | /// |
| 4 | /// There are several types of arguments available to closure: |
| 5 | /// |
| 6 | /// * Parameters are mapped to closure arguments one by one. Any declared |
| 7 | /// parameters are required |
| 8 | /// * Hash are mapped as named arguments and declared in a bracket block. |
| 9 | /// All named arguments are optional so default value is required. |
| 10 | /// * An optional `*args` provides a vector of all helper parameters. |
| 11 | /// * An optional `**kwargs` provides a map of all helper hash. |
| 12 | /// |
| 13 | /// # Examples |
| 14 | /// |
| 15 | /// ```rust |
| 16 | /// #[macro_use] extern crate handlebars; |
| 17 | /// #[macro_use] extern crate serde_json; |
| 18 | /// |
| 19 | /// handlebars_helper!(is_above_10: |x: u64| x > 10); |
| 20 | /// handlebars_helper!(is_above: |x: u64, { compare: u64 = 10 }| x > compare); |
| 21 | /// |
| 22 | /// # fn main() { |
| 23 | /// # |
| 24 | /// let mut handlebars = handlebars::Handlebars::new(); |
| 25 | /// handlebars.register_helper("is-above-10", Box::new(is_above_10)); |
| 26 | /// handlebars.register_helper("is-above", Box::new(is_above)); |
| 27 | /// |
| 28 | /// let result = handlebars |
| 29 | /// .render_template("{{#if (is-above-10 12)}}great!{{else}}okay{{/if}}", &json!({})) |
| 30 | /// .unwrap(); |
| 31 | /// assert_eq!(&result, "great!"); |
| 32 | /// let result2 = handlebars |
| 33 | /// .render_template("{{#if (is-above 12 compare=10)}}great!{{else}}okay{{/if}}", &json!({})) |
| 34 | /// .unwrap(); |
| 35 | /// assert_eq!(&result2, "great!"); |
| 36 | /// # } |
| 37 | /// ``` |
| 38 | |
| 39 | #[macro_export] |
| 40 | macro_rules! handlebars_helper { |
| 41 | ($struct_name:ident: |$($name:ident: $tpe:tt$(<$($gen:ty),+>)?),* |
| 42 | $($(,)?{$($hash_name:ident: $hash_tpe:tt=$dft_val:literal),*})? |
| 43 | $($(,)?*$args:ident)? |
| 44 | $($(,)?**$kwargs:ident)?| |
| 45 | $body:expr ) => { |
| 46 | #[allow(non_camel_case_types)] |
| 47 | pub struct $struct_name; |
| 48 | |
| 49 | impl $crate::HelperDef for $struct_name { |
| 50 | #[allow(unused_assignments)] |
| 51 | fn call_inner<'reg: 'rc, 'rc>( |
| 52 | &self, |
| 53 | h: &$crate::Helper<'rc>, |
| 54 | r: &'reg $crate::Handlebars<'reg>, |
| 55 | _: &'rc $crate::Context, |
| 56 | _: &mut $crate::RenderContext<'reg, 'rc>, |
| 57 | ) -> std::result::Result<$crate::ScopedJson<'rc>, $crate::RenderError> { |
| 58 | let mut param_idx = 0; |
| 59 | |
| 60 | $( |
| 61 | let $name = h.param(param_idx) |
| 62 | .and_then(|x| { |
| 63 | if r.strict_mode() && x.is_value_missing() { |
| 64 | None |
| 65 | } else { |
| 66 | Some(x.value()) |
| 67 | } |
| 68 | }) |
| 69 | .ok_or_else(|| $crate::RenderErrorReason::ParamNotFoundForName(stringify!($struct_name), stringify!($name).to_string())) |
| 70 | .and_then(|x| |
| 71 | $crate::handlebars_helper!(@as_json_value x, $tpe$(<$($gen),+>)?) |
| 72 | .ok_or_else(|| $crate::RenderErrorReason::ParamTypeMismatchForName(stringify!($struct_name), stringify!($name).to_string(), stringify!($tpe$(<$($gen),+>)?).to_string()).into()) |
| 73 | )?; |
| 74 | param_idx += 1; |
| 75 | )* |
| 76 | |
| 77 | $( |
| 78 | $( |
| 79 | let $hash_name = h.hash_get(stringify!($hash_name)) |
| 80 | .map(|x| x.value()) |
| 81 | .map(|x| |
| 82 | $crate::handlebars_helper!(@as_json_value x, $hash_tpe) |
| 83 | .ok_or_else(|| $crate::RenderErrorReason::HashTypeMismatchForName( |
| 84 | stringify!($struct_name), stringify!($hash_name).to_string(), stringify!($hash_tpe).to_string() |
| 85 | )) |
| 86 | ) |
| 87 | .unwrap_or_else(|| Ok($dft_val))?; |
| 88 | )* |
| 89 | )? |
| 90 | |
| 91 | $(let $args = h.params().iter().map(|x| x.value()).collect::<Vec<&serde_json::Value>>();)? |
| 92 | $(let $kwargs = h.hash().iter().map(|(k, v)| (k.to_owned(), v.value())).collect::<std::collections::BTreeMap<&str, &serde_json::Value>>();)? |
| 93 | |
| 94 | let result = $body; |
| 95 | Ok($crate::ScopedJson::Derived($crate::JsonValue::from(result))) |
| 96 | } |
| 97 | } |
| 98 | }; |
| 99 | |
| 100 | (@as_json_value $x:ident, object) => { $x.as_object() }; |
| 101 | (@as_json_value $x:ident, array) => { $x.as_array() }; |
| 102 | (@as_json_value $x:ident, str) => { $x.as_str() }; |
| 103 | (@as_json_value $x:ident, i64) => { $x.as_i64() }; |
| 104 | (@as_json_value $x:ident, u64) => { $x.as_u64() }; |
| 105 | (@as_json_value $x:ident, f64) => { $x.as_f64() }; |
| 106 | (@as_json_value $x:ident, bool) => { $x.as_bool() }; |
| 107 | (@as_json_value $x:ident, null) => { $x.as_null() }; |
| 108 | (@as_json_value $x:ident, Json) => { Some($x) }; |
| 109 | (@as_json_value $x:ident, $tpe:tt$(<$($gen:ty),+>)?) => { serde_json::from_value::<$tpe$(<$($gen),+>)?>($x.clone()).ok() }; |
| 110 | } |
| 111 | |
| 112 | #[cfg(feature = "no_logging")] |
| 113 | #[macro_use] |
| 114 | #[doc(hidden)] |
| 115 | pub mod logging { |
| 116 | /// This macro is defined if the `logging` feature is set. |
| 117 | /// |
| 118 | /// It ignores all logging calls inside the library. |
| 119 | #[doc(hidden)] |
| 120 | #[macro_export] |
| 121 | macro_rules! debug { |
| 122 | (target: $target:expr, $($arg:tt)*) => {}; |
| 123 | ($($arg:tt)*) => {}; |
| 124 | } |
| 125 | |
| 126 | /// This macro is defined if the `logging` feature is not set. |
| 127 | /// |
| 128 | /// It ignores all logging calls inside the library. |
| 129 | #[doc(hidden)] |
| 130 | #[macro_export] |
| 131 | macro_rules! error { |
| 132 | (target: $target:expr, $($arg:tt)*) => {}; |
| 133 | ($($arg:tt)*) => {}; |
| 134 | } |
| 135 | |
| 136 | /// This macro is defined if the `logging` feature is not set. |
| 137 | /// |
| 138 | /// It ignores all logging calls inside the library. |
| 139 | #[doc(hidden)] |
| 140 | #[macro_export] |
| 141 | macro_rules! info { |
| 142 | (target: $target:expr, $($arg:tt)*) => {}; |
| 143 | ($($arg:tt)*) => {}; |
| 144 | } |
| 145 | |
| 146 | /// This macro is defined if the `logging` feature is not set. |
| 147 | /// |
| 148 | /// It ignores all logging calls inside the library. |
| 149 | #[doc(hidden)] |
| 150 | #[macro_export] |
| 151 | macro_rules! log { |
| 152 | (target: $target:expr, $($arg:tt)*) => {}; |
| 153 | ($($arg:tt)*) => {}; |
| 154 | } |
| 155 | |
| 156 | /// This macro is defined if the `logging` feature is not set. |
| 157 | /// |
| 158 | /// It ignores all logging calls inside the library. |
| 159 | #[doc(hidden)] |
| 160 | #[macro_export] |
| 161 | macro_rules! trace { |
| 162 | (target: $target:expr, $($arg:tt)*) => {}; |
| 163 | ($($arg:tt)*) => {}; |
| 164 | } |
| 165 | |
| 166 | /// This macro is defined if the `logging` feature is not set. |
| 167 | /// |
| 168 | /// It ignores all logging calls inside the library. |
| 169 | #[doc(hidden)] |
| 170 | #[macro_export] |
| 171 | macro_rules! warn { |
| 172 | (target: $target:expr, $($arg:tt)*) => {}; |
| 173 | ($($arg:tt)*) => {}; |
| 174 | } |
| 175 | } |