diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2020-02-28 10:46:08 +0100 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2020-02-28 11:01:09 +0100 |
commit | b01424e280b3966ece064446b7cd91d0b16dc521 (patch) | |
tree | 5b45202dd7338b26c2d254dc04a1f12be8f30747 | |
parent | 0d21b32431da044b87d937e4803c7399ad5eae93 (diff) |
Add helper type for passing data to Handlebars::render()
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | lib/etc/libimaginteraction/Cargo.toml | 2 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/format.rs | 39 | ||||
-rw-r--r-- | lib/etc/libimaginteraction/src/lib.rs | 2 |
3 files changed, 43 insertions, 0 deletions
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<String, HandlebarsData> 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<bool> for HandlebarsData { + fn from(b: bool) -> Self { + HandlebarsData::Bool(b) + } +} + +impl From<usize> for HandlebarsData { + fn from(u: usize) -> Self { + HandlebarsData::Int(u) + } +} + +impl From<f64> for HandlebarsData { + fn from(f: f64) -> Self { + HandlebarsData::Float(f) + } +} + +impl From<String> 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; |