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 SetProcessVars,
43}
44
45impl EventAction {
46 pub fn parse(v: &str) -> Result<Self> {
47 Self::from_str(v)
48 .map_err(|_| ActError::Action(format!("cannot find the action define '{v}'")))
49 }
50}
51
52impl<T, E> std::ops::Deref for Event<T, E>
53where
54 T: std::fmt::Debug + Clone,
55 E: std::fmt::Debug + Clone + Default,
56{
57 type Target = T;
58 fn deref(&self) -> &Self::Target {
59 &self.inner
60 }
61}
62
63impl<T, E> Event<T, E>
64where
65 T: std::fmt::Debug + Clone,
66 E: std::fmt::Debug + Clone + Default,
67{
68 pub fn inner(&self) -> &T {
69 &self.inner
70 }
71 pub fn new(_s: &Option<Arc<Runtime>>, inner: &T) -> Self {
72 Self {
73 #[cfg(test)]
74 runtime: _s.clone(),
75 extra: E::default(),
76 inner: inner.clone(),
77 }
78 }
79
80 pub fn new_with_extra(_rt: &Option<Arc<Runtime>>, inner: &T, extra: &E) -> Self {
81 Self {
82 #[cfg(test)]
83 runtime: _rt.clone(),
84 extra: extra.clone(),
85 inner: inner.clone(),
86 }
87 }
88
89 pub fn extra(&self) -> &E {
90 &self.extra
91 }
92
93 #[cfg(test)]
94 pub fn do_action(
95 &self,
96 pid: &str,
97 tid: &str,
98 action: EventAction,
99 options: &crate::Vars,
100 ) -> Result<()> {
101 if let Some(scher) = &self.runtime {
102 return scher.do_action(&Action::new(pid, tid, action, options));
103 }
104 Err(ActError::Action("scher is not define in Event".to_string()))
105 }
106}
107
108impl<T, E> std::fmt::Debug for Event<T, E>
109where
110 T: std::fmt::Debug + Clone,
111 E: std::fmt::Debug + Clone + Default,
112{
113 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
114 f.write_fmt(format_args!("{:?}", self.inner))
115 }
116}
117
118impl std::fmt::Display for EventAction {
119 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120 f.write_str(self.as_ref())
121 }
122}