-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from yuma140902/text-to-template
add TextAsTemplate transformer
- Loading branch information
Showing
4 changed files
with
30 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
use anyhow::Context; | ||
use handlebars::Template; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
use crate::{store::Store, Value}; | ||
|
||
use super::Transformer; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct TextAsTemplate; | ||
|
||
impl Transformer for TextAsTemplate { | ||
fn transform(&self, value: &Value, _store: &Store) -> anyhow::Result<Value> { | ||
if let Value::JSON(serde_json::Value::String(string)) = value { | ||
Ok(Value::Template(Template::compile(string).with_context( | ||
|| format!("failed to compile template {}", string), | ||
)?)) | ||
} else { | ||
anyhow::bail!( | ||
"value should be string, but it was {}", | ||
value.get_type_name() | ||
) | ||
} | ||
} | ||
} |