pub struct OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes = (), NodeIOETypes = ()> { /* private fields */ }Expand description
OneOfSequentialFlow executes nodes (branches) sequentially, returning when one succeeds or fails.
Nodes (branches) are executed sequentially in order of insertion until one succeeds or “hard” fails.
- If a node returns
NodeOutput::Ok, that value is returned. - If a node returns
NodeOutput::SoftFail, the flow continues onto the next node (branch). - If a node returns an error, then that error is returned.
If all nodes (branches) soft-fail, the flow itself returns NodeOutput::SoftFail.
This flow allows for defining fallback-style chain, where multiple nodes are tried in sequence until one handles the input successfully.
§Type Parameters
Input: The type of data accepted by this flow.Output: The type of data produced by this flow.Error: The type of error emitted by this flow.Context: The type of context used during execution.
§Examples
use node_flow::node::{Node, NodeOutput};
use node_flow::flows::OneOfSequentialFlow;
use node_flow::context::{Fork, Update};
// Example nodes
#[derive(Clone)]
struct A;
#[derive(Clone)]
struct B;
struct ExampleCtx;
impl Fork for ExampleCtx // ...
impl Update for ExampleCtx // ...
impl<Ctx: Send> Node<(), NodeOutput<i32>, (), Ctx> for A {
async fn run(&mut self, _: (), _: &mut Ctx) -> Result<NodeOutput<i32>, ()> {
Ok(NodeOutput::SoftFail) // Try next
}
}
impl<Ctx: Send> Node<(), NodeOutput<i32>, (), Ctx> for B {
async fn run(&mut self, _: (), _: &mut Ctx) -> Result<NodeOutput<i32>, ()> {
Ok(NodeOutput::Ok(5)) // Succeeds
}
}
async fn main() {
let mut flow = OneOfSequentialFlow::<(), i32, (), _>::builder()
.add_node(A)
.add_node(B)
.build();
let mut ctx = ExampleCtx;
let result = flow.run((), &mut ctx).await;
assert_eq!(result, Ok(NodeOutput::Ok(5)));
}Implementations§
Source§impl<Input, Output, Error, Context> OneOfSequentialFlow<Input, Output, Error, Context>
impl<Input, Output, Error, Context> OneOfSequentialFlow<Input, Output, Error, Context>
Sourcepub fn builder() -> Builder<Input, Output, Error, Context>
pub fn builder() -> Builder<Input, Output, Error, Context>
Creates a new Builder for constructing OneOfSequentialFlow.
See also OneOfSequentialFlow.
§Examples
use node_flow::flows::OneOfSequentialFlow;
let builder = OneOfSequentialFlow::<u8, u16, (), Ctx>::builder();Trait Implementations§
Source§impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Clone for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Clone for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>
Source§impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Debug for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>where
NodeTypes: ChainDebug,
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Debug for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>where
NodeTypes: ChainDebug,
Source§impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Node<Input, NodeOutput<Output>, Error, Context> for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>where
NodeTypes: ChainRun<Input, Result<NodeOutput<Output>, Error>, Context, NodeIOETypes> + ChainDescribe<Context, NodeIOETypes>,
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Node<Input, NodeOutput<Output>, Error, Context> for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>where
NodeTypes: ChainRun<Input, Result<NodeOutput<Output>, Error>, Context, NodeIOETypes> + ChainDescribe<Context, NodeIOETypes>,
Source§fn run(
&mut self,
input: Input,
context: &mut Context,
) -> impl Future<Output = Result<NodeOutput<Output>, Error>> + Send
fn run( &mut self, input: Input, context: &mut Context, ) -> impl Future<Output = Result<NodeOutput<Output>, Error>> + Send
Runs the node. Read more
Source§fn describe(&self) -> Description
fn describe(&self) -> Description
Describes this node, its type signature and other specifics. Read more
Auto Trait Implementations§
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Freeze for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> RefUnwindSafe for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>where
NodeTypes: RefUnwindSafe,
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Send for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Sync for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> Unpin for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>
impl<Input, Output, Error, Context, NodeTypes, NodeIOETypes> UnwindSafe for OneOfSequentialFlow<Input, Output, Error, Context, NodeTypes, NodeIOETypes>where
NodeTypes: RefUnwindSafe,
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
Source§impl<Input, Output, Error, Context, T> BoxedNode<Input, Output, Error, Context> for Twhere
T: Node<Input, Output, Error, Context>,
impl<Input, Output, Error, Context, T> BoxedNode<Input, Output, Error, Context> for Twhere
T: Node<Input, Output, Error, Context>,
Source§fn run_boxed<'life0, 'life1, 'async_trait>(
&'life0 mut self,
input: Input,
context: &'life1 mut Context,
) -> Pin<Box<dyn Future<Output = Result<Output, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Input: 'async_trait,
Output: 'async_trait,
Error: 'async_trait,
T: 'async_trait,
fn run_boxed<'life0, 'life1, 'async_trait>(
&'life0 mut self,
input: Input,
context: &'life1 mut Context,
) -> Pin<Box<dyn Future<Output = Result<Output, Error>> + Send + 'async_trait>>where
'life0: 'async_trait,
'life1: 'async_trait,
Input: 'async_trait,
Output: 'async_trait,
Error: 'async_trait,
T: 'async_trait,
Available on crate feature
boxed_node only.Runs the node. Read more
Source§fn describe(&self) -> Description
fn describe(&self) -> Description
Available on crate feature
boxed_node only.Describes this node, its type signature and other specifics. Read more