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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use super::Identifier;
use crate::{Number, Value};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::{self, Display, Write};
pub type Object<K, V> = indexmap::IndexMap<K, V>;
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq)]
#[serde(rename = "$hcl::expression")]
#[non_exhaustive]
pub enum Expression {
Null,
Bool(bool),
Number(Number),
String(String),
Array(Vec<Expression>),
Object(Object<ObjectKey, Expression>),
Raw(RawExpression),
}
impl From<Expression> for Value {
fn from(expr: Expression) -> Self {
match expr {
Expression::Null => Value::Null,
Expression::Bool(b) => Value::Bool(b),
Expression::Number(n) => Value::Number(n),
Expression::String(s) => Value::String(s),
Expression::Array(array) => array.into_iter().collect(),
Expression::Object(object) => object.into_iter().collect(),
Expression::Raw(raw) => Value::String(raw.into()),
}
}
}
impl From<Value> for Expression {
fn from(value: Value) -> Self {
match value {
Value::Null => Expression::Null,
Value::Bool(b) => Expression::Bool(b),
Value::Number(n) => Expression::Number(n),
Value::String(s) => Expression::String(s),
Value::Array(array) => array.into_iter().collect(),
Value::Object(object) => object.into_iter().collect(),
}
}
}
macro_rules! impl_from_integer {
($($ty:ty),*) => {
$(
impl From<$ty> for Expression {
fn from(n: $ty) -> Self {
Expression::Number(n.into())
}
}
)*
};
}
impl_from_integer!(i8, i16, i32, i64, isize);
impl_from_integer!(u8, u16, u32, u64, usize);
impl From<f32> for Expression {
fn from(f: f32) -> Self {
Expression::Number(f.into())
}
}
impl From<f64> for Expression {
fn from(f: f64) -> Self {
Expression::Number(f.into())
}
}
impl From<bool> for Expression {
fn from(b: bool) -> Self {
Expression::Bool(b)
}
}
impl From<String> for Expression {
fn from(s: String) -> Self {
Expression::String(s)
}
}
impl From<&str> for Expression {
fn from(s: &str) -> Self {
Expression::String(s.to_string())
}
}
impl<'a> From<Cow<'a, str>> for Expression {
fn from(s: Cow<'a, str>) -> Self {
Expression::String(s.into_owned())
}
}
impl From<Object<ObjectKey, Expression>> for Expression {
fn from(f: Object<ObjectKey, Expression>) -> Self {
Expression::Object(f)
}
}
impl<T: Into<Expression>> From<Vec<T>> for Expression {
fn from(f: Vec<T>) -> Self {
Expression::Array(f.into_iter().map(Into::into).collect())
}
}
impl<'a, T: Clone + Into<Expression>> From<&'a [T]> for Expression {
fn from(f: &'a [T]) -> Self {
Expression::Array(f.iter().cloned().map(Into::into).collect())
}
}
impl<T: Into<Expression>> FromIterator<T> for Expression {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Expression::Array(iter.into_iter().map(Into::into).collect())
}
}
impl<K: Into<ObjectKey>, V: Into<Expression>> FromIterator<(K, V)> for Expression {
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
Expression::Object(
iter.into_iter()
.map(|(k, v)| (k.into(), v.into()))
.collect(),
)
}
}
impl From<()> for Expression {
fn from((): ()) -> Self {
Expression::Null
}
}
impl From<RawExpression> for Expression {
fn from(raw: RawExpression) -> Self {
Expression::Raw(raw)
}
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename = "$hcl::object_key")]
#[non_exhaustive]
pub enum ObjectKey {
Identifier(Identifier),
String(String),
RawExpression(RawExpression),
}
impl ObjectKey {
pub fn identifier<I>(identifier: I) -> Self
where
I: Into<Identifier>,
{
ObjectKey::Identifier(identifier.into())
}
pub fn string<S>(string: S) -> Self
where
S: Into<String>,
{
ObjectKey::String(string.into())
}
pub fn raw_expression<E>(expr: E) -> Self
where
E: Into<RawExpression>,
{
ObjectKey::RawExpression(expr.into())
}
}
impl From<&str> for ObjectKey {
fn from(key: &str) -> Self {
ObjectKey::String(key.into())
}
}
impl From<String> for ObjectKey {
fn from(key: String) -> Self {
ObjectKey::String(key)
}
}
impl<'a> From<Cow<'a, str>> for ObjectKey {
fn from(key: Cow<'a, str>) -> Self {
ObjectKey::String(key.into())
}
}
impl From<ObjectKey> for String {
fn from(key: ObjectKey) -> Self {
key.to_string()
}
}
impl Display for ObjectKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ObjectKey::Identifier(k) => Display::fmt(k.as_str(), f),
ObjectKey::String(k) => Display::fmt(k, f),
ObjectKey::RawExpression(raw) => Display::fmt(raw, f),
}
}
}
#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(rename = "$hcl::raw_expression")]
pub struct RawExpression(String);
impl RawExpression {
pub fn new<E>(expr: E) -> RawExpression
where
E: Into<String>,
{
RawExpression(expr.into())
}
pub fn into_inner(self) -> String {
self.0
}
}
impl From<String> for RawExpression {
fn from(expr: String) -> Self {
RawExpression::new(expr)
}
}
impl From<&str> for RawExpression {
fn from(expr: &str) -> Self {
RawExpression::new(expr)
}
}
impl<'a> From<Cow<'a, str>> for RawExpression {
fn from(expr: Cow<'a, str>) -> Self {
RawExpression::new(expr)
}
}
impl From<RawExpression> for String {
fn from(expr: RawExpression) -> Self {
expr.to_string()
}
}
impl Display for RawExpression {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("${")?;
f.write_str(&self.0)?;
f.write_char('}')
}
}