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
/*
* Hurl (https://hurl.dev)
* Copyright (C) 2026 Orange
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
use std::collections::{HashMap, HashSet};
use super::value::Value;
/// Represents a variable named to hold `Value`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Variable {
/// Name of this variable.
name: String,
/// Value of this variable.
value: Value,
/// A variable is either public, or secret.
visibility: Visibility,
}
/// Visibility of a variable value.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Visibility {
/// The variable's value is always visible.
Public,
/// The variable's value is redacted from standard error and reports.
Secret,
}
impl Variable {
/// Creates a new variable named `name` with this `value` and `visibility`.
pub fn new(name: &str, value: &Value, visibility: Visibility) -> Self {
Variable {
name: name.to_string(),
value: value.clone(),
visibility,
}
}
/// Returns a reference to this variable's `value`.
pub fn value(&self) -> &Value {
&self.value
}
/// Returns `true` if this variable is secret.
pub fn is_secret(&self) -> bool {
matches!(self.visibility, Visibility::Secret)
}
}
/// Represents a set of variables, either injected at the start
/// of execution, or inserted during a run.
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub struct VariableSet {
/// The map of variables
variables: HashMap<String, Variable>,
/// All the secrets values: the current secrets, as values of current secret variables,
/// and the previous secrets. What's a secret remains a secret forever, even if a new secret
/// variable get a new value.
secrets: HashSet<String>,
}
impl VariableSet {
/// Creates a new empty set of variables.
pub fn new() -> Self {
VariableSet {
variables: HashMap::new(),
secrets: HashSet::new(),
}
}
/// Creates a new set of public variables from a [`HashMap`].
pub fn from(variables: &HashMap<String, Value>) -> Self {
let variables = variables
.iter()
.map(|(name, value)| {
(
name.to_string(),
Variable::new(name, value, Visibility::Public),
)
})
.collect::<HashMap<_, _>>();
VariableSet {
variables: variables.clone(),
secrets: HashSet::new(),
}
}
/// Inserts a public variable named `name` with `value`.
pub fn insert(&mut self, name: String, value: Value) {
let variable = Variable::new(&name, &value, Visibility::Public);
self.variables.insert(name, variable);
}
/// Inserts a secret string value named `name` with `value`.
///
/// A secret is declaring also a new secret variable named `name` with `value` so this secret
/// can be used directly as a variable in a Hurl file.
pub fn insert_secret(&mut self, name: String, value: String) {
// Update our secrets list with this new value
self.secrets.insert(value.clone());
let value = Value::String(value);
let variable = Variable::new(&name, &value, Visibility::Secret);
self.variables.insert(name, variable);
}
/// Returns a reference to the value corresponding to the variable named `name`.
pub fn get(&self, name: &str) -> Option<&Variable> {
self.variables.get(name)
}
/// Returns an iterator over all the variables values.
pub fn iter(&self) -> impl Iterator<Item = (&String, &Variable)> {
self.variables.iter()
}
/// Returns the list of all secrets values.
pub fn secrets(&self) -> Vec<String> {
self.secrets.iter().cloned().collect::<Vec<_>>()
}
}
#[cfg(test)]
mod test {
use crate::runner::Number::{Float, Integer};
use crate::runner::{Value, Variable, VariableSet, Visibility};
#[test]
fn simple_variable_set() {
let mut variables = VariableSet::new();
variables.insert("foo".to_string(), Value::String("xxx".to_string()));
variables.insert("bar".to_string(), Value::Number(Integer(42)));
variables.insert("bar".to_string(), Value::Bool(true));
variables.insert("baz".to_string(), Value::Number(Float(1.0)));
variables.insert_secret("quic".to_string(), "42".to_string());
assert_eq!(
variables.get("foo"),
Some(&Variable::new(
"foo",
&Value::String("xxx".to_string()),
Visibility::Public
))
);
assert!(variables.get("Foo").is_none());
assert_eq!(
variables.get("bar"),
Some(&Variable::new(
"bar",
&Value::Bool(true),
Visibility::Public
))
);
assert_eq!(
variables.get("baz"),
Some(&Variable::new(
"baz",
&Value::Number(Float(1.0)),
Visibility::Public
))
);
assert_eq!(
variables.get("quic"),
Some(&Variable::new(
"quic",
&Value::String("42".to_string()),
Visibility::Secret
))
);
assert!(variables.get("BAZ").is_none());
assert_eq!(variables.secrets().len(), 1);
assert!(variables.secrets().contains(&"42".to_string()));
}
#[test]
fn iter_variable_set() {
fn expected_value<'data>(
name: &str,
data: &'data [(String, Value)],
) -> Option<&'data Value> {
for (n, v) in data.iter() {
if n == name {
return Some(v);
}
}
None
}
let data = [
("foo".to_string(), Value::String("yyy".to_string())),
("bar".to_string(), Value::Bool(false)),
("baz".to_string(), Value::Number(Float(12.0))),
];
let mut variables = VariableSet::new();
data.clone().into_iter().for_each(|(name, value)| {
variables.insert(name, value);
});
// Test iter()
for (name, variable) in variables.iter() {
let expected = expected_value(name, &data);
assert_eq!(expected.unwrap(), &variable.value);
}
}
#[test]
fn secret_variable_can_be_reassigned_but_remains_secret() {
let mut variables = VariableSet::new();
variables.insert_secret("foo".to_string(), "42".to_string());
variables.insert("foo".to_string(), Value::String("xxx".to_string()));
assert_eq!(
variables.get("foo").unwrap().value,
Value::String("xxx".to_string())
);
assert!(variables.secrets().contains(&"42".to_string()));
}
#[test]
fn get_secrets() {
let mut variables = VariableSet::new();
variables.insert_secret("foo".to_string(), "42".to_string());
variables.insert("bar".to_string(), Value::String("toto".to_string()));
variables.insert("baz".to_string(), Value::Bool(true));
variables.insert_secret("a".to_string(), "1234".to_string());
let mut secrets = variables.secrets();
secrets.sort();
assert_eq!(secrets, vec!["1234", "42"]);
}
}