1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use std::fmt;
19

            
20
use crate::types::{Count, DurationUnit, SourceString, ToSource};
21

            
22
use super::primitive::{
23
    LineTerminator, Number, Placeholder, SourceInfo, Template, U64, Whitespace,
24
};
25

            
26
#[derive(Clone, Debug, PartialEq, Eq)]
27
pub struct EntryOption {
28
    pub line_terminators: Vec<LineTerminator>,
29
    pub space0: Whitespace,
30
    pub space1: Whitespace,
31
    pub space2: Whitespace,
32
    pub kind: OptionKind,
33
    pub line_terminator0: LineTerminator,
34
}
35

            
36
#[derive(Clone, Debug, PartialEq, Eq)]
37
pub enum OptionKind {
38
    AwsSigV4(Template),
39
    CaCertificate(Template),
40
    ClientCert(Template),
41
    ClientKey(Template),
42
    Compressed(BooleanOption),
43
    ConnectTo(Template),
44
    ConnectTimeout(DurationOption),
45
    Delay(DurationOption),
46
    Digest(BooleanOption),
47
    FailWithBody(BooleanOption),
48
    FollowLocation(BooleanOption),
49
    FollowLocationTrusted(BooleanOption),
50
    Header(Template),
51
    Http10(BooleanOption),
52
    Http11(BooleanOption),
53
    Http2(BooleanOption),
54
    Http3(BooleanOption),
55
    Insecure(BooleanOption),
56
    IpV4(BooleanOption),
57
    IpV6(BooleanOption),
58
    LimitRate(NaturalOption),
59
    MaxRedirect(CountOption),
60
    MaxTime(DurationOption),
61
    Negotiate(BooleanOption),
62
    NetRc(BooleanOption),
63
    NetRcFile(Template),
64
    NetRcOptional(BooleanOption),
65
    NoHeader(Template),
66
    Ntlm(BooleanOption),
67
    Output(Template),
68
    PathAsIs(BooleanOption),
69
    PinnedPublicKey(Template),
70
    Proxy(Template),
71
    Repeat(CountOption),
72
    Resolve(Template),
73
    Retry(CountOption),
74
    RetryInterval(DurationOption),
75
    Skip(BooleanOption),
76
    UnixSocket(Template),
77
    User(Template),
78
    Variable(VariableDefinition),
79
    Verbose(BooleanOption),
80
    Verbosity(VerbosityOption),
81
    VeryVerbose(BooleanOption),
82
}
83

            
84
impl OptionKind {
85
    /// Returns the Hurl string identifier of this option.
86
5260
    pub fn identifier(&self) -> &'static str {
87
5260
        match self {
88
50
            OptionKind::AwsSigV4(_) => "aws-sigv4",
89
75
            OptionKind::CaCertificate(_) => "cacert",
90
80
            OptionKind::ClientCert(_) => "cert",
91
60
            OptionKind::ClientKey(_) => "key",
92
420
            OptionKind::Compressed(_) => "compressed",
93
75
            OptionKind::ConnectTo(_) => "connect-to",
94
40
            OptionKind::ConnectTimeout(_) => "connect-timeout",
95
620
            OptionKind::Delay(_) => "delay",
96
45
            OptionKind::Digest(_) => "digest",
97
45
            OptionKind::FailWithBody(_) => "fail-with-body",
98
450
            OptionKind::FollowLocation(_) => "location",
99
60
            OptionKind::FollowLocationTrusted(_) => "location-trusted",
100
80
            OptionKind::Header(_) => "header",
101
130
            OptionKind::Http10(_) => "http1.0",
102
100
            OptionKind::Http11(_) => "http1.1",
103
60
            OptionKind::Http2(_) => "http2",
104
40
            OptionKind::Http3(_) => "http3",
105
110
            OptionKind::Insecure(_) => "insecure",
106
40
            OptionKind::IpV4(_) => "ipv4",
107
40
            OptionKind::IpV6(_) => "ipv6",
108
45
            OptionKind::LimitRate(_) => "limit-rate",
109
95
            OptionKind::MaxRedirect(_) => "max-redirs",
110
40
            OptionKind::MaxTime(_) => "max-time",
111
50
            OptionKind::Negotiate(_) => "negotiate",
112
40
            OptionKind::NetRc(_) => "netrc",
113
45
            OptionKind::NetRcFile(_) => "netrc-file",
114
40
            OptionKind::NetRcOptional(_) => "netrc-optional",
115
65
            OptionKind::NoHeader(_) => "no-header",
116
55
            OptionKind::Ntlm(_) => "ntlm",
117
190
            OptionKind::Output(_) => "output",
118
45
            OptionKind::PathAsIs(_) => "path-as-is",
119
50
            OptionKind::PinnedPublicKey(_) => "pinnedpubkey",
120
80
            OptionKind::Proxy(_) => "proxy",
121
175
            OptionKind::Repeat(_) => "repeat",
122
60
            OptionKind::Resolve(_) => "resolve",
123
175
            OptionKind::Retry(_) => "retry",
124
145
            OptionKind::RetryInterval(_) => "retry-interval",
125
50
            OptionKind::Skip(_) => "skip",
126
40
            OptionKind::UnixSocket(_) => "unix-socket",
127
160
            OptionKind::User(_) => "user",
128
685
            OptionKind::Variable(_) => "variable",
129
195
            OptionKind::Verbose(_) => "verbose",
130
55
            OptionKind::Verbosity(_) => "verbosity",
131
60
            OptionKind::VeryVerbose(_) => "very-verbose",
132
        }
133
    }
