pub struct BranchBuilder<DataType, OutputType, ErrorType> { /* private fields */ }
Expand description
A builder for constructing conditional branches in a flow.
§Examples
use cortex_ai::composer::Flow;
use cortex_ai::flow::condition::Condition;
use cortex_ai::flow::processor::Processor;
use cortex_ai::FlowComponent;
use cortex_ai::FlowError;
use std::error::Error;
use std::fmt;
use std::pin::Pin;
use std::future::Future;
#[derive(Clone, Debug)]
struct MyData;
#[derive(Clone, Debug)]
struct MyError;
impl fmt::Display for MyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MyError")
}
}
impl Error for MyError {}
impl From<FlowError> for MyError {
fn from(e: FlowError) -> Self { MyError }
}
struct MyProcessor;
impl FlowComponent for MyProcessor {
type Input = MyData;
type Output = MyData;
type Error = MyError;
}
impl Processor for MyProcessor {
fn process(&self, input: Self::Input) -> Pin<Box<dyn Future<Output = Result<Self::Output, Self::Error>> + Send>> {
Box::pin(async move { Ok(input) })
}
}
struct MyCondition;
impl FlowComponent for MyCondition {
type Input = MyData;
type Output = bool;
type Error = MyError;
}
impl Condition for MyCondition {
fn evaluate(&self, input: Self::Input) -> Pin<Box<dyn Future<Output = Result<(bool, Option<Self::Output>), Self::Error>> + Send>> {
Box::pin(async move { Ok((true, Some(false))) })
}
}
let flow = Flow::<MyData, MyError, bool>::new();
let branch = flow
.when(MyCondition)
.process(MyProcessor)
.otherwise();
Implementations§
Source§impl<DataType, OutputType, ErrorType> BranchBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> BranchBuilder<DataType, OutputType, ErrorType>
Sourcepub fn new(
condition: Box<dyn Condition<Input = DataType, Output = OutputType, Error = ErrorType> + Send + Sync>,
parent: Flow<DataType, ErrorType, OutputType>,
) -> Self
pub fn new( condition: Box<dyn Condition<Input = DataType, Output = OutputType, Error = ErrorType> + Send + Sync>, parent: Flow<DataType, ErrorType, OutputType>, ) -> Self
Sourcepub fn otherwise(self) -> OtherwiseBuilder<DataType, OutputType, ErrorType>
pub fn otherwise(self) -> OtherwiseBuilder<DataType, OutputType, ErrorType>
Transitions to building the alternative branch that will be executed when the condition is false.
§Returns
An OtherwiseBuilder
for constructing the alternative branch
Auto Trait Implementations§
impl<DataType, OutputType, ErrorType> Freeze for BranchBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> !RefUnwindSafe for BranchBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> Send for BranchBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> Sync for BranchBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> Unpin for BranchBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> !UnwindSafe for BranchBuilder<DataType, OutputType, ErrorType>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more