acts_next/model/act/
each.rs

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