1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 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::ast::primitive::{
21
    LineTerminator, Number, Placeholder, SourceInfo, Template, Whitespace, U64,
22
};
23
use crate::typing::{Count, Duration, SourceString, ToSource};
24

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

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

            
77
impl OptionKind {
78
    /// Returns the Hurl string identifier of this option.
79
3740
    pub fn identifier(&self) -> &'static str {
80
3740
        match self {
81
40
            OptionKind::AwsSigV4(_) => "aws-sigv4",
82
65
            OptionKind::CaCertificate(_) => "cacert",
83
65
            OptionKind::ClientCert(_) => "cert",
84
50
            OptionKind::ClientKey(_) => "key",
85
410
            OptionKind::Compressed(_) => "compressed",
86
65
            OptionKind::ConnectTo(_) => "connect-to",
87
30
            OptionKind::ConnectTimeout(_) => "connect-timeout",
88
200
            OptionKind::Delay(_) => "delay",
89
410
            OptionKind::FollowLocation(_) => "location",
90
50
            OptionKind::FollowLocationTrusted(_) => "location-trusted",
91
55
            OptionKind::Header(_) => "header",
92
120
            OptionKind::Http10(_) => "http1.0",
93
90
            OptionKind::Http11(_) => "http1.1",
94
50
            OptionKind::Http2(_) => "http2",
95
30
            OptionKind::Http3(_) => "http3",
96
90
            OptionKind::Insecure(_) => "insecure",
97
30
            OptionKind::IpV4(_) => "ipv4",
98
30
            OptionKind::IpV6(_) => "ipv6",
99
35
            OptionKind::LimitRate(_) => "limit-rate",
100
85
            OptionKind::MaxRedirect(_) => "max-redirs",
101
30
            OptionKind::MaxTime(_) => "max-time",
102
30
            OptionKind::NetRc(_) => "netrc",
103
35
            OptionKind::NetRcFile(_) => "netrc-file",
104
30
            OptionKind::NetRcOptional(_) => "netrc-optional",
105
145
            OptionKind::Output(_) => "output",
106
35
            OptionKind::PathAsIs(_) => "path-as-is",
107
40
            OptionKind::PinnedPublicKey(_) => "pinnedpubkey",
108
70
            OptionKind::Proxy(_) => "proxy",
109
130
            OptionKind::Repeat(_) => "repeat",
110
50
            OptionKind::Resolve(_) => "resolve",
111
135
            OptionKind::Retry(_) => "retry",
112
110
            OptionKind::RetryInterval(_) => "retry-interval",
113
40
            OptionKind::Skip(_) => "skip",
114
30
            OptionKind::UnixSocket(_) => "unix-socket",
115
100
            OptionKind::User(_) => "user",
116
555
            OptionKind::Variable(_) => "variable",
117
125
            OptionKind::Verbose(_) => "verbose",
118
50
            OptionKind::VeryVerbose(_) => "very-verbose",
119
        }
120
    }
121
}
122

            
123
impl fmt::Display for OptionKind {
124
1950
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
125
1950
        let value = match self {
126
10
            OptionKind::AwsSigV4(value) => value.to_string(),
127
35
            OptionKind::CaCertificate(filename) => filename.to_string(),
128
20
            OptionKind::ClientCert(filename) => filename.to_string(),
129
20
            OptionKind::ClientKey(filename) => filename.to_string(),
130
285
            OptionKind::Compressed(value) => value.to_string(),
131
35
            OptionKind::ConnectTo(value) => value.to_string(),
132
            OptionKind::ConnectTimeout(value) => value.to_string(),
133
140
            OptionKind::Delay(value) => value.to_string(),
134
290
            OptionKind::FollowLocation(value) => value.to_string(),
135
15
            OptionKind::FollowLocationTrusted(value) => value.to_string(),
136
25
            OptionKind::Header(value) => value.to_string(),
137
60
            OptionKind::Http10(value) => value.to_string(),
138
40
            OptionKind::Http11(value) => value.to_string(),
139
20
            OptionKind::Http2(value) => value.to_string(),
140
            OptionKind::Http3(value) => value.to_string(),
141
45
            OptionKind::Insecure(value) => value.to_string(),
142
            OptionKind::IpV4(value) => value.to_string(),
143
            OptionKind::IpV6(value) => value.to_string(),
144
5
            OptionKind::LimitRate(value) => value.to_string(),
145
45
            OptionKind::MaxRedirect(value) => value.to_string(),
146
            OptionKind::MaxTime(value) => value.to_string(),
147
            OptionKind::NetRc(value) => value.to_string(),
148
5
            OptionKind::NetRcFile(filename) => filename.to_string(),
149
            OptionKind::NetRcOptional(value) => value.to_string(),
150
115
            OptionKind::Output(filename) => filename.to_string(),
151
5
            OptionKind::PathAsIs(value) => value.to_string(),
152
10
            OptionKind::PinnedPublicKey(value) => value.to_string(),
153
30
            OptionKind::Proxy(value) => value.to_string(),
154
80
            OptionKind::Repeat(value) => value.to_string(),
155
20
            OptionKind::Resolve(value) => value.to_string(),
156
55
            OptionKind::Retry(value) => value.to_string(),
157
40
            OptionKind::RetryInterval(value) => value.to_string(),
158
10
            OptionKind::Skip(value) => value.to_string(),
159
            OptionKind::UnixSocket(value) => value.to_string(),
160
60
            OptionKind::User(value) => value.to_string(),
161
365
            OptionKind::Variable(value) => value.to_string(),
162
50
            OptionKind::Verbose(value) => value.to_string(),
163
15
            OptionKind::VeryVerbose(value) => value.to_string(),
164
        };
165
1950
        write!(f, "{}: {}", self.identifier(), value)
166
    }
