acts_next/model/act/
chain.rs

1use crate::{Act, ActError, Context, Result};
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Default, Clone, Serialize, Deserialize)]
5pub struct Chain {
6    #[serde(default)]
7    pub r#in: String,
8    pub then: Vec<Act>,
9}
10
11impl Chain {
12    pub fn parse(&self, ctx: &Context, scr: &str) -> Result<Vec<String>> {
13        if scr.is_empty() {
14            return Err(ActError::Runtime("chain's 'in' is empty".to_string()));
15        }
16        let result = ctx.eval::<Vec<String>>(scr)?;
17        if result.is_empty() {
18            return Err(ActError::Runtime(format!(
19                "chain.in is empty in task({})",
20                ctx.task().id
21            )));
22        }
23        Ok(result)
24    }
25
26    pub fn new() -> Self {
27        Default::default()
28    }
29
30    pub fn with_in(mut self, scr: &str) -> Self {
31        self.r#in = scr.to_string();
32        self
33    }
34
35    pub fn with_then(mut self, build: fn(Vec<Act>) -> Vec<Act>) -> Self {
36        let stmts = Vec::new();
37        self.then = build(stmts);
38        self
39    }
40}
41
42impl From<Chain> for Act {
43    fn from(val: Chain) -> Self {
44        Act::chain(|_| val.clone())
45    }
46}