anchor_chain/nodes/
logger.rs

1//! A simple input logging node.
2//!
3//! This node logs the input to the console and passes it through unchanged.
4use crate::error::AnchorChainError;
5use crate::node::Node;
6#[cfg(feature = "tracing")]
7use tracing::instrument;
8
9/// A simple input logging node
10#[derive(Debug)]
11pub struct Logger<T> {
12    prefix: String,
13    _marker: std::marker::PhantomData<T>,
14}
15
16impl<T> Logger<T> {
17    /// Create a new Logger node with the given prefix.
18    ///
19    /// The prefix is prepended to the input in the format `prefix: input`.
20    pub fn new(prefix: String) -> Self {
21        Self {
22            prefix,
23            _marker: std::marker::PhantomData,
24        }
25    }
26}
27
28#[async_trait::async_trait]
29impl<T> Node for Logger<T>
30where
31    T: std::fmt::Debug + Send + Sync,
32{
33    type Input = T;
34    type Output = T;
35
36    /// Log the input and pass it through unchanged.
37    #[cfg_attr(feature = "tracing", instrument)]
38    async fn process(&self, input: Self::Input) -> Result<Self::Output, AnchorChainError> {
39        println!("{}: {:?}", self.prefix, input);
40        Ok(input)
41    }
42}
43
44impl<T> Default for Logger<T> {
45    fn default() -> Self {
46        Self {
47            prefix: "Input".to_string(),
48            _marker: std::marker::PhantomData,
49        }
50    }
51}