hcl/expr/for_expr.rs
1use super::Expression;
2use crate::Identifier;
3use serde::Deserialize;
4
5/// A for expression is a construct for constructing a collection by projecting the items from
6/// another collection.
7#[derive(Deserialize, Debug, PartialEq, Eq, Clone)]
8pub struct ForExpr {
9 /// Optional name of the variable that will be temporarily assigned the key of each element
10 /// during iteration. If the source collection is an array, it gets assigned the zero-based
11 /// array index. For an object source collection, this gets assigned the object's key.
12 pub key_var: Option<Identifier>,
13 /// The name of the variable that will be temporarily assigned the value of each element
14 /// during iteration.
15 pub value_var: Identifier,
16 /// An expression that must evaluate to a value that can be iterated.
17 pub collection_expr: Expression,
18 /// An expression that is evaluated once for each key in the source collection. If set, the
19 /// result of the `for` expression will be an object. Otherwise, the result will be an array.
20 pub key_expr: Option<Expression>,
21 /// An expression that is evaluated once for each value in the source collection.
22 pub value_expr: Expression,
23 /// Indicates whether grouping mode is enabled. In grouping mode, each value in the resulting
24 /// object is a list of all of the values that were produced against each distinct key. This is
25 /// ignored if `key_expr` is `None`.
26 pub grouping: bool,
27 /// An optional filter expression. Elements for which the condition evaluates to `true` will
28 /// be evaluated as normal, while if `false` the element will be skipped.
29 pub cond_expr: Option<Expression>,
30}
31
32impl ForExpr {
33 /// Create a new `ForExpr` with the name of the variable that will be temporarily assigned the
34 /// value of each element during iteration, an expression that must evaluate to a value that
35 /// can be iterated, and one expression that is evaluated once for each value in the source
36 /// collection.
37 pub fn new<C, V>(value_var: Identifier, collection_expr: C, value_expr: V) -> ForExpr
38 where
39 C: Into<Expression>,
40 V: Into<Expression>,
41 {
42 ForExpr {
43 key_var: None,
44 value_var,
45 collection_expr: collection_expr.into(),
46 key_expr: None,
47 value_expr: value_expr.into(),
48 grouping: false,
49 cond_expr: None,
50 }
51 }
52
53 /// Adds the iterator key variable identifier to the `for` expression and returns the modified
54 /// `ForExpr`.
55 pub fn with_key_var(mut self, key_var: Identifier) -> ForExpr {
56 self.key_var = Some(key_var);
57 self
58 }
59
60 /// Adds an expression that is evaluated once for each key in the source collection. If set,
61 /// the result of the `for` expression will be an object. Returns the modified `ForExpr`.
62 pub fn with_key_expr<T>(mut self, key_expr: T) -> ForExpr
63 where
64 T: Into<Expression>,
65 {
66 self.key_expr = Some(key_expr.into());
67 self
68 }
69
70 /// Sets the filter expression. Elements for which the condition evaluates to `true` will be
71 /// evaluated as normal, while if `false` the element will be skipped.
72 pub fn with_cond_expr<T>(mut self, cond_expr: T) -> ForExpr
73 where
74 T: Into<Expression>,
75 {
76 self.cond_expr = Some(cond_expr.into());
77 self
78 }
79
80 /// Enables or disabled grouping mode.
81 pub fn with_grouping(mut self, yes: bool) -> ForExpr {
82 self.grouping = yes;
83 self
84 }
85}