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

            
29
/// Lint a parsed `HurlFile` to a string.
30
84
pub fn lint_hurl_file(file: &HurlFile) -> String {
31
84
    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
384
    fn lint(&self) -> String {
41
384
        let mut s = String::new();
42
384
        self.line_terminators
43
384
            .iter()
44
384
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
45
384
        s.push_str(&self.query.lint());
46
384
        if !self.filters.is_empty() {
47
117
            s.push(' ');
48
117
            let filters = self
49
117
                .filters
50
117
                .iter()
51
183
                .map(|(_, f)| f.value.lint())
52
117
                .collect::<Vec<_>>()
53
117
                .join(" ");
54
117
            s.push_str(&filters);
55
        }
56
384
        s.push(' ');
57
384
        s.push_str(&self.predicate.lint());
58
384
        s.push_str(&lint_lt(&self.line_terminator0, true));
59
384
        s
60
    }
61
}
62

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
247
impl Lint for FilterValue {
248
147
    fn lint(&self) -> String {
249
147
        let mut s = String::new();
250
147
        s.push_str(self.identifier());
251
147
        match self {
252
6
            FilterValue::CharsetDecode { encoding, .. } => {
253
6
                s.push(' ');
254
6
                s.push_str(&encoding.lint());
255
            }
256
3
            FilterValue::Decode { encoding, .. } => {
257
3
                s.push(' ');
258
3
                s.push_str(&encoding.lint());
259
            }
260
6
            FilterValue::Format { fmt, .. } => {
261
6
                s.push(' ');
262
6
                s.push_str(&fmt.lint());
263
            }
264
9
            FilterValue::DateFormat { fmt, .. } => {
265
9
                s.push(' ');
266
9
                s.push_str(&fmt.lint());
267
            }
268
9
            FilterValue::JsonPath { expr, .. } => {
269
9
                s.push(' ');
270
9
                s.push_str(&expr.lint());
271
            }
272
3
            FilterValue::Nth { n, .. } => {
273
3
                s.push(' ');
274
3
                s.push_str(&n.lint());
275
            }
276
3
            FilterValue::Regex { value, .. } => {
277
3
                s.push(' ');
278
3
                s.push_str(&value.lint());
279
            }
280
            FilterValue::Replace {
281
15
                old_value,
282
15
                new_value,
283
                ..
284
15
            } => {
285
15
                s.push(' ');
286
15
                s.push_str(&old_value.lint());
287
15
                s.push(' ');
288
15
                s.push_str(&new_value.lint());
289
            }
290
3
            FilterValue::Split { sep, .. } => {
291
3
                s.push(' ');
292
3
                s.push_str(&sep.lint());
293
            }
294
            FilterValue::ReplaceRegex {
295
3
                pattern, new_value, ..
296
3
            } => {
297
3
                s.push(' ');
298
3
                s.push_str(&pattern.lint());
299
3
                s.push(' ');
300
3
                s.push_str(&new_value.lint());
301
            }
302
3
            FilterValue::ToDate { fmt, .. } => {
303
3
                s.push(' ');
304
3
                s.push_str(&fmt.lint());
305
            }
306
3
            FilterValue::UrlQueryParam { param, .. } => {
307
3
                s.push(' ');
308
3
                s.push_str(&param.lint());
309
            }
310
3
            FilterValue::XPath { expr, .. } => {
311
3
                s.push(' ');
312
3
                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
78
            | FilterValue::Utf8Encode => {}
334
        }
335
147
        s
336
    }
337
}
338

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
563
impl Lint for Placeholder {
564
84
    fn lint(&self) -> String {
565
84
        self.to_source().to_string()
566
    }
567
}
568

            
569
impl Lint for Predicate {
570
384
    fn lint(&self) -> String {
571
384
        let mut s = String::new();
572
384
        if self.not {
573
3
            s.push_str("not");
574
3
            s.push(' ');
575
        }
576
384
        s.push_str(&self.predicate_func.value.lint());
577
384
        s
578
    }
579
}
580

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

            
650
impl Lint for PredicateValue {
651
324
    fn lint(&self) -> String {
652
324
        match self {
653
3
            PredicateValue::Base64(value) => value.lint(),
654
3
            PredicateValue::Bool(value) => value.to_string(),
655
3
            PredicateValue::File(value) => value.lint(),
656
27
            PredicateValue::Hex(value) => value.lint(),
657
18
            PredicateValue::MultilineString(value) => value.lint(),
658
3
            PredicateValue::Null => "null".to_string(),
659
108
            PredicateValue::Number(value) => value.lint(),
660
3
            PredicateValue::Placeholder(value) => value.lint(),
661
6
            PredicateValue::Regex(value) => value.lint(),
662
150
            PredicateValue::String(value) => value.lint(),
663
        }
664
    }
665
}
666

            
667
impl Lint for Regex {
668
12
    fn lint(&self) -> String {
669
12
        self.to_source().to_string()
670
    }
671
}
672

            
673
impl Lint for RegexValue {
674
9
    fn lint(&self) -> String {
675
9
        match self {
676
3
            RegexValue::Template(value) => value.lint(),
677
6
            RegexValue::Regex(value) => value.lint(),
678
        }
679
    }
680
}
681

            
682
impl Lint for Request {
683
279
    fn lint(&self) -> String {
684
279
        let mut s = String::new();
685
279
        self.line_terminators
686
279
            .iter()
687
340
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
688
279
        s.push_str(&self.method.lint());
689
279
        s.push(' ');
690
279
        s.push_str(&self.url.lint());
691
279
        s.push_str(&lint_lt(&self.line_terminator0, true));
692

            
693
304
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
694

            
695
        // We rewrite our file and reorder the various section.
696
279
        if let Some(section) = get_option_section(self) {
697
42
            s.push_str(&section.lint());
698
        }
699
279
        if let Some(section) = get_query_params_section(self) {
700
12
            s.push_str(&section.lint());
701
        }
702
279
        if let Some(section) = get_basic_auth_section(self) {
703
3
            s.push_str(&section.lint());
704
        }
705
279
        if let Some(section) = get_form_params_section(self) {
706
6
            s.push_str(&section.lint());
707
        }
708
279
        if let Some(section) = get_multipart_section(self) {
709
6
            s.push_str(&section.lint());
710
        }
711
279
        if let Some(section) = get_cookies_section(self) {
712
9
            s.push_str(&section.lint());
713
        }
714
279
        if let Some(body) = &self.body {
715
54
            s.push_str(&body.lint());
716
        }
717
279
        s
718
    }
719
}
720

            
721
impl Lint for Response {
722
114
    fn lint(&self) -> String {
723
114
        let mut s = String::new();
724
114
        self.line_terminators
725
114
            .iter()
726
115
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
727
114
        s.push_str(&self.version.value.lint());
728
114
        s.push(' ');
729
114
        s.push_str(&self.status.value.lint());
730
114
        s.push_str(&lint_lt(&self.line_terminator0, true));
731

            
732
119
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
733

            
734
114
        if let Some(section) = get_captures_section(self) {
735
12
            s.push_str(&section.lint());
736
        }
737
114
        if let Some(section) = get_asserts_section(self) {
738
48
            s.push_str(&section.lint());
739
        }
740
114
        if let Some(body) = &self.body {
741
48
            s.push_str(&body.lint());
742
        }
743
114
        s
744
    }
745
}
746

            
747
impl Lint for Section {
748
138
    fn lint(&self) -> String {
749
138
        let mut s = String::new();
750
138
        self.line_terminators
751
138
            .iter()
752
150
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
753
138
        s.push('[');
754
138
        s.push_str(self.identifier());
755
138
        s.push(']');
756
138
        s.push_str(&lint_lt(&self.line_terminator0, true));
757
138
        s.push_str(&self.value.lint());
758
138
        s
759
    }
760
}
761

            
762
impl Lint for SectionValue {
763
138
    fn lint(&self) -> String {
764
138
        let mut s = String::new();
765
3
        match self {
766
12
            SectionValue::QueryParams(params, _) => {
767
22
                params.iter().for_each(|p| s.push_str(&p.lint()));
768
            }
769
3
            SectionValue::BasicAuth(Some(auth)) => {
770
3
                s.push_str(&auth.lint());
771
            }
772
            SectionValue::BasicAuth(_) => {}
773
6
            SectionValue::FormParams(params, _) => {
774
14
                params.iter().for_each(|p| s.push_str(&p.lint()));
775
            }
776
6
            SectionValue::MultipartFormData(params, _) => {
777
11
                params.iter().for_each(|p| s.push_str(&p.lint()));
778
            }
779
9
            SectionValue::Cookies(cookies) => {
780
12
                cookies.iter().for_each(|c| s.push_str(&c.lint()));
781
            }
782
12
            SectionValue::Captures(captures) => {
783
22
                captures.iter().for_each(|c| s.push_str(&c.lint()));
784
            }
785
48
            SectionValue::Asserts(asserts) => {
786
400
                asserts.iter().for_each(|a| s.push_str(&a.lint()));
787
            }
788
42
            SectionValue::Options(options) => {
789
347
                options.iter().for_each(|o| s.push_str(&o.lint()));
790
            }
791
        }
792
138
        s
793
    }
794
}
795

            
796
impl Lint for StatusValue {
797
114
    fn lint(&self) -> String {
798
114
        self.to_source().to_string()
799
    }
800
}
801

            
802
impl Lint for Template {
803
1191
    fn lint(&self) -> String {
804
1191
        self.to_source().to_string()
805
    }
806
}
807

            
808
impl Lint for U64 {
809
33
    fn lint(&self) -> String {
810
33
        self.to_source().to_string()
811
    }
812
}
813

            
814
impl Lint for VariableDefinition {
815
27
    fn lint(&self) -> String {
816
27
        let mut s = String::new();
817
27
        s.push_str(&self.name);
818
27
        s.push('=');
819
27
        s.push_str(&self.value.lint());
820
27
        s
821
    }
822
}
823

            
824
impl Lint for VariableValue {
825
27
    fn lint(&self) -> String {
826
27
        self.to_source().to_string()
827
    }
828
}
829

            
830
impl Lint for VersionValue {
831
114
    fn lint(&self) -> String {
832
114
        self.to_source().to_string()
833
    }
834
}
835

            
836
impl Lint for VerbosityOption {
837
6
    fn lint(&self) -> String {
838
6
        self.to_string()
839
    }
840
}
841

            
842
114
fn get_asserts_section(response: &Response) -> Option<&Section> {
843
114
    for s in &response.sections {
844
60
        if let SectionValue::Asserts(_) = s.value {
845
48
            return Some(s);
846
        }
847
    }
848
66
    None
849
}
850

            
851
114
fn get_captures_section(response: &Response) -> Option<&Section> {
852
114
    for s in &response.sections {
853
51
        if let SectionValue::Captures(_) = s.value {
854
12
            return Some(s);
855
        }
856
    }
857
102
    None
858
}
859

            
860
279
fn get_cookies_section(request: &Request) -> Option<&Section> {
861
279
    for s in &request.sections {
862
69
        if let SectionValue::Cookies(_) = s.value {
863
9
            return Some(s);
864
        }
865
    }
866
270
    None
867
}
868

            
869
279
fn get_form_params_section(request: &Request) -> Option<&Section> {
870
279
    for s in &request.sections {
871
60
        if let SectionValue::FormParams(_, _) = s.value {
872
6
            return Some(s);
873
        }
874
    }
875
273
    None
876
}
877

            
878
279
fn get_option_section(request: &Request) -> Option<&Section> {
879
279
    for s in &request.sections {
880
78
        if let SectionValue::Options(_) = s.value {
881
42
            return Some(s);
882
        }
883
    }
884
237
    None
885
}
886

            
887
279
fn get_multipart_section(request: &Request) -> Option<&Section> {
888
279
    for s in &request.sections {
889
66
        if let SectionValue::MultipartFormData(_, _) = s.value {
890
6
            return Some(s);
891
        }
892
    }
893
273
    None
894
}
895

            
896
279
fn get_query_params_section(request: &Request) -> Option<&Section> {
897
279
    for s in &request.sections {
898
51
        if let SectionValue::QueryParams(_, _) = s.value {
899
12
            return Some(s);
900
        }
901
    }
902
267
    None
903
}
904

            
905
279
fn get_basic_auth_section(request: &Request) -> Option<&Section> {
906
279
    for s in &request.sections {
907
3
        if let SectionValue::BasicAuth(Some(_)) = s.value {
908
3
            return Some(s);
909
        }
910
    }
911
276
    None
912
}
913

            
914
42
fn lint_duration_option(option: &DurationOption, default_unit: DurationUnit) -> String {
915
42
    match option {
916
30
        DurationOption::Literal(duration) => lint_duration(duration, default_unit),
917
12
        DurationOption::Placeholder(expr) => expr.lint(),
918
    }
919
}
920

            
921
30
fn lint_duration(duration: &Duration, default_unit: DurationUnit) -> String {
922
30
    let mut s = String::new();
923
30
    s.push_str(&duration.value.lint());
924
30
    let unit = duration.unit.unwrap_or(default_unit);
925
30
    s.push_str(&unit.to_string());
926
30
    s
927
}
928

            
929
#[cfg(test)]
930
mod tests {
931
    use crate::linter::lint_hurl_file;
932
    use hurl_core::parser;
933

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