167
}
168

            
169
#[derive(Clone, Debug, PartialEq, Eq)]
170
pub enum BooleanOption {
171
    Literal(bool),
172
    Placeholder(Placeholder),
173
}
174

            
175
impl fmt::Display for BooleanOption {
176
835
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
177
835
        match self {
178
830
            BooleanOption::Literal(v) => write!(f, "{}", v),
179
5
            BooleanOption::Placeholder(v) => write!(f, "{}", v),
180
        }
181
    }
182
}
183

            
184
#[derive(Clone, Debug, PartialEq, Eq)]
185
pub enum NaturalOption {
186
    Literal(U64),
187
    Placeholder(Placeholder),
188
}
189

            
190
impl fmt::Display for NaturalOption {
191
5
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
192
5
        match self {
193
5
            NaturalOption::Literal(v) => write!(f, "{}", v),
194
            NaturalOption::Placeholder(v) => write!(f, "{}", v),
195
        }
196
    }
197
}
198

            
199
#[derive(Clone, Debug, PartialEq, Eq)]
200
pub enum CountOption {
201
    Literal(Count),
202
    Placeholder(Placeholder),
203
}
204

            
205
impl fmt::Display for CountOption {
206
180
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207
180
        match self {
208
170
            CountOption::Literal(v) => write!(f, "{}", v),
209
10
            CountOption::Placeholder(v) => write!(f, "{}", v),
210
        }
211
    }
212
}
213

            
214
#[derive(Clone, Debug, PartialEq, Eq)]
215
pub enum DurationOption {
216
    Literal(Duration),
217
    Placeholder(Placeholder),
218
}
219

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

            
229
#[derive(Clone, Debug, PartialEq, Eq)]
230
pub struct VariableDefinition {
231
    pub source_info: SourceInfo,
232
    pub name: String,
233
    pub space0: Whitespace,
234
    pub space1: Whitespace,
235
    pub value: VariableValue,
236
}
237

            
238
impl fmt::Display for VariableDefinition {
239
365
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
240
365
        write!(f, "{}={}", self.name, self.value)
241
    }
242
}
243

            
244
#[derive(Clone, Debug, PartialEq, Eq)]
245
pub enum VariableValue {
246
    Null,
247
    Bool(bool),
248
    Number(Number),
249
    String(Template),
250
}
251

            
252
impl fmt::Display for VariableValue {
253
365
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
254
365
        let s = match self {
255
            VariableValue::Null => "null".to_string(),
256
5
            VariableValue::Bool(value) => value.to_string(),
257
125
            VariableValue::Number(n) => n.to_string(),
258
235
            VariableValue::String(s) => s.to_string(),
259
        };
260
365
        write!(f, "{}", s)
261
    }
262
}
263

            
264
impl ToSource for VariableValue {
265
45
    fn to_source(&self) -> SourceString {
266
45
        match self {
267
5
            VariableValue::Null => "null".to_source(),
268
5
            VariableValue::Bool(value) => value.to_string().to_source(),
269
10
            VariableValue::Number(value) => value.to_source(),
270
25
            VariableValue::String(value) => value.to_string().to_source(),
271
        }
272
    }
273
}