use crate::{SimpleProcessor, SinkHandle};
pub struct SimpleProcessorBuilder<T, S> {
pub transformer: T,
pub sink: S,
}
impl SimpleProcessorBuilder<fn(String) -> Option<String>, ()> {
pub fn new() -> Self {
Self {
transformer: Some,
sink: (),
}
}
}
impl Default for SimpleProcessorBuilder<fn(String) -> Option<String>, ()> {
fn default() -> Self {
Self::new()
}
}
impl<T, S> SimpleProcessorBuilder<T, S> {
pub fn transformer<N: FnMut(String) -> Option<String> + Send + 'static>(
self,
transformer: N,
) -> SimpleProcessorBuilder<N, S> {
SimpleProcessorBuilder {
transformer,
sink: self.sink,
}
}
pub fn sink(self, sink: SinkHandle) -> SimpleProcessorBuilder<T, SinkHandle> {
SimpleProcessorBuilder {
transformer: self.transformer,
sink,
}
}
}
impl<T: FnMut(String) -> Option<String> + Send + 'static> SimpleProcessorBuilder<T, SinkHandle> {
pub fn build(self) -> SimpleProcessor<T> {
SimpleProcessor::new(self.transformer, self.sink)
}
}
#[cfg(test)]
mod tests {
use super::*;
macro_rules! assert_unit {
($unit:expr) => {
let _: () = $unit;
};
}
#[test]
fn default_transformer() {
let processor = SimpleProcessorBuilder::default();
assert_unit!(processor.sink);
let text = "test".to_string();
let transformed = (processor.transformer)(text.clone());
assert_eq!(transformed, Some(text));
}
}