Skip to main content

acts_next/model/
act.rs

1mod catch;
2mod retry;
3mod timeout;
4
5pub use catch::Catch;
6pub use retry::Retry;
7
8use crate::{ModelBase, StmtBuild, Vars};
9use serde::{Deserialize, Serialize};
10use serde_json::Value as JsonValue;
11
12#[allow(unused_imports)]
13pub use timeout::{Timeout, TimeoutLimit, TimeoutUnit};
14
15use super::ActEvent;
16
17#[derive(Debug, Default, Clone, Serialize, Deserialize)]
18pub struct Act {
19    #[serde(default)]
20    pub id: String,
21
22    #[serde(default)]
23    pub name: String,
24
25    #[serde(default)]
26    pub desc: String,
27
28    // to use a package, such as 'acts.transform.set'
29    #[serde(default)]
30    pub uses: String,
31
32    // package params
33    #[serde(default)]
34    pub params: JsonValue,
35
36    // package extra options
37    // such as ACT_INDEX, ACT_VALUE
38    #[serde(default)]
39    pub options: Vars,
40
41    #[serde(default)]
42    pub r#if: Option<String>,
43
44    /// act key for req and msg
45    #[serde(default)]
46    pub key: String,
47
48    #[serde(default)]
49    pub tag: String,
50
51    /// on event for 'created', 'completed'
52    #[serde(default)]
53    pub on: Option<ActEvent>,
54
55    /// act arguments
56    #[serde(default)]
57    pub inputs: Vars,
58
59    #[serde(default)]
60    pub outputs: Vars,
61
62    #[serde(default)]
63    pub setup: Vec<Act>,
64
65    #[serde(default)]
66    pub catches: Vec<Catch>,
67
68    #[serde(default)]
69    pub timeout: Vec<Timeout>,
70}
71
72impl ModelBase for Act {
73    fn id(&self) -> &str {
74        &self.id
75    }
76}
77
78impl<T> StmtBuild<T> for Vec<T> {
79    fn add(mut self, s: T) -> Self {
80        self.push(s);
81        self
82    }
83
84    fn with<F: Fn(T) -> T>(mut self, build: F) -> Self
85    where
86        T: Default,
87    {
88        self.push(build(T::default()));
89        self
90    }
91}
92
93impl Act {
94    pub fn new() -> Self {
95        Default::default()
96    }
97
98    pub fn with_uses(mut self, pack: &str) -> Self {
99        self.uses = pack.to_string();
100        self
101    }
102
103    pub fn with_id(mut self, id: &str) -> Self {
104        self.id = id.to_string();
105        self
106    }
107
108    pub fn with_name(mut self, name: &str) -> Self {
109        self.name = name.to_string();
110        self
111    }
112
113    pub fn with_key(mut self, key: &str) -> Self {
114        self.key = key.to_string();
115        self
116    }
117
118    pub fn with_tag(mut self, tag: &str) -> Self {
119        self.tag = tag.to_string();
120        self
121    }
122
123    pub fn with_input<T>(mut self, name: &str, value: T) -> Self
124    where
125        T: Serialize + Clone,
126    {
127        self.inputs.set(name, value);
128        self
129    }
130
131    pub fn with_params_data(mut self, v: JsonValue) -> Self {
132        self.params = v;
133        self
134    }
135
136    pub fn with_params_vars<F: Fn(Vars) -> Vars>(mut self, build: F) -> Self {
137        let vars = build(Vars::default());
138        self.params = vars.into();
139
140        self
141    }
142
143    #[cfg(test)]
144    pub fn with_input_acts<T>(mut self, name: &str, f: fn(&mut Vec<T>)) -> Self
145    where
146        T: Serialize + Clone,
147    {
148        let mut vec = Vec::new();
149        f(&mut vec);
150        self.inputs.set(name, vec);
151        self
152    }
153
154    pub fn with_output(mut self, name: &str, value: JsonValue) -> Self {
155        self.outputs.insert(name.to_string(), value);
156        self
157    }
158
159    pub fn with_setup(mut self, build: fn(Vec<Act>) -> Vec<Act>) -> Self {
160        let stmts = Vec::new();
161        self.setup = build(stmts);
162        self
163    }
164
165    pub fn with_on(mut self, event: ActEvent) -> Self {
166        self.on = Some(event);
167        self
168    }
169
170    pub fn with_catch(mut self, build: fn(Catch) -> Catch) -> Self {
171        let catch = Catch::default();
172        self.catches.push(build(catch));
173        self
174    }
175
176    pub fn with_timeout(mut self, build: fn(Timeout) -> Timeout) -> Self {
177        let timeout = Timeout::default();
178        self.timeout.push(build(timeout));
179        self
180    }
181
182    pub fn with_if(mut self, v: &str) -> Self {
183        self.r#if = Some(v.to_string());
184        self
185    }
186
187    #[cfg(test)]
188    pub fn set(params: Vars) -> Self {
189        Act {
190            params: params.into(),
191            uses: "acts.transform.set".to_string(),
192            ..Default::default()
193        }
194    }
195
196    pub fn irq<T: Fn(Act) -> Act>(build: T) -> Self {
197        let act = build(Act::default());
198        Act {
199            uses: "acts.core.irq".to_string(),
200            ..act
201        }
202    }
203
204    pub fn msg<T: Fn(Act) -> Act>(build: T) -> Self {
205        let act = build(Act::default());
206        Act {
207            uses: "acts.core.msg".to_string(),
208            ..act
209        }
210    }
211
212    pub fn parallel(params: JsonValue) -> Self {
213        Act {
214            params,
215            uses: "acts.core.parallel".to_string(),
216            ..Default::default()
217        }
218    }
219
220    pub fn subflow(params: JsonValue) -> Self {
221        Act {
222            params,
223            uses: "acts.core.subflow".to_string(),
224            ..Default::default()
225        }
226    }
227
228    pub fn sequence(params: JsonValue) -> Self {
229        Act {
230            params,
231            uses: "acts.core.sequence".to_string(),
232            ..Default::default()
233        }
234    }
235
236    pub fn action(params: Vars) -> Self {
237        Act {
238            params: params.into(),
239            uses: "acts.core.action".to_string(),
240            ..Default::default()
241        }
242    }
243
244    pub fn block(params: Vars) -> Self {
245        Act {
246            params: params.into(),
247            uses: "acts.core.block".to_string(),
248            ..Default::default()
249        }
250    }
251
252    pub fn code(code: &str) -> Self {
253        Act {
254            params: code.into(),
255            uses: "acts.transform.code".to_string(),
256            ..Default::default()
257        }
258    }
259}