-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
346 additions
and
199 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use super::Pipe; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub(crate) struct Config {} | ||
|
||
pub(crate) struct Processor<I> { | ||
buffer: Vec<I>, | ||
} | ||
|
||
impl<I> Processor<I> | ||
where | ||
I: Clone, | ||
{ | ||
fn new() -> Self { | ||
Self { buffer: vec![] } | ||
} | ||
|
||
fn try_from(_config: Config) -> Result<Self> { | ||
Ok(Self::new()) | ||
} | ||
|
||
fn collected(&self) -> Vec<I> { | ||
self.buffer.clone() | ||
} | ||
} | ||
|
||
impl<I> Pipe for Processor<I> { | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
self.buffer.push(input); | ||
Ok(()) | ||
} | ||
} |
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,29 @@ | ||
use super::Pipe; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub(crate) struct Config {} | ||
|
||
struct Processor<I> { | ||
input_type: PhantomData<I>, | ||
} | ||
|
||
impl<I> Processor<I> { | ||
pub fn new() -> Self { | ||
Self { input_type: PhantomData } | ||
} | ||
|
||
pub fn try_from(_config: Config) -> Result<Self> { | ||
Ok(Self::new()) | ||
} | ||
} | ||
|
||
impl<I> Pipe for Processor<I> { | ||
type Input = I; | ||
fn send(&mut self, _input: Self::Input) -> Result<()> { | ||
Ok(()) | ||
} | ||
} |
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,38 @@ | ||
use super::Pipe; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
use std::marker::PhantomData; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub(crate) struct Config { | ||
target: String, | ||
} | ||
|
||
struct Processor<I, N> { | ||
target: String, | ||
next: N, | ||
input_type: PhantomData<I>, | ||
} | ||
|
||
impl<I, N> Processor<I, N> { | ||
pub(crate) fn new(target: String, next: N) -> Self { | ||
Self { target, next, input_type: PhantomData } | ||
} | ||
|
||
pub(crate) fn try_from(config: Config, next: N) -> Result<Self> { | ||
Ok(Self::new(config.target, next)) | ||
} | ||
} | ||
|
||
impl<I, N> Pipe for Processor<I, N> | ||
where | ||
I: Debug, | ||
N: Pipe<Input = I>, | ||
{ | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
tracing::info!(target=self.target, input=?input); | ||
self.next.send(input) | ||
} | ||
} |
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,37 @@ | ||
use crate::errors::Result; | ||
pub mod collect_to_vec; | ||
pub mod discard_all; | ||
pub mod log; | ||
|
||
/// A pipe is an interface to implement processor for inputs. | ||
/// The implementations can: | ||
/// - discard / drop all inputs | ||
/// - filter | ||
/// - transform | ||
/// - split | ||
/// - retry | ||
/// - timeout | ||
/// - ... | ||
/// The composition of Pipes to create pipeline could be done by configuration, | ||
/// and the behavior of the pipe should be internal, | ||
/// so chaining of pipes should not depends of method `map`, `fold`, `filter`, | ||
/// `filter_map`, `drop`,... like for `Iterator`, `Stream`, `RxRust`. | ||
/// Also being able to return Error to the sender could help the Sender to ease handling (vs `Stream`) | ||
/// like retry, buffering, forward to its caller... | ||
/// | ||
/// The approach and goal is similar to middleware used in some webframework | ||
/// or in [tower](https://crates.io/crates/tower), Except it's not async. | ||
/// Maybe if we need to support async, `Pipe` will become a specialization of tower's middleware, | ||
/// like [axum](https://crates.io/crates/axum), [warp](https://crates.io/crates/warp), [tonic](https://crates.io/crates/tonic),... do. | ||
pub trait Pipe { | ||
type Input; | ||
|
||
fn send(&mut self, input: Self::Input) -> Result<()>; | ||
} | ||
|
||
impl<I, T: Pipe<Input = I> + ?Sized> Pipe for Box<T> { | ||
type Input = I; | ||
fn send(&mut self, input: Self::Input) -> Result<()> { | ||
T::send(self, input) | ||
} | ||
} |
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,26 @@ | ||
use super::{http, opendal, EventSourcePipe, Extractor}; | ||
use crate::errors::Result; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
#[serde(tag = "kind")] | ||
pub(crate) enum Config { | ||
#[cfg(feature = "source_http")] | ||
#[serde(alias = "http")] | ||
Http(http::Config), | ||
#[cfg(feature = "source_opendal")] | ||
#[serde(alias = "opendal")] | ||
Opendal(opendal::Config), | ||
} | ||
|
||
impl Config { | ||
fn into_extractor(&self, next: EventSourcePipe) -> Result<Box<dyn Extractor>> { | ||
let out = match self { | ||
// #[cfg(feature = "source_http")] | ||
// Config::Http(config) => Box::new(http::HttpExtractor::try_from(config, next)?), | ||
// #[cfg(feature = "source_opendal")] | ||
// Config::Opendal(config) => OpendalSource::try_from(config)?.into(), | ||
}; | ||
Ok(out) | ||
} | ||
} |
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,39 @@ | ||
use super::{EventSource, EventSourcePipe}; | ||
use crate::errors::Result; | ||
use crate::pipes::Pipe; | ||
use handlebars::Handlebars; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fmt::Debug; | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub(crate) struct Config { | ||
template: String, | ||
} | ||
|
||
pub(crate) struct Processor { | ||
next: EventSourcePipe, | ||
hbs: Handlebars<'static>, | ||
} | ||
|
||
impl Processor { | ||
pub(crate) fn new(template: &str, next: EventSourcePipe) -> Result<Self> { | ||
let mut hbs = Handlebars::new(); | ||
hbs.set_dev_mode(false); | ||
hbs.set_strict_mode(true); | ||
hbs.register_template_string("tpl", template)?; | ||
Ok(Self { next, hbs }) | ||
} | ||
|
||
pub(crate) fn try_from(config: Config, next: EventSourcePipe) -> Result<Self> { | ||
Self::new(&config.template, next) | ||
} | ||
} | ||
|
||
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) | ||
} | ||
} |
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
Oops, something went wrong.