134
}
135

            
136
impl fmt::Display for OptionKind {
137
2625
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
138
2625
        let value = match self {
139
10
            OptionKind::AwsSigV4(value) => value.to_string(),
140
35
            OptionKind::CaCertificate(filename) => filename.to_string(),
141
20
            OptionKind::ClientCert(filename) => filename.to_string(),
142
20
            OptionKind::ClientKey(filename) => filename.to_string(),
143
285
            OptionKind::Compressed(value) => value.to_string(),
144
35
            OptionKind::ConnectTo(value) => value.to_string(),
145
            OptionKind::ConnectTimeout(value) => value.to_string(),
146
500
            OptionKind::Delay(value) => value.to_string(),
147
5
            OptionKind::Digest(value) => value.to_string(),
148
5
            OptionKind::FailWithBody(value) => value.to_string(),
149
310
            OptionKind::FollowLocation(value) => value.to_string(),
150
15
            OptionKind::FollowLocationTrusted(value) => value.to_string(),
151
40
            OptionKind::Header(value) => value.to_string(),
152
60
            OptionKind::Http10(value) => value.to_string(),
153
40
            OptionKind::Http11(value) => value.to_string(),
154
20
            OptionKind::Http2(value) => value.to_string(),
155
            OptionKind::Http3(value) => value.to_string(),
156
50
            OptionKind::Insecure(value) => value.to_string(),
157
            OptionKind::IpV4(value) => value.to_string(),
158
            OptionKind::IpV6(value) => value.to_string(),
159
5
            OptionKind::LimitRate(value) => value.to_string(),
160
45
            OptionKind::MaxRedirect(value) => value.to_string(),
161
            OptionKind::MaxTime(value) => value.to_string(),
162
            OptionKind::Negotiate(value) => value.to_string(),
163
            OptionKind::NetRc(value) => value.to_string(),
164
5
            OptionKind::NetRcFile(filename) => filename.to_string(),
165
            OptionKind::NetRcOptional(value) => value.to_string(),
166
25
            OptionKind::NoHeader(value) => value.to_string(),
167
5
            OptionKind::Ntlm(value) => value.to_string(),
168
150
            OptionKind::Output(filename) => filename.to_string(),
169
5
            OptionKind::PathAsIs(value) => value.to_string(),
170
10
            OptionKind::PinnedPublicKey(value) => value.to_string(),
171
30
            OptionKind::Proxy(value) => value.to_string(),
172
105
            OptionKind::Repeat(value) => value.to_string(),
173
20
            OptionKind::Resolve(value) => value.to_string(),
174
70
            OptionKind::Retry(value) => value.to_string(),
175
55
            OptionKind::RetryInterval(value) => value.to_string(),
176
10
            OptionKind::Skip(value) => value.to_string(),
177
            OptionKind::UnixSocket(value) => value.to_string(),
178
90
            OptionKind::User(value) => value.to_string(),
179
430
            OptionKind::Variable(value) => value.to_string(),
180
85
            OptionKind::Verbose(value) => value.to_string(),
181
15
            OptionKind::Verbosity(value) => value.to_string(),
182
15
            OptionKind::VeryVerbose(value) => value.to_string(),
183
        };
184
2625
        write!(f, "{}: {}", self.identifier(), value)
185
    }
