From 246f239d9f40f633160f0c18f87a20922d4e77bb Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:06:37 +0200 Subject: Merging debian version 1.65.0+dfsg1-2. Signed-off-by: Daniel Baumann --- vendor/handlebars/examples/helper_macro.rs | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 vendor/handlebars/examples/helper_macro.rs (limited to 'vendor/handlebars/examples/helper_macro.rs') diff --git a/vendor/handlebars/examples/helper_macro.rs b/vendor/handlebars/examples/helper_macro.rs new file mode 100644 index 000000000..dcc6ea31a --- /dev/null +++ b/vendor/handlebars/examples/helper_macro.rs @@ -0,0 +1,63 @@ +use std::error::Error; + +use handlebars::{handlebars_helper, Handlebars, JsonRender}; +use serde_json::{json, Value}; +use time::format_description::parse; +use time::OffsetDateTime; + +// define a helper using helper +// a date format helper accept an `OffsetDateTime` as parameter +handlebars_helper!(date: |dt: OffsetDateTime| dt.format(&parse("[year]-[month]-[day]").unwrap()).unwrap()); + +// a helper returns number of provided parameters +handlebars_helper!(nargs: |*args| args.len()); + +// a helper joins all values, using both hash and parameters +handlebars_helper!(join: |{sep:str=","}, *args| + args.iter().map(|a| a.render()).collect::>().join(sep) +); + +handlebars_helper!(isdefined: |v: Value| !v.is_null()); + +// a helper provides format +handlebars_helper!(date2: |dt: OffsetDateTime, {fmt:str = "[year]-[month]-[day]"}| + dt.format(&parse(fmt).unwrap()).unwrap() +); + +fn main() -> Result<(), Box> { + // create the handlebars registry + let mut handlebars = Handlebars::new(); + + handlebars.register_helper("date", Box::new(date)); + handlebars.register_helper("date2", Box::new(date2)); + handlebars.register_helper("nargs", Box::new(nargs)); + handlebars.register_helper("join", Box::new(join)); + handlebars.register_helper("isdefined", Box::new(isdefined)); + + let data = OffsetDateTime::now_utc(); + + println!("{}", handlebars.render_template("{{date this}}", &data)?); + println!("{}", handlebars.render_template("{{date2 this}}", &data)?); + println!( + "{}", + handlebars.render_template("{{date2 this fmt=\"[day]/[month]/[year]\"}}", &data)? + ); + + println!("{}", handlebars.render_template("{{nargs 1 2 3 4}}", &())?); + + println!( + "{}", + handlebars.render_template("{{join 1 2 3 4 sep=\"|\" }}", &())? + ); + + println!( + "{}", + handlebars.render_template( + r#"{{isdefined a}} {{isdefined b}} +{{#if (isdefined a)}}a{{/if}} {{#if (isdefined b)}}b{{/if}}"#, + &json!({"a": 1}) + )? + ); + + Ok(()) +} -- cgit v1.2.3