BranchBuilder

Struct BranchBuilder 

Source
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>
where DataType: Clone + Send + Sync + 'static, OutputType: Send + Sync + 'static, ErrorType: Error + Send + Sync + 'static,

Source

pub fn new( condition: Box<dyn Condition<Input = DataType, Output = OutputType, Error = ErrorType> + Send + Sync>, parent: Flow<DataType, ErrorType, OutputType>, ) -> Self

Creates a new BranchBuilder with the specified condition and parent flow.

§Arguments
  • condition - A boxed condition that determines when this branch should execute
  • parent - The parent flow this branch belongs to
§Returns

A new instance of BranchBuilder

Source

pub fn process<ProcessorType>(self, processor: ProcessorType) -> Self
where ProcessorType: Processor<Input = DataType, Output = DataType, Error = ErrorType> + Send + Sync + 'static,

Adds a processor to the branch that will be executed when the condition is true.

§Arguments
  • processor - The processor to add to the branch
§Returns

The builder instance for method chaining

§Type Parameters
  • ProcessorType - The type of the processor being added
Source

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>
where DataType: Send, ErrorType: Send, OutputType: Send,

§

impl<DataType, OutputType, ErrorType> Sync for BranchBuilder<DataType, OutputType, ErrorType>
where DataType: Sync, ErrorType: Sync, OutputType: Sync,

§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more