dagrs/node/
action.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4
5use crate::{
6    connection::{in_channel::InChannels, out_channel::OutChannels},
7    utils::{env::EnvVar, output::Output},
8};
9
10/// Node specific behavior
11///
12/// [`Action`] stores the specific execution logic of a task.
13///
14/// # Example
15/// An implementation of [`Action`]: `HelloAction`, having private
16/// fields `statement` and `repeat`.
17///
18/// ```rust
19/// use std::sync::Arc;
20/// use dagrs::{Action, EnvVar, Output, InChannels, OutChannels};
21/// use async_trait::async_trait;
22///
23/// struct HelloAction{
24///    statement: String,
25///    repeat: usize,
26/// }
27///
28/// #[async_trait]
29/// impl Action for HelloAction{
30///     async fn run(&self, _: &mut InChannels, _: &mut OutChannels, _: Arc<EnvVar>) -> Output{
31///         for i in 0..self.repeat {
32///             println!("{}",self.statement);
33///         }
34///         Output::empty()
35///     }
36/// }
37///
38/// let hello=HelloAction {
39///     statement: "hello world!".to_string(),
40///     repeat: 10
41/// };
42///
43/// ```
44#[async_trait]
45pub trait Action: Send + Sync {
46    async fn run(
47        &self,
48        in_channels: &mut InChannels,
49        out_channels: &mut OutChannels,
50        env: Arc<EnvVar>,
51    ) -> Output;
52}
53
54/// An empty implementaion of [`Action`].
55///
56/// Used as a placeholder when creating a `Node` without `Action`.
57pub struct EmptyAction;
58#[async_trait]
59impl Action for EmptyAction {
60    async fn run(&self, _: &mut InChannels, _: &mut OutChannels, _: Arc<EnvVar>) -> Output {
61        Output::Out(None)
62    }
63}