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