1mod block;
2mod call;
3mod catch;
4mod chain;
5mod r#do;
6mod each;
7mod r#if;
8mod irq;
9mod msg;
10mod pack;
11mod timeout;
12
13use crate::{ModelBase, StmtBuild, Vars};
14pub use block::Block;
15pub use call::Call;
16pub use catch::Catch;
17pub use chain::Chain;
18pub use each::Each;
19pub use irq::Irq;
20pub use msg::Msg;
21pub use pack::Pack;
22pub use r#do::Do;
23pub use r#if::If;
24use serde::{Deserialize, Serialize};
25
26#[allow(unused_imports)]
27pub use timeout::{Timeout, TimeoutLimit, TimeoutUnit};
28
29#[derive(Debug, Default, Clone, Serialize, Deserialize)]
30pub struct Act {
31 #[serde(default)]
32 pub id: String,
33
34 #[serde(default)]
35 pub name: String,
36
37 #[serde(default, rename = "act")]
38 pub act: String,
39
40 #[serde(default)]
42 pub key: String,
43
44 #[serde(default)]
45 pub tag: String,
46
47 #[serde(default)]
49 pub r#in: String,
50
51 #[serde(default)]
53 pub on: String,
54
55 #[serde(default)]
57 pub inputs: Vars,
58
59 #[serde(default)]
60 pub rets: Vars,
61
62 #[serde(default)]
63 pub outputs: Vars,
64
65 #[serde(default)]
66 pub setup: Vec<Act>,
67
68 #[serde(default)]
70 pub then: Vec<Act>,
71
72 #[serde(default)]
74 pub r#else: Vec<Act>,
75
76 #[serde(default)]
78 pub next: Option<Box<Act>>,
79
80 #[serde(default)]
81 pub catches: Vec<Catch>,
82
83 #[serde(default)]
84 pub timeout: Vec<Timeout>,
85}
86
87#[derive(Debug, Clone, Serialize, Deserialize)]
88pub enum ActFn {
89 None,
90
91 #[serde(rename = "set")]
92 Set(Vars),
93
94 #[serde(rename = "expose")]
95 Expose(Vars),
96
97 #[serde(rename = "irq")]
98 Irq(Irq),
99
100 #[serde(rename = "msg")]
101 Msg(Msg),
102
103 #[serde(rename = "cmd")]
104 Cmd(Do),
105
106 #[serde(rename = "each")]
107 Each(Each),
108
109 #[serde(rename = "chain")]
110 Chain(Chain),
111
112 #[serde(rename = "block")]
113 Block(Block),
114
115 #[serde(rename = "if")]
116 If(If),
117
118 #[serde(rename = "call")]
119 Call(Call),
120
121 #[serde(rename = "pack")]
122 Pack(Pack),
123
124 #[serde(rename = "on_created")]
125 OnCreated(Vec<Act>),
126
127 #[serde(rename = "on_timeout")]
128 OnTimeout(Vec<Timeout>),
129
130 #[serde(rename = "on_updated")]
131 OnUpdated(Vec<Act>),
132
133 #[serde(rename = "on_before_update")]
134 OnBeforeUpdate(Vec<Act>),
135
136 #[serde(rename = "on_step")]
137 OnStep(Vec<Act>),
138
139 #[serde(rename = "on_completed")]
140 OnCompleted(Vec<Act>),
141
142 #[serde(rename = "on_catch")]
143 OnErrorCatch(Vec<Catch>),
144}
145
146impl ModelBase for Act {
147 fn id(&self) -> &str {
148 &self.id
149 }
150}
151
152impl<T> StmtBuild<T> for Vec<T> {
153 fn add(mut self, s: T) -> Self {
154 self.push(s);
155 self
156 }
157
158 fn with<F: Fn(T) -> T>(mut self, build: F) -> Self
159 where
160 T: Default,
161 {
162 self.push(build(T::default()));
163 self
164 }
165}
166
167impl Act {
168 pub fn is_taskable(&self) -> bool {
169 let act_fn = self.into();
170 matches!(
171 act_fn,
172 ActFn::Pack(_) | ActFn::Irq(_) | ActFn::Block(_) | ActFn::Call(_)
173 )
174 }
175
176 pub fn new() -> Self {
177 Default::default()
178 }
179
180 pub fn with_act(mut self, act: &str) -> Self {
181 self.act = act.to_string();
182 self
183 }
184
185 pub fn with_id(mut self, id: &str) -> Self {
186 self.id = id.to_string();
187 self
188 }
189
190 pub fn with_name(mut self, name: &str) -> Self {
191 self.name = name.to_string();
192 self
193 }
194
195 pub fn with_key(mut self, key: &str) -> Self {
196 self.key = key.to_string();
197 self
198 }
199
200 pub fn with_tag(mut self, tag: &str) -> Self {
201 self.tag = tag.to_string();
202 self
203 }
204
205 pub fn with_input<T>(mut self, name: &str, value: T) -> Self
206 where
207 T: Serialize + Clone,
208 {
209 self.inputs.set(name, value);
210 self
211 }
212
213 pub fn with_ret<T>(mut self, name: &str, value: T) -> Self
214 where
215 T: Serialize + Clone,
216 {
217 self.rets.set(name, value);
218 self
219 }
220
221 pub fn with_then(mut self, build: fn(Vec<Act>) -> Vec<Act>) -> Self {
222 let stmts = Vec::new();
223 self.then = build(stmts);
224 self
225 }
226
227 pub fn with_next<F: Fn(Act) -> Act>(mut self, build: F) -> Self {
228 self.next = Some(Box::new(build(Act::default())));
229 self
230 }
231
232 pub fn with_setup(mut self, build: fn(Vec<Act>) -> Vec<Act>) -> Self {
233 let stmts = Vec::new();
234 self.setup = build(stmts);
235 self
236 }
237
238 pub fn with_in(mut self, expr: &str) -> Self {
239 self.r#in = expr.to_string();
240 self
241 }
242
243 pub fn with_on(mut self, expr: &str) -> Self {
244 self.on = expr.to_string();
245 self
246 }
247
248 pub fn with_catch(mut self, build: fn(Catch) -> Catch) -> Self {
249 let catch = Catch::default();
250 self.catches.push(build(catch));
251 self
252 }
253
254 pub fn with_timeout(mut self, build: fn(Timeout) -> Timeout) -> Self {
255 let timeout = Timeout::default();
256 self.timeout.push(build(timeout));
257 self
258 }
259
260 pub fn set(var: Vars) -> Self {
261 Act {
262 inputs: var,
263 act: "set".to_string(),
264 ..Default::default()
265 }
266 }
267
268 pub fn expose(var: Vars) -> Self {
269 Act {
270 inputs: var,
271 act: "expose".to_string(),
272 ..Default::default()
273 }
274 }
275
276 pub fn irq<F: Fn(Irq) -> Irq>(build: F) -> Self {
277 let req = build(Irq::default());
278 Act {
279 act: "irq".to_string(),
280 inputs: req.inputs,
281 tag: req.tag,
282 key: req.key,
283 rets: req.rets,
284 outputs: req.outputs,
285 ..Default::default()
286 }
287 }
288
289 pub fn msg<F: Fn(Msg) -> Msg>(build: F) -> Self {
290 let msg = build(Msg::default());
291 Act {
292 inputs: msg.inputs,
293 tag: msg.tag,
294 key: msg.key,
295 act: "msg".to_string(),
296 ..Default::default()
297 }
298 }
299
300 pub fn r#if<F: Fn(If) -> If>(build: F) -> Self {
301 let cond = build(If::default());
302 Act {
303 on: cond.on,
304 then: cond.then,
305 r#else: cond.r#else,
306 act: "if".to_string(),
307 ..Default::default()
308 }
309 }
310
311 pub fn each<F: Fn(Each) -> Each>(build: F) -> Self {
312 let each = build(Each::default());
313 Act {
314 r#in: each.r#in,
315 then: each.then,
316 act: "each".to_string(),
317 ..Default::default()
318 }
319 }
320
321 pub fn call<F: Fn(Call) -> Call>(build: F) -> Self {
322 let call = build(Call::default());
323 Act {
324 key: call.key,
325 inputs: call.inputs,
326 rets: call.rets,
327 act: "call".to_string(),
328 ..Default::default()
329 }
330 }
331
332 pub fn chain<F: Fn(Chain) -> Chain>(build: F) -> Self {
333 let chain = build(Chain::default());
334 Act {
335 r#in: chain.r#in,
336 then: chain.then,
337 act: "chain".to_string(),
338 ..Default::default()
339 }
340 }
341
342 pub fn cmd<F: Fn(Do) -> Do>(build: F) -> Self {
343 let cmd = build(Do::default());
344 Act {
345 inputs: cmd.inputs,
346 key: cmd.key,
347 act: "cmd".to_string(),
348 ..Default::default()
349 }
350 }
351
352 pub fn block<F: Fn(Block) -> Block>(build: F) -> Self {
353 let block = build(Block::default());
354 Act {
355 inputs: block.inputs,
356 then: block.then,
357 next: block.next,
358 act: "block".to_string(),
359 ..Default::default()
360 }
361 }
362
363 pub fn pack<F: Fn(Pack) -> Pack>(build: F) -> Self {
364 let pack = build(Pack::default());
365
366 Act {
367 act: "pack".to_string(),
368 key: pack.key,
369 inputs: pack.inputs,
370 outputs: pack.outputs,
371 ..Default::default()
372 }
373 }
374
375 pub fn catch<F: Fn(Catch) -> Catch>(build: F) -> Self {
376 let c = build(Catch::default());
377
378 Act {
379 act: "catch".to_string(),
380 on: match c.on {
381 Some(on) => on,
382 None => "".to_string(),
383 },
384 inputs: c.inputs,
385 then: c.then,
386 ..Default::default()
387 }
388 }
389
390 pub fn timeout<F: Fn(Timeout) -> Timeout>(build: F) -> Self {
391 let timeout = build(Timeout::default());
392
393 Act {
394 act: "timeout".to_string(),
395 on: timeout.on.to_string(),
396 then: timeout.then,
397 ..Default::default()
398 }
399 }
400
401 pub fn on_created(build: fn(Vec<Act>) -> Vec<Act>) -> Self {
402 let stmts = build(Vec::new());
403 Act {
404 act: "on_created".to_string(),
405 then: stmts,
406 ..Default::default()
407 }
408 }
409
410 pub fn on_completed(build: fn(Vec<Act>) -> Vec<Act>) -> Self {
411 let stmts = build(Vec::new());
412 Act {
413 act: "on_completed".to_string(),
414 then: stmts,
415 ..Default::default()
416 }
417 }
418
419 pub fn on_before_update(build: fn(Vec<Act>) -> Vec<Act>) -> Self {
420 let stmts = build(Vec::new());
421 Act {
422 act: "on_before_update".to_string(),
423 then: stmts,
424 ..Default::default()
425 }
426 }
427
428 pub fn on_updated(build: fn(Vec<Act>) -> Vec<Act>) -> Self {
429 let stmts = build(Vec::new());
430 Act {
431 act: "on_updated".to_string(),
432 then: stmts,
433 ..Default::default()
434 }
435 }
436
437 pub fn on_step(build: fn(Vec<Act>) -> Vec<Act>) -> Self {
438 let stmts = build(Vec::new());
439 Act {
440 act: "on_step".to_string(),
441 then: stmts,
442 ..Default::default()
443 }
444 }
445
446 pub fn on_catch(build: fn(Vec<Catch>) -> Vec<Catch>) -> Self {
447 let stmts = build(Vec::new());
448 Act {
449 act: "on_catch".to_string(),
450 catches: stmts,
451 ..Default::default()
452 }
453 }
454
455 pub fn on_timeout(build: fn(Vec<Timeout>) -> Vec<Timeout>) -> Self {
456 let stmts = build(Vec::new());
457 Act {
458 act: "on_timeout".to_string(),
459 timeout: stmts,
460 ..Default::default()
461 }
462 }
463}
464
465impl From<&Act> for ActFn {
466 fn from(act: &Act) -> Self {
467 let fn_name = act.act.as_str();
468 match fn_name {
469 "set" => ActFn::Set(act.inputs.clone()),
470 "expose" => ActFn::Expose(act.inputs.clone()),
471 "irq" => {
472 let irq = Irq {
473 tag: act.tag.clone(),
474 key: act.key.clone(),
475 inputs: act.inputs.clone(),
476 rets: act.rets.clone(),
477 ..Default::default()
478 };
479 ActFn::Irq(irq)
480 }
481 "msg" => {
482 let msg = Msg {
483 tag: act.tag.clone(),
484 key: act.key.clone(),
485 inputs: act.inputs.clone(),
486 };
487 ActFn::Msg(msg)
488 }
489 "cmd" => {
490 let cmd = Do {
491 key: act.key.clone(),
492 inputs: act.inputs.clone(),
493 };
494 ActFn::Cmd(cmd)
495 }
496 "each" => {
497 let each = Each {
498 r#in: act.r#in.clone(),
499 then: act.then.clone(),
500 };
501 ActFn::Each(each)
502 }
503 "chain" => {
504 let chain = Chain {
505 r#in: act.r#in.clone(),
506 then: act.then.clone(),
507 };
508 ActFn::Chain(chain)
509 }
510 "block" => {
511 let block = Block {
512 then: act.then.clone(),
513 inputs: act.inputs.clone(),
514 next: act.next.clone(),
515 };
516 ActFn::Block(block)
517 }
518 "if" => {
519 let r#if = If {
520 on: act.on.clone(),
521 then: act.then.clone(),
522 r#else: act.r#else.clone(),
523 };
524 ActFn::If(r#if)
525 }
526 "call" => {
527 let call = Call {
528 key: act.key.clone(),
529 inputs: act.inputs.clone(),
530 rets: act.rets.clone(),
531 };
532 ActFn::Call(call)
533 }
534 "pack" => {
535 let pack = Pack {
536 key: act.key.clone(),
537 inputs: act.inputs.clone(),
538 outputs: act.rets.clone(),
539 };
540 ActFn::Pack(pack)
541 }
542 "on_created" => ActFn::OnCreated(act.then.clone()),
543 "on_timeout" => ActFn::OnTimeout(act.timeout.clone()),
544 "on_updated" => ActFn::OnUpdated(act.then.clone()),
545 "on_before_update" => ActFn::OnBeforeUpdate(act.then.clone()),
546 "on_step" => ActFn::OnStep(act.then.clone()),
547 "on_completed" => ActFn::OnCompleted(act.then.clone()),
548 "on_catch" => ActFn::OnErrorCatch(act.catches.clone()),
549 _ => ActFn::None,
550 }
551 }
552}