anchor_chain/link.rs
1//! A link in a processing chain that connects one `Node` to another.
2//!
3//! `Link` serves as a container for chaining two `Node` instances together,
4//! where the output of the first node is fed as the input to the next.
5
6use async_trait::async_trait;
7
8use crate::error::AnchorChainError;
9use crate::node::Node;
10
11/// A link in a processing chain that connects one `Node` to another.
12///
13/// `Link` serves as a container for chaining two `Node` instances together,
14/// where the output of the first node is fed as the input to the next.
15#[derive(Debug)]
16pub struct Link<C, N>
17where
18 C: std::fmt::Debug,
19 N: std::fmt::Debug,
20{
21 /// The first node in the chain.
22 pub node: C,
23 /// The next node or link in the chain.
24 pub next: N,
25}
26
27impl<C, N> Link<C, N>
28where
29 C: std::fmt::Debug,
30 N: std::fmt::Debug,
31{
32 /// Creates a new `Link` connecting the specified nodes.
33 ///
34 /// The `node` is linked with the `next` node in the chain. Output from the
35 /// `node` is passed as input to the `next` node. Either node can also be
36 /// a `Link` forming a nested linked list of nodes.
37 pub fn new(node: C, next: N) -> Self {
38 Link { node, next }
39 }
40}
41
42#[async_trait]
43impl<C, N> Node for Link<C, N>
44where
45 C: Node + Send + Sync + std::fmt::Debug,
46 C::Output: Send + 'static,
47 C::Input: Send,
48 N: Node<Input = C::Output> + Send + Sync + std::fmt::Debug,
49 N::Output: Send,
50{
51 /// The input type for the current node
52 type Input = C::Input;
53 /// The output type of the next node
54 type Output = <N as Node>::Output;
55
56 /// Processes the given input through the chain of nodes.
57 ///
58 /// First, the input is processed by the current node. Then, the output of the current
59 /// node is passed to the next node or link in the chain for further processing.
60 async fn process(&self, input: Self::Input) -> Result<Self::Output, AnchorChainError> {
61 let output = self.node.process(input).await?;
62 self.next.process(output).await
63 }
64}