pub struct OtherwiseBuilder<DataType, OutputType, ErrorType> { /* private fields */ }
Expand description
A builder for constructing the alternative branch of a conditional 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()
.when(MyCondition)
.process(MyProcessor)
.otherwise()
.process(MyProcessor)
.end();
Implementations§
Source§impl<DataType, OutputType, ErrorType> OtherwiseBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> OtherwiseBuilder<DataType, OutputType, ErrorType>
Sourcepub fn end(self) -> Flow<DataType, ErrorType, OutputType>
pub fn end(self) -> Flow<DataType, ErrorType, OutputType>
Finalizes the branch construction and returns the parent flow.
This method combines the condition, then branch, and else branch into a single branch stage and adds it to the parent flow.
§Returns
The parent flow with the completed branch stage added
Auto Trait Implementations§
impl<DataType, OutputType, ErrorType> Freeze for OtherwiseBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> !RefUnwindSafe for OtherwiseBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> Send for OtherwiseBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> Sync for OtherwiseBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> Unpin for OtherwiseBuilder<DataType, OutputType, ErrorType>
impl<DataType, OutputType, ErrorType> !UnwindSafe for OtherwiseBuilder<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