use std::error::Error;
use handlebars::{handlebars_helper, Handlebars};
use time::format_description::parse;
use time::OffsetDateTime;
handlebars_helper!(date: |dt: OffsetDateTime| dt.format(&parse("[year]-[month]-[day]").unwrap()).unwrap());
handlebars_helper!(nargs: |*args| args.len());
handlebars_helper!(date2: |dt: OffsetDateTime, {fmt:str = "[year]-[month]-[day]"}|
dt.format(&parse(fmt).unwrap()).unwrap()
);
fn main() -> Result<(), Box<dyn Error>> {
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));
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}}", &())?);
Ok(())
}