use super::Flow;
use crate::flow::{
condition::Condition,
processor::Processor,
stage::{BranchStage, Stage},
};
use std::error::Error;
use std::marker::PhantomData;
use tracing::{debug, instrument};
pub struct OtherwiseBuilder<DataType, OutputType, ErrorType> {
condition:
Box<dyn Condition<Input = DataType, Output = OutputType, Error = ErrorType> + Send + Sync>,
then_branch: Vec<Stage<DataType, ErrorType, OutputType>>,
else_branch: Vec<Stage<DataType, ErrorType, OutputType>>,
parent: Flow<DataType, ErrorType, OutputType>,
}
impl<DataType, OutputType, ErrorType> OtherwiseBuilder<DataType, OutputType, ErrorType>
where
DataType: Clone + Send + Sync + 'static,
OutputType: Send + Sync + 'static,
ErrorType: Error + Send + Sync + 'static,
{
#[instrument(skip(condition, then_branch, parent))]
pub(crate) fn new(
condition: Box<
dyn Condition<Input = DataType, Output = OutputType, Error = ErrorType> + Send + Sync,
>,
then_branch: Vec<Stage<DataType, ErrorType, OutputType>>,
parent: Flow<DataType, ErrorType, OutputType>,
) -> Self {
debug!("Creating new otherwise builder");
Self {
condition,
then_branch,
else_branch: Vec::new(),
parent,
}
}
#[instrument(skip(self, processor))]
#[must_use]
pub fn process<ProcessorType>(mut self, processor: ProcessorType) -> Self
where
ProcessorType: Processor<Input = DataType, Output = DataType, Error = ErrorType>
+ Send
+ Sync
+ 'static,
{
debug!("Adding processor to else branch");
self.else_branch.push(Stage::Process(Box::new(processor)));
self
}
#[instrument(skip(self))]
#[must_use]
pub fn end(self) -> Flow<DataType, ErrorType, OutputType> {
debug!("Finalizing branch construction");
let branch_stage = Stage::Branch(Box::new(BranchStage {
condition: self.condition,
then_branch: self.then_branch,
else_branch: self.else_branch,
_marker: PhantomData,
}));
let mut flow = self.parent;
flow.stages.push(branch_stage);
debug!("Branch added to flow");
flow
}
}