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 hurl_core::ast::{
19
    Assert, Base64, Body, BooleanOption, Bytes, Capture, CertificateAttributeName, Comment, Cookie,
20
    CookiePath, CountOption, Duration, DurationOption, Entry, EntryOption, File, FilenameParam,
21
    FilenameValue, FilterValue, Hex, HurlFile, I64, IntegerValue, JsonValue, KeyValue,
22
    LineTerminator, Method, MultilineString, MultipartParam, NaturalOption, Number, OptionKind,
23
    Placeholder, Predicate, PredicateFuncValue, PredicateValue, Query, QueryValue, Regex,
24
    RegexValue, Request, Response, Section, SectionValue, StatusValue, Template, U64,
25
    VariableDefinition, VariableValue, VerbosityOption, VersionValue,
26
};
27
use hurl_core::types::{Count, DurationUnit, ToSource};
28

            
29
/// Lint a parsed `HurlFile` to a string.
30
56
pub fn lint_hurl_file(file: &HurlFile) -> String {
31
56
    file.lint()
32
}
33

            
34
/// Lint something (usually a Hurl AST node) to a string.
35
trait Lint {
36
    fn lint(&self) -> String;
37
}
38

            
39
impl Lint for Assert {
40
256
    fn lint(&self) -> String {
41
256
        let mut s = String::new();
42
256
        self.line_terminators
43
256
            .iter()
44
256
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
45
256
        s.push_str(&self.query.lint());
46
256
        if !self.filters.is_empty() {
47
78
            s.push(' ');
48
78
            let filters = self
49
78
                .filters
50
78
                .iter()
51
96
                .map(|(_, f)| f.value.lint())
52
78
                .collect::<Vec<_>>()
53
78
                .join(" ");
54
78
            s.push_str(&filters);
55
        }
56
256
        s.push(' ');
57
256
        s.push_str(&self.predicate.lint());
58
256
        s.push_str(&lint_lt(&self.line_terminator0, true));
59
256
        s
60
    }
61
}
62

            
63
impl Lint for Base64 {
64
10
    fn lint(&self) -> String {
65
10
        let mut s = String::new();
66
10
        s.push_str("base64,");
67
10
        s.push_str(self.source.as_str());
68
10
        s.push(';');
69
10
        s
70
    }
71
}
72

            
73
impl Lint for Body {
74
72
    fn lint(&self) -> String {
75
72
        let mut s = String::new();
76
72
        self.line_terminators
77
72
            .iter()
78
72
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
79
72
        s.push_str(&self.value.lint());
80
72
        s.push_str(&lint_lt(&self.line_terminator0, true));
81
72
        s
82
    }
83
}
84

            
85
impl Lint for BooleanOption {
86
98
    fn lint(&self) -> String {
87
98
        match self {
88
58
            BooleanOption::Literal(value) => value.to_string(),
89
40
            BooleanOption::Placeholder(value) => value.lint(),
90
        }
91
    }
92
}
93

            
94
impl Lint for Bytes {
95
72
    fn lint(&self) -> String {
96
72
        match self {
97
4
            Bytes::Json(value) => value.lint(),
98
2
            Bytes::Xml(value) => value.clone(),
99
34
            Bytes::MultilineString(value) => value.lint(),
100
10
            Bytes::OnelineString(value) => value.lint(),
101
8
            Bytes::Base64(value) => value.lint(),
102
8
            Bytes::File(value) => value.lint(),
103
6
            Bytes::Hex(value) => value.lint(),
104
        }
105
    }
106
}
107

            
108
impl Lint for Capture {
109
12
    fn lint(&self) -> String {
110
12
        let mut s = String::new();
111
12
        self.line_terminators
112
12
            .iter()
113
12
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
114
12
        s.push_str(&self.name.lint());
115
12
        s.push(':');
116
12
        s.push(' ');
117
12
        s.push_str(&self.query.lint());
118
12
        if !self.filters.is_empty() {
119
2
            s.push(' ');
120
2
            let filters = self
121
2
                .filters
122
2
                .iter()
123
2
                .map(|(_, f)| f.value.lint())
124
2
                .collect::<Vec<_>>()
125
2
                .join(" ");
126
2
            s.push_str(&filters);
127
        }
128
12
        if self.redacted {
129
4
            s.push(' ');
130
4
            s.push_str("redact");
131
        }
132
12
        s.push_str(&lint_lt(&self.line_terminator0, true));
133
12
        s
134
    }
135
}
136

            
137
impl Lint for CertificateAttributeName {
138
22
    fn lint(&self) -> String {
139
22
        self.to_source().to_string()
140
    }
141
}
142

            
143
impl Lint for Cookie {
144
6
    fn lint(&self) -> String {
145
6
        let mut s = String::new();
146
6
        self.line_terminators
147
6
            .iter()
148
6
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
149
6
        s.push_str(&self.name.lint());
150
6
        s.push(':');
151
6
        s.push(' ');
152
6
        s.push_str(&self.value.lint());
153
6
        s.push_str(&lint_lt(&self.line_terminator0, true));
154
6
        s
155
    }
156
}
157

            
158
impl Lint for CookiePath {
159
6
    fn lint(&self) -> String {
160
6
        self.to_source().to_string()
161
    }
162
}
163

            
164
impl Lint for Comment {
165
212
    fn lint(&self) -> String {
166
212
        format!("#{}", self.value.trim_end())
167
    }
168
}
169

            
170
impl Lint for Count {
171
14
    fn lint(&self) -> String {
172
14
        self.to_string()
173
    }
174
}
175

            
176
impl Lint for CountOption {
177
20
    fn lint(&self) -> String {
178
20
        match self {
179
14
            CountOption::Literal(value) => value.lint(),
180
6
            CountOption::Placeholder(value) => value.lint(),
181
        }
182
    }
183
}
184

            
185
impl Lint for Entry {
186
190
    fn lint(&self) -> String {
187
190
        let mut s = String::new();
188
190
        s.push_str(&self.request.lint());
189
190
        if let Some(response) = &self.response {
190
76
            s.push_str(&response.lint());
191
        }
192
190
        s
193
    }
194
}
195

            
196
impl Lint for EntryOption {
197
238
    fn lint(&self) -> String {
198
238
        let mut s = String::new();
199
238
        self.line_terminators
200
238
            .iter()
201
238
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
202
238
        s.push_str(&self.kind.lint());
203
238
        s.push_str(&lint_lt(&self.line_terminator0, true));
204
238
        s
205
    }
206
}
207

            
208
impl Lint for File {
209
10
    fn lint(&self) -> String {
210
10
        let mut s = String::new();
211
10
        s.push_str("file,");
212
10
        s.push_str(&self.filename.lint());
213
10
        s.push(';');
214
10
        s
215
    }
216
}
217

            
218
impl Lint for FilenameParam {
219
4
    fn lint(&self) -> String {
220
4
        let mut s = String::new();
221
4
        self.line_terminators
222
4
            .iter()
223
4
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
224
4
        s.push_str(&self.key.lint());
225
4
        s.push(':');
226
4
        s.push(' ');
227
4
        s.push_str(&self.value.lint());
228
4
        s.push_str(&lint_lt(&self.line_terminator0, true));
229
4
        s
230
    }
231
}
232

            
233
impl Lint for FilenameValue {
234
4
    fn lint(&self) -> String {
235
4
        let mut s = String::new();
236
4
        s.push_str("file,");
237
4
        s.push_str(&self.filename.lint());
238
4
        s.push(';');
239
4
        if let Some(content_type) = &self.content_type {
240
2
            s.push(' ');
241
2
            s.push_str(&content_type.lint());
242
        }
243
4
        s
244
    }
245
}
246

            
247
impl Lint for FilterValue {
248
98
    fn lint(&self) -> String {
249
98
        let mut s = String::new();
250
98
        s.push_str(self.identifier());
251
98
        match self {
252
4
            FilterValue::CharsetDecode { encoding, .. } => {
253
4
                s.push(' ');
254
4
                s.push_str(&encoding.lint());
255
            }
256
2
            FilterValue::Decode { encoding, .. } => {
257
2
                s.push(' ');
258
2
                s.push_str(&encoding.lint());
259
            }
260
4
            FilterValue::Format { fmt, .. } => {
261
4
                s.push(' ');
262
4
                s.push_str(&fmt.lint());
263
            }
264
6
            FilterValue::DateFormat { fmt, .. } => {
265
6
                s.push(' ');
266
6
                s.push_str(&fmt.lint());
267
            }
268
6
            FilterValue::JsonPath { expr, .. } => {
269
6
                s.push(' ');
270
6
                s.push_str(&expr.lint());
271
            }
272
2
            FilterValue::Nth { n, .. } => {
273
2
                s.push(' ');
274
2
                s.push_str(&n.lint());
275
            }
276
2
            FilterValue::Regex { value, .. } => {
277
2
                s.push(' ');
278
2
                s.push_str(&value.lint());
279
            }
280
            FilterValue::Replace {
281
10
                old_value,
282
10
                new_value,
283
                ..
284
10
            } => {
285
10
                s.push(' ');
286
10
                s.push_str(&old_value.lint());
287
10
                s.push(' ');
288
10
                s.push_str(&new_value.lint());
289
            }
290
2
            FilterValue::Split { sep, .. } => {
291
2
                s.push(' ');
292
2
                s.push_str(&sep.lint());
293
            }
294
            FilterValue::ReplaceRegex {
295
2
                pattern, new_value, ..
296
2
            } => {
297
2
                s.push(' ');
298
2
                s.push_str(&pattern.lint());
299
2
                s.push(' ');
300
2
                s.push_str(&new_value.lint());
301
            }
302
2
            FilterValue::ToDate { fmt, .. } => {
303
2
                s.push(' ');
304
2
                s.push_str(&fmt.lint());
305
            }
306
2
            FilterValue::UrlQueryParam { param, .. } => {
307
2
                s.push(' ');
308
2
                s.push_str(&param.lint());
309
            }
310
2
            FilterValue::XPath { expr, .. } => {
311
2
                s.push(' ');
312
2
                s.push_str(&expr.lint());
313
            }
314
            FilterValue::Base64Decode
315
            | FilterValue::Base64Encode
316
            | FilterValue::Base64UrlSafeDecode
317
            | FilterValue::Base64UrlSafeEncode
318
            | FilterValue::Count
319
            | FilterValue::DaysAfterNow
320
            | FilterValue::DaysBeforeNow
321
            | FilterValue::First
322
            | FilterValue::HtmlEscape
323
            | FilterValue::HtmlUnescape
324
            | FilterValue::Last
325
            | FilterValue::Location
326
            | FilterValue::ToFloat
327
            | FilterValue::ToHex
328
            | FilterValue::ToInt
329
            | FilterValue::ToString
330
            | FilterValue::UrlDecode
331
            | FilterValue::UrlEncode
332
            | FilterValue::Utf8Decode
333
52
            | FilterValue::Utf8Encode => {}
334
        }
335
98
        s
336
    }
337
}
338

            
339
impl Lint for Hex {
340
24
    fn lint(&self) -> String {
341
24
        let mut s = String::new();
342
24
        s.push_str("hex,");
343
24
        s.push_str(self.source.as_str());
344
24
        s.push(';');
345
24
        s
346
    }
347
}
348

            
349
impl Lint for HurlFile {
350
56
    fn lint(&self) -> String {
351
56
        let mut s = String::new();
352
190
        self.entries.iter().for_each(|e| s.push_str(&e.lint()));
353
56
        self.line_terminators
354
56
            .iter()
355
56
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
356
56
        s
357
    }
358
}
359

            
360
impl Lint for IntegerValue {
361
2
    fn lint(&self) -> String {
362
2
        match self {
363
2
            IntegerValue::Literal(value) => value.lint(),
364
            IntegerValue::Placeholder(value) => value.lint(),
365
        }
366
    }
367
}
368

            
369
impl Lint for I64 {
370
2
    fn lint(&self) -> String {
371
2
        self.to_source().to_string()
372
    }
373
}
374

            
375
impl Lint for JsonValue {
376
4
    fn lint(&self) -> String {
377
4
        self.to_source().to_string()
378
    }
379
}
380

            
381
impl Lint for KeyValue {
382
88
    fn lint(&self) -> String {
383
88
        let mut s = String::new();
384
88
        self.line_terminators
385
88
            .iter()
386
88
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
387
88
        s.push_str(&self.key.lint());
388
88
        s.push(':');
389
88
        if !self.value.is_empty() {
390
84
            s.push(' ');
391
84
            s.push_str(&self.value.lint());
392
        }
393
88
        s.push_str(&lint_lt(&self.line_terminator0, true));
394
88
        s
395
    }
396
}
397

            
398
1218
fn lint_lt(lt: &LineTerminator, is_trailing: bool) -> String {
399
1218
    let mut s = String::new();
400
1218
    if let Some(comment) = &lt.comment {
401
212
        if is_trailing {
402
154
            // if line terminator is a trailing terminator, we keep the leading whitespaces
403
154
            // to keep user alignment.
404
154
            s.push_str(lt.space0.as_str());
405
        }
406
212
        s.push_str(&comment.lint());
407
1006
    };
408
    // We always terminate a file by a newline
409
1218
    if lt.newline.value.is_empty() {
410
2
        s.push('\n');
411
1216
    } else {
412
1216
        s.push_str(&lt.newline.value);
413
    }
414
1218
    s
415
}
416

            
417
impl Lint for Method {
418
190
    fn lint(&self) -> String {
419
190
        self.to_source().to_string()
420
    }
421
}
422

            
423
impl Lint for MultipartParam {
424
6
    fn lint(&self) -> String {
425
6
        let mut s = String::new();
426
6
        match self {
427
2
            MultipartParam::Param(param) => s.push_str(&param.lint()),
428
4
            MultipartParam::FilenameParam(param) => s.push_str(&param.lint()),
429
        }
430
6
        s
431
    }
432
}
433

            
434
impl Lint for MultilineString {
435
46
    fn lint(&self) -> String {
436
46
        self.to_source().to_string()
437
    }
438
}
439

            
440
impl Lint for NaturalOption {
441
4
    fn lint(&self) -> String {
442
4
        match self {
443
2
            NaturalOption::Literal(value) => value.lint(),
444
2
            NaturalOption::Placeholder(value) => value.lint(),
445
        }
446
    }
447
}
448

            
449
impl Lint for Number {
450
72
    fn lint(&self) -> String {
451
72
        self.to_source().to_string()
452
    }
453
}
454

            
455
impl Lint for OptionKind {
456
238
    fn lint(&self) -> String {
457
238
        let mut s = String::new();
458
238
        s.push_str(self.identifier());
459
238
        s.push(':');
460
238
        s.push(' ');
461
238
        let value = match self {
462
4
            OptionKind::AwsSigV4(value) => value.lint(),
463
4
            OptionKind::CaCertificate(value) => value.lint(),
464
6
            OptionKind::ClientCert(value) => value.lint(),
465
4
            OptionKind::ClientKey(value) => value.lint(),
466
4
            OptionKind::Compressed(value) => value.lint(),
467
4
            OptionKind::ConnectTo(value) => value.lint(),
468
4
            OptionKind::ConnectTimeout(value) => {
469
4
                lint_duration_option(value, DurationUnit::MilliSecond)
470
            }
471
12
            OptionKind::Delay(value) => lint_duration_option(value, DurationUnit::MilliSecond),
472
4
            OptionKind::Digest(value) => value.lint(),
473
4
            OptionKind::FailWithBody(value) => value.lint(),
474
4
            OptionKind::Header(value) => value.lint(),
475
4
            OptionKind::Http10(value) => value.lint(),
476
4
            OptionKind::Http11(value) => value.lint(),
477
4
            OptionKind::Http2(value) => value.lint(),
478
2
            OptionKind::Http2PriorKnowledge(value) => value.lint(),
479
4
            OptionKind::Http3(value) => value.lint(),
480
6
            OptionKind::Insecure(value) => value.lint(),
481
4
            OptionKind::IpV4(value) => value.lint(),
482
4
            OptionKind::IpV6(value) => value.lint(),
483
6
            OptionKind::FollowLocation(value) => value.lint(),
484
4
            OptionKind::FollowLocationTrusted(value) => value.lint(),
485
4
            OptionKind::LimitRate(value) => value.lint(),
486
4
            OptionKind::MaxRedirect(value) => value.lint(),
487
4
            OptionKind::MaxTime(value) => lint_duration_option(value, DurationUnit::MilliSecond),
488
6
            OptionKind::Negotiate(value) => value.lint(),
489
4
            OptionKind::NetRc(value) => value.lint(),
490
4
            OptionKind::NetRcFile(value) => value.lint(),
491
4
            OptionKind::NetRcOptional(value) => value.lint(),
492
4
            OptionKind::NoHeader(value) => value.lint(),
493
2
            OptionKind::NoJsonpathCoercion(value) => value.lint(),
494
6
            OptionKind::Ntlm(value) => value.lint(),
495
4
            OptionKind::Output(value) => value.lint(),
496
4
            OptionKind::PathAsIs(value) => value.lint(),
497
4
            OptionKind::PinnedPublicKey(value) => value.lint(),
498
4
            OptionKind::Proxy(value) => value.lint(),
499
6
            OptionKind::Repeat(value) => value.lint(),
500
4
            OptionKind::Resolve(value) => value.lint(),
501
10
            OptionKind::Retry(value) => value.lint(),
502
8
            OptionKind::RetryInterval(value) => {
503
8
                lint_duration_option(value, DurationUnit::MilliSecond)
504
            }
505
4
            OptionKind::Skip(value) => value.lint(),
506
4
            OptionKind::TlsMax(value) => value.lint(),
507
4
            OptionKind::UnixSocket(value) => value.lint(),
508
8
            OptionKind::User(value) => value.lint(),
509
18
            OptionKind::Variable(value) => value.lint(),
510
10
            OptionKind::Verbose(value) => value.lint(),
511
4
            OptionKind::Verbosity(value) => value.lint(),
512
4
            OptionKind::VeryVerbose(value) => value.lint(),
513
        };
514
238
        s.push_str(&value);
515
238
        s
516
    }
517
}
518

            
519
impl Lint for Query {
520
268
    fn lint(&self) -> String {
521
268
        let mut s = String::new();
522
268
        s.push_str(self.value.identifier());
523
268
        match &self.value {
524
4
            QueryValue::Status => {}
525
2
            QueryValue::Version => {}
526
2
            QueryValue::Url => {}
527
8
            QueryValue::Header { name, .. } => {
528
8
                s.push(' ');
529
8
                s.push_str(&name.lint());
530
            }
531
6
            QueryValue::Cookie { expr, .. } => {
532
6
                s.push(' ');
533
6
                s.push_str(&expr.lint());
534
            }
535
22
            QueryValue::Body => {}
536
2
            QueryValue::Xpath { expr, .. } => {
537
2
                s.push(' ');
538
2
                s.push_str(&expr.lint());
539
            }
540
162
            QueryValue::Jsonpath { expr, .. } => {
541
162
                s.push(' ');
542
162
                s.push_str(&expr.lint());
543
            }
544
2
            QueryValue::Regex { value, .. } => {
545
2
                s.push(' ');
546
2
                s.push_str(&value.lint());
547
            }
548
6
            QueryValue::Variable { name, .. } => {
549
6
                s.push(' ');
550
6
                s.push_str(&name.lint());
551
            }
552
2
            QueryValue::Duration => {}
553
18
            QueryValue::Bytes => {}
554
            QueryValue::RawBytes => {}
555
4
            QueryValue::Sha256 => {}
556
2
            QueryValue::Md5 => {}
557
22
            QueryValue::Certificate { attribute_name, .. } => {
558
22
                s.push(' ');
559
22
                s.push_str(&attribute_name.lint());
560
            }
561
4
            QueryValue::Ip => {}
562
            QueryValue::Redirects => {}
563
        }
564
268
        s
565
    }
566
}
567

            
568
impl Lint for Placeholder {
569
58
    fn lint(&self) -> String {
570
58
        self.to_source().to_string()
571
    }
572
}
573

            
574
impl Lint for Predicate {
575
256
    fn lint(&self) -> String {
576
256
        let mut s = String::new();
577
256
        if self.not {
578
2
            s.push_str("not");
579
2
            s.push(' ');
580
        }
581
256
        s.push_str(&self.predicate_func.value.lint());
582
256
        s
583
    }
584
}
585

            
586
impl Lint for PredicateFuncValue {
587
256
    fn lint(&self) -> String {
588
256
        let mut s = String::new();
589
256
        s.push_str(self.identifier());
590
256
        match self {
591
168
            PredicateFuncValue::Equal { value, .. } => {
592
168
                s.push(' ');
593
168
                s.push_str(&value.lint());
594
            }
595
6
            PredicateFuncValue::NotEqual { value, .. } => {
596
6
                s.push(' ');
597
6
                s.push_str(&value.lint());
598
            }
599
6
            PredicateFuncValue::GreaterThan { value, .. } => {
600
6
                s.push(' ');
601
6
                s.push_str(&value.lint());
602
            }
603
2
            PredicateFuncValue::GreaterThanOrEqual { value, .. } => {
604
2
                s.push(' ');
605
2
                s.push_str(&value.lint());
606
            }
607
8
            PredicateFuncValue::LessThan { value, .. } => {
608
8
                s.push(' ');
609
8
                s.push_str(&value.lint());
610
            }
611
2
            PredicateFuncValue::LessThanOrEqual { value, .. } => {
612
2
                s.push(' ');
613
2
                s.push_str(&value.lint());
614
            }
615
6
            PredicateFuncValue::StartWith { value, .. } => {
616
6
                s.push(' ');
617
6
                s.push_str(&value.lint());
618
            }
619
4
            PredicateFuncValue::EndWith { value, .. } => {
620
4
                s.push(' ');
621
4
                s.push_str(&value.lint());
622
            }
623
6
            PredicateFuncValue::Contain { value, .. } => {
624
6
                s.push(' ');
625
6
                s.push_str(&value.lint());
626
            }
627
2
            PredicateFuncValue::Include { value, .. } => {
628
2
                s.push(' ');
629
2
                s.push_str(&value.lint());
630
            }
631
6
            PredicateFuncValue::Match { value, .. } => {
632
6
                s.push(' ');
633
6
                s.push_str(&value.lint());
634
            }
635
            PredicateFuncValue::Exist
636
            | PredicateFuncValue::IsBoolean
637
            | PredicateFuncValue::IsCollection
638
            | PredicateFuncValue::IsDate
639
            | PredicateFuncValue::IsEmpty
640
            | PredicateFuncValue::IsFloat
641
            | PredicateFuncValue::IsInteger
642
            | PredicateFuncValue::IsIpv4
643
            | PredicateFuncValue::IsIpv6
644
            | PredicateFuncValue::IsIsoDate
645
            | PredicateFuncValue::IsList
646
            | PredicateFuncValue::IsNumber
647
            | PredicateFuncValue::IsObject
648
            | PredicateFuncValue::IsString
649
40
            | PredicateFuncValue::IsUuid => {}
650
        }
651
256
        s
652
    }
653
}
654

            
655
impl Lint for PredicateValue {
656
216
    fn lint(&self) -> String {
657
216
        match self {
658
2
            PredicateValue::Base64(value) => value.lint(),
659
2
            PredicateValue::Bool(value) => value.to_string(),
660
2
            PredicateValue::File(value) => value.lint(),
661
18
            PredicateValue::Hex(value) => value.lint(),
662
12
            PredicateValue::MultilineString(value) => value.lint(),
663
2
            PredicateValue::Null => "null".to_string(),
664
72
            PredicateValue::Number(value) => value.lint(),
665
2
            PredicateValue::Placeholder(value) => value.lint(),
666
4
            PredicateValue::Regex(value) => value.lint(),
667
100
            PredicateValue::String(value) => value.lint(),
668
        }
669
    }
670
}
671

            
672
impl Lint for Regex {
673
8
    fn lint(&self) -> String {
674
8
        self.to_source().to_string()
675
    }
676
}
677

            
678
impl Lint for RegexValue {
679
6
    fn lint(&self) -> String {
680
6
        match self {
681
2
            RegexValue::Template(value) => value.lint(),
682
4
            RegexValue::Regex(value) => value.lint(),
683
        }
684
    }
685
}
686

            
687
impl Lint for Request {
688
190
    fn lint(&self) -> String {
689
190
        let mut s = String::new();
690
190
        self.line_terminators
691
190
            .iter()
692
190
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
693
190
        s.push_str(&self.method.lint());
694
190
        s.push(' ');
695
190
        s.push_str(&self.url.lint());
696
190
        s.push_str(&lint_lt(&self.line_terminator0, true));
697

            
698
190
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
699

            
700
        // We rewrite our file and reorder the various section.
701
190
        if let Some(section) = get_option_section(self) {
702
28
            s.push_str(&section.lint());
703
        }
704
190
        if let Some(section) = get_query_params_section(self) {
705
8
            s.push_str(&section.lint());
706
        }
707
190
        if let Some(section) = get_basic_auth_section(self) {
708
2
            s.push_str(&section.lint());
709
        }
710
190
        if let Some(section) = get_form_params_section(self) {
711
4
            s.push_str(&section.lint());
712
        }
713
190
        if let Some(section) = get_multipart_section(self) {
714
4
            s.push_str(&section.lint());
715
        }
716
190
        if let Some(section) = get_cookies_section(self) {
717
6
            s.push_str(&section.lint());
718
        }
719
190
        if let Some(body) = &self.body {
720
40
            s.push_str(&body.lint());
721
        }
722
190
        s
723
    }
724
}
725

            
726
impl Lint for Response {
727
76
    fn lint(&self) -> String {
728
76
        let mut s = String::new();
729
76
        self.line_terminators
730
76
            .iter()
731
76
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
732
76
        s.push_str(&self.version.value.lint());
733
76
        s.push(' ');
734
76
        s.push_str(&self.status.value.lint());
735
76
        s.push_str(&lint_lt(&self.line_terminator0, true));
736

            
737
76
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
738

            
739
76
        if let Some(section) = get_captures_section(self) {
740
8
            s.push_str(&section.lint());
741
        }
742
76
        if let Some(section) = get_asserts_section(self) {
743
32
            s.push_str(&section.lint());
744
        }
745
76
        if let Some(body) = &self.body {
746
32
            s.push_str(&body.lint());
747
        }
748
76
        s
749
    }
750
}
751

            
752
impl Lint for Section {
753
92
    fn lint(&self) -> String {
754
92
        let mut s = String::new();
755
92
        self.line_terminators
756
92
            .iter()
757
92
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
758
92
        s.push('[');
759
92
        s.push_str(self.identifier());
760
92
        s.push(']');
761
92
        s.push_str(&lint_lt(&self.line_terminator0, true));
762
92
        s.push_str(&self.value.lint());
763
92
        s
764
    }
765
}
766

            
767
impl Lint for SectionValue {
768
92
    fn lint(&self) -> String {
769
92
        let mut s = String::new();
770
2
        match self {
771
8
            SectionValue::QueryParams(params, _) => {
772
12
                params.iter().for_each(|p| s.push_str(&p.lint()));
773
            }
774
2
            SectionValue::BasicAuth(Some(auth)) => {
775
2
                s.push_str(&auth.lint());
776
            }
777
            SectionValue::BasicAuth(_) => {}
778
4
            SectionValue::FormParams(params, _) => {
779
8
                params.iter().for_each(|p| s.push_str(&p.lint()));
780
            }
781
4
            SectionValue::MultipartFormData(params, _) => {
782
6
                params.iter().for_each(|p| s.push_str(&p.lint()));
783
            }
784
6
            SectionValue::Cookies(cookies) => {
785
6
                cookies.iter().for_each(|c| s.push_str(&c.lint()));
786
            }
787
8
            SectionValue::Captures(captures) => {
788
12
                captures.iter().for_each(|c| s.push_str(&c.lint()));
789
            }
790
32
            SectionValue::Asserts(asserts) => {
791
256
                asserts.iter().for_each(|a| s.push_str(&a.lint()));
792
            }
793
28
            SectionValue::Options(options) => {
794
238
                options.iter().for_each(|o| s.push_str(&o.lint()));
795
            }
796
        }
797
92
        s
798
    }
799
}
800

            
801
impl Lint for StatusValue {
802
76
    fn lint(&self) -> String {
803
76
        self.to_source().to_string()
804
    }
805
}
806

            
807
impl Lint for Template {
808
814
    fn lint(&self) -> String {
809
814
        self.to_source().to_string()
810
    }
811
}
812

            
813
impl Lint for U64 {
814
22
    fn lint(&self) -> String {
815
22
        self.to_source().to_string()
816
    }
817
}
818

            
819
impl Lint for VariableDefinition {
820
18
    fn lint(&self) -> String {
821
18
        let mut s = String::new();
822
18
        s.push_str(&self.name);
823
18
        s.push('=');
824
18
        s.push_str(&self.value.lint());
825
18
        s
826
    }
827
}
828

            
829
impl Lint for VariableValue {
830
18
    fn lint(&self) -> String {
831
18
        self.to_source().to_string()
832
    }
833
}
834

            
835
impl Lint for VersionValue {
836
76
    fn lint(&self) -> String {
837
76
        self.to_source().to_string()
838
    }
839
}
840

            
841
impl Lint for VerbosityOption {
842
4
    fn lint(&self) -> String {
843
4
        self.to_string()
844
    }
845
}
846

            
847
76
fn get_asserts_section(response: &Response) -> Option<&Section> {
848
76
    for s in &response.sections {
849
40
        if let SectionValue::Asserts(_) = s.value {
850
32
            return Some(s);
851
        }
852
    }
853
44
    None
854
}
855

            
856
76
fn get_captures_section(response: &Response) -> Option<&Section> {
857
76
    for s in &response.sections {
858
34
        if let SectionValue::Captures(_) = s.value {
859
8
            return Some(s);
860
        }
861
    }
862
68
    None
863
}
864

            
865
190
fn get_cookies_section(request: &Request) -> Option<&Section> {
866
190
    for s in &request.sections {
867
46
        if let SectionValue::Cookies(_) = s.value {
868
6
            return Some(s);
869
        }
870
    }
871
184
    None
872
}
873

            
874
190
fn get_form_params_section(request: &Request) -> Option<&Section> {
875
190
    for s in &request.sections {
876
40
        if let SectionValue::FormParams(_, _) = s.value {
877
4
            return Some(s);
878
        }
879
    }
880
186
    None
881
}
882

            
883
190
fn get_option_section(request: &Request) -> Option<&Section> {
884
190
    for s in &request.sections {
885
52
        if let SectionValue::Options(_) = s.value {
886
28
            return Some(s);
887
        }
888
    }
889
162
    None
890
}
891

            
892
190
fn get_multipart_section(request: &Request) -> Option<&Section> {
893
190
    for s in &request.sections {
894
44
        if let SectionValue::MultipartFormData(_, _) = s.value {
895
4
            return Some(s);
896
        }
897
    }
898
186
    None
899
}
900

            
901
190
fn get_query_params_section(request: &Request) -> Option<&Section> {
902
190
    for s in &request.sections {
903
34
        if let SectionValue::QueryParams(_, _) = s.value {
904
8
            return Some(s);
905
        }
906
    }
907
182
    None
908
}
909

            
910
190
fn get_basic_auth_section(request: &Request) -> Option<&Section> {
911
190
    for s in &request.sections {
912
2
        if let SectionValue::BasicAuth(Some(_)) = s.value {
913
2
            return Some(s);
914
        }
915
    }
916
188
    None
917
}
918

            
919
28
fn lint_duration_option(option: &DurationOption, default_unit: DurationUnit) -> String {
920
28
    match option {
921
20
        DurationOption::Literal(duration) => lint_duration(duration, default_unit),
922
8
        DurationOption::Placeholder(expr) => expr.lint(),
923
    }
924
}
925

            
926
20
fn lint_duration(duration: &Duration, default_unit: DurationUnit) -> String {
927
20
    let mut s = String::new();
928
20
    s.push_str(&duration.value.lint());
929
20
    let unit = duration.unit.unwrap_or(default_unit);
930
20
    s.push_str(&unit.to_string());
931
20
    s
932
}
933

            
934
#[cfg(test)]
935
mod tests {
936
    use crate::linter::lint_hurl_file;
937
    use hurl_core::parser;
938

            
939
    #[test]
940
    fn test_lint_hurl_file() {
941
        let src = r#"
942
    # comment 1
943
  #comment 2 with trailing spaces    
944
  GET   https://foo.com
945
[Form]
946
  bar : baz
947
  [Options]
948
 location : true
949
HTTP   200"#;
950
        let file = parser::parse_hurl_file(src).unwrap();
951
        let linted = lint_hurl_file(&file);
952
        assert_eq!(
953
            linted,
954
            r#"
955
# comment 1
956
#comment 2 with trailing spaces
957
GET https://foo.com
958
[Options]
959
location: true
960
[Form]
961
bar: baz
962
HTTP 200
963
"#
964
        );
965
    }
966
}