hcl_edit/expr/
for_expr.rs1use crate::expr::Expression;
2use crate::{Decor, Decorate, Decorated, Ident};
3use std::ops::Range;
4
5#[derive(Debug, Clone, Eq)]
8pub struct ForExpr {
9 pub intro: ForIntro,
12 pub key_expr: Option<Expression>,
15 pub value_expr: Expression,
17 pub grouping: bool,
21 pub cond: Option<ForCond>,
24
25 decor: Decor,
26 span: Option<Range<usize>>,
27}
28
29impl ForExpr {
30 pub fn new(intro: ForIntro, value_expr: impl Into<Expression>) -> ForExpr {
33 ForExpr {
34 intro,
35 key_expr: None,
36 value_expr: value_expr.into(),
37 grouping: false,
38 cond: None,
39 decor: Decor::default(),
40 span: None,
41 }
42 }
43
44 pub(crate) fn despan(&mut self, input: &str) {
45 self.decor.despan(input);
46 self.intro.despan(input);
47
48 if let Some(key_expr) = &mut self.key_expr {
49 key_expr.despan(input);
50 }
51
52 self.value_expr.despan(input);
53
54 if let Some(cond) = &mut self.cond {
55 cond.despan(input);
56 }
57 }
58}
59
60impl PartialEq for ForExpr {
61 fn eq(&self, other: &Self) -> bool {
62 self.intro == other.intro
63 && self.key_expr == other.key_expr
64 && self.value_expr == other.value_expr
65 && self.grouping == other.grouping
66 && self.cond == other.cond
67 }
68}
69
70#[derive(Debug, Clone, Eq)]
73pub struct ForIntro {
74 pub key_var: Option<Decorated<Ident>>,
78 pub value_var: Decorated<Ident>,
81 pub collection_expr: Expression,
83
84 decor: Decor,
85 span: Option<Range<usize>>,
86}
87
88impl ForIntro {
89 pub fn new(
91 value_var: impl Into<Decorated<Ident>>,
92 collection_expr: impl Into<Expression>,
93 ) -> ForIntro {
94 ForIntro {
95 key_var: None,
96 value_var: value_var.into(),
97 collection_expr: collection_expr.into(),
98 decor: Decor::default(),
99 span: None,
100 }
101 }
102
103 pub(crate) fn despan(&mut self, input: &str) {
104 self.decor.despan(input);
105 if let Some(key_var) = &mut self.key_var {
106 key_var.decor_mut().despan(input);
107 }
108
109 self.value_var.decor_mut().despan(input);
110 self.collection_expr.despan(input);
111 }
112}
113
114impl PartialEq for ForIntro {
115 fn eq(&self, other: &Self) -> bool {
116 self.key_var == other.key_var
117 && self.value_var == other.value_var
118 && self.collection_expr == other.collection_expr
119 }
120}
121
122#[derive(Debug, Clone, Eq)]
125pub struct ForCond {
126 pub expr: Expression,
128
129 decor: Decor,
130 span: Option<Range<usize>>,
131}
132
133impl ForCond {
134 pub fn new(expr: impl Into<Expression>) -> ForCond {
136 ForCond {
137 expr: expr.into(),
138 decor: Decor::default(),
139 span: None,
140 }
141 }
142
143 pub(crate) fn despan(&mut self, input: &str) {
144 self.decor.despan(input);
145 self.expr.despan(input);
146 }
147}
148
149impl PartialEq for ForCond {
150 fn eq(&self, other: &Self) -> bool {
151 self.expr == other.expr
152 }
153}
154
155impl From<Expression> for ForCond {
156 fn from(value: Expression) -> Self {
157 ForCond::new(value)
158 }
159}
160
161decorate_impl!(ForExpr, ForIntro, ForCond);
162span_impl!(ForExpr, ForIntro, ForCond);