node_flow/flows/
one_of_sequential_flow.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4
5use crate::{
6    internal::internal_node2::{InternalNode2, InternalNodeStruct2},
7    node::{Node, NodeOutput},
8    storage::Storage,
9};
10
11pub struct OneOfSequentialFlow<Input, Output, Error> {
12    #[allow(clippy::type_complexity)]
13    #[cfg(not(all(doc, not(doctest))))]
14    nodes: Arc<Vec<Box<dyn InternalNode2<Input, Output, Error> + Sync>>>,
15    #[cfg(all(doc, not(doctest)))]
16    __: std::marker::PhantomData<(Input, Output, Error)>,
17}
18
19impl<Input, Output, Error> OneOfSequentialFlow<Input, Output, Error> {
20    /// Creates builder for [`OneOfSequentialFlow`].
21    #[must_use]
22    pub fn builder() -> OneOfSequentialFlowBuilder<Input, Output, Error> {
23        OneOfSequentialFlowBuilder::new()
24    }
25}
26
27#[cfg_attr(not(all(doc, not(doctest))), async_trait)]
28impl<Input, Output, Error> Node<Input, NodeOutput<Output>, Error>
29    for OneOfSequentialFlow<Input, Output, Error>
30where
31    Input: Clone + Send,
32{
33    async fn run_with_storage<'a>(
34        &mut self,
35        input: Input,
36        storage: &mut Storage,
37    ) -> Result<NodeOutput<Output>, Error> {
38        for mut node in self.nodes.iter().map(|node| node.duplicate()) {
39            let res = node.run_with_storage(input.clone(), storage).await?;
40            match res {
41                NodeOutput::SoftFail => {}
42                NodeOutput::Ok(output) => return Ok(NodeOutput::Ok(output)),
43            }
44        }
45        Ok(NodeOutput::SoftFail)
46    }
47}
48
49#[derive(Default)]
50pub struct OneOfSequentialFlowBuilder<Input, Output, Error> {
51    #[cfg(not(all(doc, not(doctest))))]
52    nodes: Vec<Box<dyn InternalNode2<Input, Output, Error> + Sync>>,
53    #[cfg(all(doc, not(doctest)))]
54    __: std::marker::PhantomData<(Input, Output, Error)>,
55}
56
57impl<Input, Output, Error> OneOfSequentialFlowBuilder<Input, Output, Error> {
58    /// Creates a new instance of [`OneOfSequentialFlowBuilder`].
59    #[must_use]
60    pub fn new() -> Self {
61        Self { nodes: Vec::new() }
62    }
63
64    /// Adds a node to the [`OneOfSequentialFlowBuilder`].
65    pub fn add_node<NodeType, NodeInput, NodeOutput_, NodeError>(&mut self, node: NodeType)
66    where
67        NodeType:
68            Node<NodeInput, NodeOutput<NodeOutput_>, NodeError> + Clone + Send + Sync + 'static,
69        Input: Into<NodeInput> + Send + Sync + 'static,
70        Output: Send + Sync + 'static,
71        Error: Send + Sync + 'static,
72        NodeInput: Send + Sync + 'static,
73        NodeOutput_: Into<Output> + Send + Sync + 'static,
74        NodeError: Into<Error> + Send + Sync + 'static,
75    {
76        self.nodes.push(Box::new(InternalNodeStruct2::new(node)));
77    }
78
79    #[must_use]
80    pub fn build(self) -> OneOfSequentialFlow<Input, Output, Error> {
81        OneOfSequentialFlow {
82            nodes: Arc::new(self.nodes),
83        }
84    }
85}