186
}
187

            
188
#[derive(Clone, Debug, PartialEq, Eq)]
189
pub enum BooleanOption {
190
    Literal(bool),
191
    Placeholder(Placeholder),
192
}
193

            
194
impl fmt::Display for BooleanOption {
195
910
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
196
910
        match self {
197
905
            BooleanOption::Literal(v) => write!(f, "{v}"),
198
5
            BooleanOption::Placeholder(v) => write!(f, "{v}"),
199
        }
200
    }
201
}
202

            
203
#[derive(Clone, Debug, PartialEq, Eq)]
204
pub enum NaturalOption {
205
    Literal(U64),
206
    Placeholder(Placeholder),
207
}
208

            
209
impl fmt::Display for NaturalOption {
210
5
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
211
5
        match self {
212
5
            NaturalOption::Literal(v) => write!(f, "{v}"),
213
            NaturalOption::Placeholder(v) => write!(f, "{v}"),
214
        }
215
    }
216
}
217

            
218
#[derive(Clone, Debug, PartialEq, Eq)]
219
pub enum CountOption {
220
    Literal(Count),
221
    Placeholder(Placeholder),
222
}
223

            
224
impl fmt::Display for CountOption {
225
220
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
226
220
        match self {
227
210
            CountOption::Literal(v) => write!(f, "{v}"),
228
10
            CountOption::Placeholder(v) => write!(f, "{v}"),
229
        }
230
    }
231
}
232

            
233
#[derive(Clone, Debug, PartialEq, Eq)]
234
pub enum DurationOption {
235
    Literal(Duration),
236
    Placeholder(Placeholder),
237
}
238

            
239
impl fmt::Display for DurationOption {
240
555
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241
555
        match self {
242
550
            DurationOption::Literal(v) => write!(f, "{v}"),
243
5
            DurationOption::Placeholder(v) => write!(f, "{v}"),
244
        }
245
    }
246
}
247

            
248
/// Represent a duration
249
#[derive(Clone, Debug, PartialEq, Eq)]
250
pub struct Duration {
251
    pub value: U64,
252
    pub unit: Option<DurationUnit>,
253
}
254

            
255
impl Duration {
256
760
    pub fn new(value: U64, unit: Option<DurationUnit>) -> Duration {
257
760
        Duration { value, unit }
258
    }
259
}
260

            
261
impl fmt::Display for Duration {
262
550
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
263
550
        let unit = if let Some(value) = self.unit {
264
550
            value.to_string()
265
        } else {
266
            String::new()
267
        };
268
550
        write!(f, "{}{unit}", self.value)
269
    }
270
}
271

            
272
#[derive(Clone, Debug, PartialEq, Eq)]
273
pub struct VariableDefinition {
274
    pub source_info: SourceInfo,
275
    pub name: String,
276
    pub space0: Whitespace,
277
    pub space1: Whitespace,
278
    pub value: VariableValue,
279
}
280

            
281
impl fmt::Display for VariableDefinition {
282
430
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283
430
        write!(f, "{}={}", self.name, self.value)
284
    }
285
}
286

            
287
#[derive(Clone, Debug, PartialEq, Eq)]
288
pub enum VariableValue {
289
    Null,
290
    Bool(bool),
291
    Number(Number),
292
    String(Template),
293
}
294

            
295
impl fmt::Display for VariableValue {
296
430
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
297
430
        let s = match self {
298
            VariableValue::Null => "null".to_string(),
299
5
            VariableValue::Bool(value) => value.to_string(),
300
155
            VariableValue::Number(n) => n.to_string(),
301
270
            VariableValue::String(s) => s.to_string(),
302
        };
303
430
        write!(f, "{s}")
304
    }
305
}
306

            
307
impl ToSource for VariableValue {
308
90
    fn to_source(&self) -> SourceString {
309
90
        match self {
310
10
            VariableValue::Null => "null".to_source(),
311
10
            VariableValue::Bool(value) => value.to_string().to_source(),
312
20
            VariableValue::Number(value) => value.to_source(),
313
50
            VariableValue::String(value) => value.to_source(),
314
        }
315
    }
316
}
317

            
318
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
319
pub enum VerbosityOption {
320
    Brief,
321
    Verbose,
322
    Debug,
323
}
324

            
325
impl fmt::Display for VerbosityOption {
326
35
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
327
35
        write!(f, "{}", self.identifier())
328
    }
329
}
330

            
331
impl VerbosityOption {
332
55
    pub fn identifier(&self) -> &'static str {
333
55
        match self {
334
45
            VerbosityOption::Brief => "brief",
335
5
            VerbosityOption::Verbose => "verbose",
336
5
            VerbosityOption::Debug => "debug",
337
        }
338
    }
339
}