From 55678c61fd47321d11e4a2ad63b9eaaccb92059b Mon Sep 17 00:00:00 2001 From: David Bernard Date: Sat, 7 Dec 2024 18:21:37 +0100 Subject: [PATCH] refactor: group transformer under dedicated module and features flags for dependencies --- cdviz-collector/Cargo.toml | 6 +++- cdviz-collector/src/errors.rs | 4 +-- cdviz-collector/src/sources/hbs.rs | 30 ------------------- cdviz-collector/src/sources/mod.rs | 1 - .../src/sources/transformers/hbs.rs | 30 +++++++++++++++++++ .../{transformers.rs => transformers/mod.rs} | 7 ++++- 6 files changed, 43 insertions(+), 35 deletions(-) delete mode 100644 cdviz-collector/src/sources/hbs.rs create mode 100644 cdviz-collector/src/sources/transformers/hbs.rs rename cdviz-collector/src/sources/{transformers.rs => transformers/mod.rs} (91%) diff --git a/cdviz-collector/Cargo.toml b/cdviz-collector/Cargo.toml index fc88efc..137ed86 100644 --- a/cdviz-collector/Cargo.toml +++ b/cdviz-collector/Cargo.toml @@ -116,11 +116,15 @@ source_opendal = [ "dep:handlebars", "dep:handlebars_misc_helpers", "dep:opendal", + "transformer_hbs", ] +#! ### Transformers +transformer_hbs = ["dep:handlebars", "dep:handlebars_misc_helpers"] + #! ### Tools tool_ui = ["dep:cliclack", "dep:console", "dep:similar"] -tool_transform = ["source_opendal", "tool_ui"] +tool_transform = ["source_opendal", "tool_ui", "transformer_hbs"] [target.'cfg(all(target_env = "musl", target_pointer_width = "64"))'.dependencies.jemallocator] version = "0.5" diff --git a/cdviz-collector/src/errors.rs b/cdviz-collector/src/errors.rs index e01e49f..eb6cb89 100644 --- a/cdviz-collector/src/errors.rs +++ b/cdviz-collector/src/errors.rs @@ -31,9 +31,9 @@ pub(crate) enum Error { Opendal(opendal::Error), #[cfg(feature = "source_opendal")] GlobPattern(globset::Error), - #[cfg(feature = "source_opendal")] + #[cfg(feature = "transformer_hbs")] HandlebarsRender(handlebars::RenderError), - #[cfg(feature = "source_opendal")] + #[cfg(feature = "transformer_hbs")] HandlebarsTemplate(handlebars::TemplateError), #[cfg(feature = "source_opendal")] Csv(csv::Error), diff --git a/cdviz-collector/src/sources/hbs.rs b/cdviz-collector/src/sources/hbs.rs deleted file mode 100644 index bdfed2b..0000000 --- a/cdviz-collector/src/sources/hbs.rs +++ /dev/null @@ -1,30 +0,0 @@ -use super::{EventSource, EventSourcePipe}; -use crate::errors::Result; -use crate::pipes::Pipe; -use handlebars::Handlebars; - -pub(crate) struct Processor { - next: EventSourcePipe, - hbs: Handlebars<'static>, -} - -impl Processor { - pub(crate) fn new(template: &str, next: EventSourcePipe) -> Result { - let mut hbs = Handlebars::new(); - hbs.set_dev_mode(false); - hbs.set_strict_mode(true); - hbs.register_escape_fn(handlebars::no_escape); - handlebars_misc_helpers::register(&mut hbs); - hbs.register_template_string("tpl", template)?; - Ok(Self { next, hbs }) - } -} - -impl Pipe for Processor { - type Input = EventSource; - fn send(&mut self, input: Self::Input) -> Result<()> { - let res = self.hbs.render("tpl", &input)?; - let output: EventSource = serde_json::from_str(&res)?; - self.next.send(output) - } -} diff --git a/cdviz-collector/src/sources/mod.rs b/cdviz-collector/src/sources/mod.rs index fa89512..37b3baa 100644 --- a/cdviz-collector/src/sources/mod.rs +++ b/cdviz-collector/src/sources/mod.rs @@ -1,5 +1,4 @@ pub(crate) mod extractors; -mod hbs; #[cfg(feature = "source_http")] pub(crate) mod http; #[cfg(feature = "source_opendal")] diff --git a/cdviz-collector/src/sources/transformers/hbs.rs b/cdviz-collector/src/sources/transformers/hbs.rs new file mode 100644 index 0000000..86b8d0a --- /dev/null +++ b/cdviz-collector/src/sources/transformers/hbs.rs @@ -0,0 +1,30 @@ +use crate::errors::Result; +use crate::pipes::Pipe; +use crate::sources::{EventSource, EventSourcePipe}; +use handlebars::Handlebars; + +pub(crate) struct Processor { + next: EventSourcePipe, + renderer: Handlebars<'static>, +} + +impl Processor { + pub(crate) fn new(template: &str, next: EventSourcePipe) -> Result { + let mut renderer = Handlebars::new(); + renderer.set_dev_mode(false); + renderer.set_strict_mode(true); + renderer.register_escape_fn(handlebars::no_escape); + handlebars_misc_helpers::register(&mut renderer); + renderer.register_template_string("tpl", template)?; + Ok(Self { next, renderer }) + } +} + +impl Pipe for Processor { + type Input = EventSource; + fn send(&mut self, input: Self::Input) -> Result<()> { + let res = self.renderer.render("tpl", &input)?; + let output: EventSource = serde_json::from_str(&res)?; + self.next.send(output) + } +} diff --git a/cdviz-collector/src/sources/transformers.rs b/cdviz-collector/src/sources/transformers/mod.rs similarity index 91% rename from cdviz-collector/src/sources/transformers.rs rename to cdviz-collector/src/sources/transformers/mod.rs index 12a79fd..8e66c0e 100644 --- a/cdviz-collector/src/sources/transformers.rs +++ b/cdviz-collector/src/sources/transformers/mod.rs @@ -1,6 +1,9 @@ +#[cfg(feature = "transformer_hbs")] +mod hbs; + use std::collections::HashMap; -use super::{hbs, EventSourcePipe}; +use super::EventSourcePipe; use crate::{ errors::{Error, Result}, pipes::{discard_all, log, passthrough}, @@ -17,6 +20,7 @@ pub(crate) enum Config { Log(log::Config), #[serde(alias = "discard_all")] DiscardAll, + #[cfg(feature = "transformer_hbs")] #[serde(alias = "hbs")] Hbs { template: String }, // #[serde(alias = "vrl")] @@ -29,6 +33,7 @@ impl Config { Config::Passthrough => Box::new(passthrough::Processor::new(next)), Config::Log(config) => Box::new(log::Processor::try_from(config, next)?), Config::DiscardAll => Box::new(discard_all::Processor::new()), + #[cfg(feature = "transformer_hbs")] Config::Hbs { template } => Box::new(hbs::Processor::new(template, next)?), }; Ok(out)