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
    Http2PriorKnowledge(BooleanOption),
55
    Http3(BooleanOption),
56
    Insecure(BooleanOption),
57
    IpV4(BooleanOption),
58
    IpV6(BooleanOption),
59
    LimitRate(NaturalOption),
60
    MaxRedirect(CountOption),
61
    MaxTime(DurationOption),
62
    Negotiate(BooleanOption),
63
    NetRc(BooleanOption),
64
    NetRcFile(Template),
65
    NetRcOptional(BooleanOption),
66
    NoHeader(Template),
67
    NoJsonpathCoercion(BooleanOption),
68
    Ntlm(BooleanOption),
69
    Output(Template),
70
    PathAsIs(BooleanOption),
71
    PinnedPublicKey(Template),
72
    Proxy(Template),
73
    Repeat(CountOption),
74
    Resolve(Template),
75
    Retry(CountOption),
76
    RetryInterval(DurationOption),
77
    Skip(BooleanOption),
78
    UnixSocket(Template),
79
    User(Template),
80
    Variable(VariableDefinition),
81
    VariablesFile(Template),
82
    Verbose(BooleanOption),
83
    Verbosity(VerbosityOption),
84
    VeryVerbose(BooleanOption),
85
}
86

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

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

            
197
#[derive(Clone, Debug, PartialEq, Eq)]
198
pub enum BooleanOption {
199
    Literal(bool),
200
    Placeholder(Placeholder),
201
}
202

            
203
impl fmt::Display for BooleanOption {
204
975
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205
975
        match self {
206
970
            BooleanOption::Literal(v) => write!(f, "{v}"),
207
5
            BooleanOption::Placeholder(v) => write!(f, "{v}"),
208
        }
209
    }
210
}
211

            
212
#[derive(Clone, Debug, PartialEq, Eq)]
213
pub enum NaturalOption {
214
    Literal(U64),
215
    Placeholder(Placeholder),
216
}
217

            
218
impl fmt::Display for NaturalOption {
219
5
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
220
5
        match self {
221
5
            NaturalOption::Literal(v) => write!(f, "{v}"),
222
            NaturalOption::Placeholder(v) => write!(f, "{v}"),
223
        }
224
    }
225
}
226

            
227
#[derive(Clone, Debug, PartialEq, Eq)]
228
pub enum CountOption {
229
    Literal(Count),
230
    Placeholder(Placeholder),
231
}
232

            
233
impl fmt::Display for CountOption {
234
220
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
235
220
        match self {
236
210
            CountOption::Literal(v) => write!(f, "{v}"),
237
10
            CountOption::Placeholder(v) => write!(f, "{v}"),
238
        }
239
    }
240
}
241

            
242
#[derive(Clone, Debug, PartialEq, Eq)]
243
pub enum DurationOption {
244
    Literal(Duration),
245
    Placeholder(Placeholder),
246
}
247

            
248
impl fmt::Display for DurationOption {
249
555
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
250
555
        match self {
251
550
            DurationOption::Literal(v) => write!(f, "{v}"),
252
5
            DurationOption::Placeholder(v) => write!(f, "{v}"),
253
        }
254
    }
255
}
256

            
257
/// Represent a duration
258
#[derive(Clone, Debug, PartialEq, Eq)]
259
pub struct Duration {
260
    pub value: U64,
261
    pub unit: Option<DurationUnit>,
262
}
263

            
264
impl Duration {
265
760
    pub fn new(value: U64, unit: Option<DurationUnit>) -> Duration {
266
760
        Duration { value, unit }
267
    }
268
}
269

            
270
impl fmt::Display for Duration {
271
550
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
272
550
        let unit = if let Some(value) = self.unit {
273
550
            value.to_string()
274
        } else {
275
            String::new()
276
        };
277
550
        write!(f, "{}{unit}", self.value)
278
    }
279
}
280

            
281
#[derive(Clone, Debug, PartialEq, Eq)]
282
pub struct VariableDefinition {
283
    pub source_info: SourceInfo,
284
    pub name: String,
285
    pub space0: Whitespace,
286
    pub space1: Whitespace,
287
    pub value: VariableValue,
288
}
289

            
290
impl fmt::Display for VariableDefinition {
291
440
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
292
440
        write!(f, "{}={}", self.name, self.value)
293
    }
294
}
295

            
296
#[derive(Clone, Debug, PartialEq, Eq)]
297
pub enum VariableValue {
298
    Null,
299
    Bool(bool),
300
    Number(Number),
301
    String(Template),
302
}
303

            
304
impl fmt::Display for VariableValue {
305
440
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
306
440
        let s = match self {
307
            VariableValue::Null => "null".to_string(),
308
5
            VariableValue::Bool(value) => value.to_string(),
309
155
            VariableValue::Number(n) => n.to_string(),
310
280
            VariableValue::String(s) => s.to_string(),
311
        };
312
440
        write!(f, "{s}")
313
    }
314
}
315

            
316
impl ToSource for VariableValue {
317
90
    fn to_source(&self) -> SourceString {
318
90
        match self {
319
10
            VariableValue::Null => "null".to_source(),
320
10
            VariableValue::Bool(value) => value.to_string().to_source(),
321
20
            VariableValue::Number(value) => value.to_source(),
322
50
            VariableValue::String(value) => value.to_source(),
323
        }
324
    }
325
}
326

            
327
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
328
pub enum VerbosityOption {
329
    Brief,
330
    Verbose,
331
    Debug,
332
}
333

            
334
impl fmt::Display for VerbosityOption {
335
35
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
336
35
        write!(f, "{}", self.identifier())
337
    }
338
}
339

            
340
impl VerbosityOption {
341
55
    pub fn identifier(&self) -> &'static str {
342
55
        match self {
343
45
            VerbosityOption::Brief => "brief",
344
5
            VerbosityOption::Verbose => "verbose",
345
5
            VerbosityOption::Debug => "debug",
346
        }
347
    }
348
}