From b01424e280b3966ece064446b7cd91d0b16dc521 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Fri, 28 Feb 2020 10:46:08 +0100 Subject: Add helper type for passing data to Handlebars::render() Signed-off-by: Matthias Beyer --- lib/etc/libimaginteraction/Cargo.toml | 2 ++ lib/etc/libimaginteraction/src/format.rs | 39 ++++++++++++++++++++++++++++++++ lib/etc/libimaginteraction/src/lib.rs | 2 ++ 3 files changed, 43 insertions(+) diff --git a/lib/etc/libimaginteraction/Cargo.toml b/lib/etc/libimaginteraction/Cargo.toml index ac4a952c..01c6e0c7 100644 --- a/lib/etc/libimaginteraction/Cargo.toml +++ b/lib/etc/libimaginteraction/Cargo.toml @@ -28,6 +28,8 @@ regex = "1.1.7" toml = "0.5.1" handlebars = "2" serde_json = "1.0.39" +serde_derive = "1" +serde = "1" failure = "0.1.5" failure_derive = "0.1.5" diff --git a/lib/etc/libimaginteraction/src/format.rs b/lib/etc/libimaginteraction/src/format.rs index 0f38257e..6515df2d 100644 --- a/lib/etc/libimaginteraction/src/format.rs +++ b/lib/etc/libimaginteraction/src/format.rs @@ -23,6 +23,45 @@ use serde_json::value::Value; use ansi_term::Colour; use ansi_term::Style; +/// Helper type for passing data to handlebars +/// +/// This helper type can be used to fill a BtreeMap which is then passed to +/// Handlebars::render() for passing data to the template string. +/// +#[derive(Clone, Debug, Serialize)] +#[serde(untagged)] +pub enum HandlebarsData { + Bool(bool), + Int(usize), + Float(f64), + Str(String), +} + +impl From for HandlebarsData { + fn from(b: bool) -> Self { + HandlebarsData::Bool(b) + } +} + +impl From for HandlebarsData { + fn from(u: usize) -> Self { + HandlebarsData::Int(u) + } +} + +impl From for HandlebarsData { + fn from(f: f64) -> Self { + HandlebarsData::Float(f) + } +} + +impl From for HandlebarsData { + fn from(s: String) -> Self { + HandlebarsData::Str(s) + } +} + + #[derive(Clone, Copy)] pub struct ColorizeBlackHelper; diff --git a/lib/etc/libimaginteraction/src/lib.rs b/lib/etc/libimaginteraction/src/lib.rs index 36dceb17..980ab92c 100644 --- a/lib/etc/libimaginteraction/src/lib.rs +++ b/lib/etc/libimaginteraction/src/lib.rs @@ -44,6 +44,8 @@ extern crate regex; extern crate clap; extern crate toml; extern crate handlebars; +extern crate serde; +#[macro_use] extern crate serde_derive; extern crate serde_json; extern crate failure; -- cgit v1.2.3