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
    NetRc(BooleanOption),
58
    NetRcFile(Template),
59
    NetRcOptional(BooleanOption),
60
    Output(Template),
61
    PathAsIs(BooleanOption),
62
    Proxy(Template),
63
    Repeat(CountOption),
64
    Resolve(Template),
65
    Retry(CountOption),
66
    RetryInterval(DurationOption),
67
    Skip(BooleanOption),
68
    UnixSocket(Template),
69
    User(Template),
70
    Variable(VariableDefinition),
71
    Verbose(BooleanOption),
72
    VeryVerbose(BooleanOption),
73
}
74

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

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

            
163
#[derive(Clone, Debug, PartialEq, Eq)]
164
pub enum BooleanOption {
165
    Literal(bool),
166
    Placeholder(Placeholder),
167
}
168

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

            
178
#[derive(Clone, Debug, PartialEq, Eq)]
179
pub enum NaturalOption {
180
    Literal(U64),
181
    Placeholder(Placeholder),
182
}
183

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

            
193
#[derive(Clone, Debug, PartialEq, Eq)]
194
pub enum CountOption {
195
    Literal(Count),
196
    Placeholder(Placeholder),
197
}
198

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

            
208
#[derive(Clone, Debug, PartialEq, Eq)]
209
pub enum DurationOption {
210
    Literal(Duration),
211
    Placeholder(Placeholder),
212
}
213

            
214
impl fmt::Display for DurationOption {
215
70
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
216
70
        match self {
217
65
            DurationOption::Literal(v) => write!(f, "{}", v),
218
5
            DurationOption::Placeholder(v) => write!(f, "{}", v),
219
        }
220
    }
221
}
222

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

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

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

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

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