ranvier_std/nodes/
debug.rs1use async_trait::async_trait;
2use ranvier_core::{bus::Bus, outcome::Outcome, transition::Transition};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::fmt::Debug;
6use std::marker::PhantomData;
7
8#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
9pub struct LogNode<T> {
10 pub message: String,
11 pub level: String,
12 #[serde(skip)]
13 pub _marker: PhantomData<T>,
14}
15
16impl<T> LogNode<T> {
17 pub fn new(message: impl Into<String>, level: impl Into<String>) -> Self {
18 Self {
19 message: message.into(),
20 level: level.into(),
21 _marker: PhantomData,
22 }
23 }
24}
25
26#[async_trait]
27impl<T> Transition<T, T> for LogNode<T>
28where
29 T: Debug + Send + Sync + 'static,
30{
31 type Error = String;
32 type Resources = ();
33
34 async fn run(
35 &self,
36 input: T,
37 _resources: &Self::Resources,
38 _bus: &mut Bus,
39 ) -> Outcome<T, Self::Error> {
40 match self.level.as_str() {
41 "error" => tracing::error!("{}: {:?}", self.message, input),
42 "warn" => tracing::warn!("{}: {:?}", self.message, input),
43 "debug" => tracing::debug!("{}: {:?}", self.message, input),
44 _ => tracing::info!("{}: {:?}", self.message, input),
45 }
46 Outcome::next(input)
47 }
48}
49
50#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
51pub struct ErrorNode<T> {
52 pub error_message: String,
53 #[serde(skip)]
54 pub _marker: PhantomData<T>,
55}
56
57impl<T> ErrorNode<T> {
58 pub fn new(error_message: impl Into<String>) -> Self {
59 Self {
60 error_message: error_message.into(),
61 _marker: PhantomData,
62 }
63 }
64}
65
66#[async_trait]
67impl<T> Transition<T, T> for ErrorNode<T>
68where
69 T: Send + Sync + 'static,
70{
71 type Error = String;
72 type Resources = ();
73
74 async fn run(
75 &self,
76 _input: T,
77 _resources: &Self::Resources,
78 _bus: &mut Bus,
79 ) -> Outcome<T, Self::Error> {
80 Outcome::fault(self.error_message.clone())
81 }
82}