Skip to main content

swf_core/models/task/
for_task.rs

1use serde::{Deserialize, Serialize};
2
3use super::{Map, TaskDefinition, TaskDefinitionFields};
4use crate::models::input::InputDataModelDefinition;
5
6/// Represents the definition of a task that executes a set of subtasks iteratively for each element in a collection
7#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
8pub struct ForTaskDefinition {
9    /// Gets/sets the definition of the loop that iterates over a range of values
10    #[serde(rename = "for")]
11    pub for_: ForLoopDefinition,
12
13    /// Gets/sets a runtime expression that represents the condition, if any, that must be met for the iteration to continue
14    #[serde(rename = "while", skip_serializing_if = "Option::is_none")]
15    pub while_: Option<String>,
16
17    /// Gets/sets the tasks to perform for each item in the collection
18    #[serde(rename = "do")]
19    pub do_: Map<String, TaskDefinition>,
20
21    /// Gets/sets the task's common fields
22    #[serde(flatten)]
23    pub common: TaskDefinitionFields,
24}
25impl ForTaskDefinition {
26    /// Initializes a new ForTaskDefinition
27    pub fn new(
28        for_: ForLoopDefinition,
29        do_: Map<String, TaskDefinition>,
30        while_: Option<String>,
31    ) -> Self {
32        Self {
33            for_,
34            while_,
35            do_,
36            common: TaskDefinitionFields::new(),
37        }
38    }
39}
40
41/// Represents the definition of a loop that iterates over a range of values
42#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
43pub struct ForLoopDefinition {
44    /// Gets/sets the name of the variable that represents each element in the collection during iteration
45    #[serde(rename = "each")]
46    pub each: String,
47
48    /// Gets/sets the runtime expression used to get the collection to iterate over
49    #[serde(rename = "in")]
50    pub in_: String,
51
52    /// Gets/sets the name of the variable used to hold the index of each element in the collection during iteration
53    #[serde(rename = "at", skip_serializing_if = "Option::is_none")]
54    pub at: Option<String>,
55
56    /// Gets/sets the definition of the data, if any, to pass to iterations to run
57    #[serde(rename = "input", skip_serializing_if = "Option::is_none")]
58    pub input: Option<InputDataModelDefinition>,
59}
60impl ForLoopDefinition {
61    pub fn new(
62        each: &str,
63        in_: &str,
64        at: Option<String>,
65        input: Option<InputDataModelDefinition>,
66    ) -> Self {
67        Self {
68            each: each.to_string(),
69            in_: in_.to_string(),
70            at,
71            input,
72        }
73    }
74}