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
    Proxy(Template),
64
    Repeat(CountOption),
65
    Resolve(Template),
66
    Retry(CountOption),
67
    RetryInterval(DurationOption),
68
    Skip(BooleanOption),
69
    UnixSocket(Template),
70
    User(Template),
71
    Variable(VariableDefinition),
72
    Verbose(BooleanOption),
73
    VeryVerbose(BooleanOption),
74
}
75

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

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

            
166
#[derive(Clone, Debug, PartialEq, Eq)]
167
pub enum BooleanOption {
168
    Literal(bool),
169
    Placeholder(Placeholder),
170
}
171

            
172
impl fmt::Display for BooleanOption {
173
820
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
174
820
        match self {
175
815
            BooleanOption::Literal(v) => write!(f, "{}", v),
176
5
            BooleanOption::Placeholder(v) => write!(f, "{}", v),
177
        }
178
    }
179
}
180

            
181
#[derive(Clone, Debug, PartialEq, Eq)]
182
pub enum NaturalOption {
183
    Literal(U64),
184
    Placeholder(Placeholder),
185
}
186

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

            
196
#[derive(Clone, Debug, PartialEq, Eq)]
197
pub enum CountOption {
198
    Literal(Count),
199
    Placeholder(Placeholder),
200
}
201

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

            
211
#[derive(Clone, Debug, PartialEq, Eq)]
212
pub enum DurationOption {
213
    Literal(Duration),
214
    Placeholder(Placeholder),
215
}
216

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

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

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

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

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

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