Skip to main content

acts_next/event/
mod.rs

1mod action;
2mod emitter;
3mod extra;
4mod message;
5
6#[cfg(test)]
7mod tests;
8
9use crate::{ActError, Result, scheduler::Runtime};
10pub use action::Action;
11pub use emitter::Emitter;
12pub use extra::TaskExtra;
13pub use message::{Message, MessageState, Model};
14
15use serde::{Deserialize, Serialize};
16use std::str::FromStr;
17use std::sync::Arc;
18
19#[derive(Clone)]
20pub struct Event<T, E = ()> {
21    inner: T,
22    extra: E,
23    #[cfg(test)]
24    pub(crate) runtime: Option<Arc<Runtime>>,
25}
26
27#[derive(
28    Serialize, Deserialize, Debug, Clone, Default, PartialEq, strum::AsRefStr, strum::EnumString,
29)]
30#[serde(rename_all = "snake_case")]
31#[strum(serialize_all = "snake_case")]
32pub enum EventAction {
33    #[default]
34    Next,
35    Submit,
36    Back,
37    Cancel,
38    Abort,
39    Skip,
40    Error,
41    Push,
42    Remove,
43    SetVars,
44    SetProcessVars,
45}
46
47impl EventAction {
48    pub fn parse(v: &str) -> Result<Self> {
49        Self::from_str(v)
50            .map_err(|_| ActError::Action(format!("cannot find the action define '{v}'")))
51    }
52}
53
54impl<T, E> std::ops::Deref for Event<T, E>
55where
56    T: std::fmt::Debug + Clone,
57    E: std::fmt::Debug + Clone + Default,
58{
59    type Target = T;
60    fn deref(&self) -> &Self::Target {
61        &self.inner
62    }
63}
64
65impl<T, E> Event<T, E>
66where
67    T: std::fmt::Debug + Clone,
68    E: std::fmt::Debug + Clone + Default,
69{
70    pub fn inner(&self) -> &T {
71        &self.inner
72    }
73    pub fn new(_s: &Option<Arc<Runtime>>, inner: &T) -> Self {
74        Self {
75            #[cfg(test)]
76            runtime: _s.clone(),
77            extra: E::default(),
78            inner: inner.clone(),
79        }
80    }
81
82    pub fn new_with_extra(_rt: &Option<Arc<Runtime>>, inner: &T, extra: &E) -> Self {
83        Self {
84            #[cfg(test)]
85            runtime: _rt.clone(),
86            extra: extra.clone(),
87            inner: inner.clone(),
88        }
89    }
90
91    pub fn extra(&self) -> &E {
92        &self.extra
93    }
94
95    #[cfg(test)]
96    pub fn do_action(
97        &self,
98        pid: &str,
99        tid: &str,
100        action: EventAction,
101        options: &crate::Vars,
102    ) -> Result<()> {
103        if let Some(scher) = &self.runtime {
104            return scher.do_action(&Action::new(pid, tid, action, options));
105        }
106        Err(ActError::Action("scher is not define in Event".to_string()))
107    }
108}
109
110impl<T, E> std::fmt::Debug for Event<T, E>
111where
112    T: std::fmt::Debug + Clone,
113    E: std::fmt::Debug + Clone + Default,
114{
115    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116        f.write_fmt(format_args!("{:?}", self.inner))
117    }
118}
119
120impl std::fmt::Display for EventAction {
121    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
122        f.write_str(self.as_ref())
123    }
124}