Skip to content

Commit

Permalink
refactor: group transformer under dedicated module and features flags…
Browse files Browse the repository at this point in the history
… for dependencies
  • Loading branch information
davidB committed Dec 9, 2024
1 parent ae907c4 commit 55678c6
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
6 changes: 5 additions & 1 deletion cdviz-collector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions cdviz-collector/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
30 changes: 0 additions & 30 deletions cdviz-collector/src/sources/hbs.rs

This file was deleted.

1 change: 0 additions & 1 deletion cdviz-collector/src/sources/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub(crate) mod extractors;
mod hbs;
#[cfg(feature = "source_http")]
pub(crate) mod http;
#[cfg(feature = "source_opendal")]
Expand Down
30 changes: 30 additions & 0 deletions cdviz-collector/src/sources/transformers/hbs.rs
Original file line number Diff line number Diff line change
@@ -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<Self> {
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)
}
}
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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")]
Expand All @@ -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)
Expand Down

0 comments on commit 55678c6

Please sign in to comment.