hcl_edit/expr/
for_expr.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::expr::Expression;
use crate::{Decor, Decorate, Decorated, Ident};
use std::ops::Range;

/// A for expression is a construct for constructing a collection by projecting the items from
/// another collection.
#[derive(Debug, Clone, Eq)]
pub struct ForExpr {
    /// The `for` expression introduction, containing an optional key var, value var and the
    /// collection expression that is iterated.
    pub intro: ForIntro,
    /// An expression that is evaluated once for each key in the source collection. If set, the
    /// result of the `for` expression will be an object. Otherwise, the result will be an array.
    pub key_expr: Option<Expression>,
    /// An expression that is evaluated once for each value in the source collection.
    pub value_expr: Expression,
    /// Indicates whether grouping mode is enabled. In grouping mode, each value in the resulting
    /// object is a list of all of the values that were produced against each distinct key. This is
    /// ignored if `key_expr` is `None`.
    pub grouping: bool,
    /// An optional filter expression. Elements for which the condition evaluates to `true` will
    /// be evaluated as normal, while if `false` the element will be skipped.
    pub cond: Option<ForCond>,

    decor: Decor,
    span: Option<Range<usize>>,
}

impl ForExpr {
    /// Creates a new `ForExpr` from a `for` expression introduction and a result value
    /// expression.
    pub fn new(intro: ForIntro, value_expr: impl Into<Expression>) -> ForExpr {
        ForExpr {
            intro,
            key_expr: None,
            value_expr: value_expr.into(),
            grouping: false,
            cond: None,
            decor: Decor::default(),
            span: None,
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        self.decor.despan(input);
        self.intro.despan(input);

        if let Some(key_expr) = &mut self.key_expr {
            key_expr.despan(input);
        }

        self.value_expr.despan(input);

        if let Some(cond) = &mut self.cond {
            cond.despan(input);
        }
    }
}

impl PartialEq for ForExpr {
    fn eq(&self, other: &Self) -> bool {
        self.intro == other.intro
            && self.key_expr == other.key_expr
            && self.value_expr == other.value_expr
            && self.grouping == other.grouping
            && self.cond == other.cond
    }
}

/// The `for` expression introduction, containing an optional key var, value var and the
/// collection expression that is iterated.
#[derive(Debug, Clone, Eq)]
pub struct ForIntro {
    /// Optional name of the variable that will be temporarily assigned the key of each element
    /// during iteration. If the source collection is an array, it gets assigned the zero-based
    /// array index. For an object source collection, this gets assigned the object's key.
    pub key_var: Option<Decorated<Ident>>,
    /// The name of the variable that will be temporarily assigned the value of each element
    /// during iteration.
    pub value_var: Decorated<Ident>,
    /// An expression that must evaluate to a value that can be iterated.
    pub collection_expr: Expression,

    decor: Decor,
    span: Option<Range<usize>>,
}

impl ForIntro {
    /// Creates a new `ForIntro` from a value variable and a collection expression.
    pub fn new(
        value_var: impl Into<Decorated<Ident>>,
        collection_expr: impl Into<Expression>,
    ) -> ForIntro {
        ForIntro {
            key_var: None,
            value_var: value_var.into(),
            collection_expr: collection_expr.into(),
            decor: Decor::default(),
            span: None,
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        self.decor.despan(input);
        if let Some(key_var) = &mut self.key_var {
            key_var.decor_mut().despan(input);
        }

        self.value_var.decor_mut().despan(input);
        self.collection_expr.despan(input);
    }
}

impl PartialEq for ForIntro {
    fn eq(&self, other: &Self) -> bool {
        self.key_var == other.key_var
            && self.value_var == other.value_var
            && self.collection_expr == other.collection_expr
    }
}

/// A filter expression. Elements for which the condition evaluates to `true` will be evaluated as
/// normal, while if `false` the element will be skipped.
#[derive(Debug, Clone, Eq)]
pub struct ForCond {
    /// The filter expression.
    pub expr: Expression,

    decor: Decor,
    span: Option<Range<usize>>,
}

impl ForCond {
    /// Creates a new `ForCond` from an expression.
    pub fn new(expr: impl Into<Expression>) -> ForCond {
        ForCond {
            expr: expr.into(),
            decor: Decor::default(),
            span: None,
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        self.decor.despan(input);
        self.expr.despan(input);
    }
}

impl PartialEq for ForCond {
    fn eq(&self, other: &Self) -> bool {
        self.expr == other.expr
    }
}

impl From<Expression> for ForCond {
    fn from(value: Expression) -> Self {
        ForCond::new(value)
    }
}

decorate_impl!(ForExpr, ForIntro, ForCond);
span_impl!(ForExpr, ForIntro, ForCond);