Skip to main content

swf_builders/services/task/
for_loop.rs

1use super::*;
2
3// ============== ForTaskDefinitionBuilder ==============
4/// Builder for constructing a for task that iterates over a collection.
5#[derive(Default)]
6pub struct ForTaskDefinitionBuilder {
7    task: ForTaskDefinition,
8}
9
10impl ForTaskDefinitionBuilder {
11    pub fn new() -> Self {
12        Self::default()
13    }
14
15    /// Sets the iteration variable name (e.g., "item").
16    pub fn each(&mut self, each: &str) -> &mut Self {
17        self.task.for_.each = each.to_string();
18        self
19    }
20
21    /// Sets the collection expression to iterate over.
22    pub fn in_(&mut self, in_: &str) -> &mut Self {
23        self.task.for_.in_ = in_.to_string();
24        self
25    }
26
27    /// Sets the index variable name for the current iteration position.
28    pub fn at(&mut self, at: &str) -> &mut Self {
29        self.task.for_.at = Some(at.to_string());
30        self
31    }
32
33    /// Adds a named sub-task to execute on each iteration.
34    pub fn do_<F>(&mut self, name: &str, setup: F) -> &mut Self
35    where
36        F: FnOnce(&mut TaskDefinitionBuilder),
37    {
38        let mut builder = TaskDefinitionBuilder::new();
39        setup(&mut builder);
40        let task = builder.build();
41        self.task.do_.add(name.to_string(), task);
42        self
43    }
44
45    /// Sets a while condition to continue iteration.
46    pub fn while_(&mut self, while_expr: &str) -> &mut Self {
47        self.task.while_ = Some(while_expr.to_string());
48        self
49    }
50}
51
52impl_task_definition_builder_base!(ForTaskDefinitionBuilder, task, TaskDefinition::For);