1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::action::{Action, Props, StatefulAction, INFINITE};
use crate::comm::{QWriter, Signal};
use crate::resource::{IoManager, LoggerSignal, ResourceManager};
use crate::server::{AsyncSignal, Config, State, SyncSignal};
use eyre::Result;
use serde::{Deserialize, Serialize};
use serde_cbor::Value;

#[derive(Debug, Deserialize, Serialize)]
pub struct Event(String);

stateful!(Event { name: String });

impl Action for Event {
    #[inline(always)]
    fn stateful(
        &self,
        _io: &IoManager,
        _res: &ResourceManager,
        _config: &Config,
        _sync_writer: &QWriter<SyncSignal>,
        _async_writer: &QWriter<AsyncSignal>,
    ) -> Result<Box<dyn StatefulAction>> {
        Ok(Box::new(StatefulEvent {
            done: false,
            name: self.0.clone(),
        }))
    }
}

impl StatefulAction for StatefulEvent {
    impl_stateful!();

    fn props(&self) -> Props {
        INFINITE.into()
    }

    fn start(
        &mut self,
        _sync_writer: &mut QWriter<SyncSignal>,
        async_writer: &mut QWriter<AsyncSignal>,
        _state: &State,
    ) -> Result<Signal> {
        async_writer.push(LoggerSignal::Append(
            "event".to_owned(),
            (self.name.clone(), Value::Text("start".to_owned())),
        ));
        Ok(Signal::none())
    }

    fn stop(
        &mut self,
        _sync_writer: &mut QWriter<SyncSignal>,
        async_writer: &mut QWriter<AsyncSignal>,
        _state: &State,
    ) -> Result<Signal> {
        async_writer.push(LoggerSignal::Append(
            "event".to_owned(),
            (self.name.clone(), Value::Text("stop".to_owned())),
        ));

        self.done = true;
        Ok(Signal::none())
    }
}