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 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
    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
381
    fn lint(&self) -> String {
41
381
        let mut s = String::new();
42
381
        self.line_terminators
43
381
            .iter()
44
381
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
45
381
        s.push_str(&self.query.lint());
46
381
        if !self.filters.is_empty() {
47
114
            s.push(' ');
48
114
            let filters = self
49
114
                .filters
50
114
                .iter()
51
179
                .map(|(_, f)| f.value.lint())
52
114
                .collect::<Vec<_>>()
53
114
                .join(" ");
54
114
            s.push_str(&filters);
55
        }
56
381
        s.push(' ');
57
381
        s.push_str(&self.predicate.lint());
58
381
        s.push_str(&lint_lt(&self.line_terminator0, true));
59
381
        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
99
    fn lint(&self) -> String {
75
99
        let mut s = String::new();
76
99
        self.line_terminators
77
99
            .iter()
78
104
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
79
99
        s.push_str(&self.value.lint());
80
99
        s.push_str(&lint_lt(&self.line_terminator0, true));
81
99
        s
82
    }
83
}
84

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

            
94
impl Lint for Bytes {
95
99
    fn lint(&self) -> String {
96
99
        match self {
97
6
            Bytes::Json(value) => value.lint(),
98
3
            Bytes::Xml(value) => value.clone(),
99
42
            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
306
    fn lint(&self) -> String {
166
306
        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
282
    fn lint(&self) -> String {
187
282
        let mut s = String::new();
188
282
        s.push_str(&self.request.lint());
189
282
        if let Some(response) = &self.response {
190
114
            s.push_str(&response.lint());
191
        }
192
282
        s
193
    }
194
}
195

            
196
impl Lint for EntryOption {
197
321
    fn lint(&self) -> String {
198
321
        let mut s = String::new();
199
321
        self.line_terminators
200
321
            .iter()
201
322
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
202
321
        s.push_str(&self.kind.lint());
203
321
        s.push_str(&lint_lt(&self.line_terminator0, true));
204
321
        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
144
    fn lint(&self) -> String {
249
144
        let mut s = String::new();
250
144
        s.push_str(self.identifier());
251
144
        match self {
252
6
            FilterValue::Decode { encoding, .. } => {
253
6
                s.push(' ');
254
6
                s.push_str(&encoding.lint());
255
            }
256
6
            FilterValue::Format { fmt, .. } => {
257
6
                s.push(' ');
258
6
                s.push_str(&fmt.lint());
259
            }
260
9
            FilterValue::DateFormat { fmt, .. } => {
261
9
                s.push(' ');
262
9
                s.push_str(&fmt.lint());
263
            }
264
9
            FilterValue::JsonPath { expr, .. } => {
265
9
                s.push(' ');
266
9
                s.push_str(&expr.lint());
267
            }
268
3
            FilterValue::Nth { n, .. } => {
269
3
                s.push(' ');
270
3
                s.push_str(&n.lint());
271
            }
272
3
            FilterValue::Regex { value, .. } => {
273
3
                s.push(' ');
274
3
                s.push_str(&value.lint());
275
            }
276
            FilterValue::Replace {
277
15
                old_value,
278
15
                new_value,
279
                ..
280
15
            } => {
281
15
                s.push(' ');
282
15
                s.push_str(&old_value.lint());
283
15
                s.push(' ');
284
15
                s.push_str(&new_value.lint());
285
            }
286
3
            FilterValue::Split { sep, .. } => {
287
3
                s.push(' ');
288
3
                s.push_str(&sep.lint());
289
            }
290
            FilterValue::ReplaceRegex {
291
3
                pattern, new_value, ..
292
3
            } => {
293
3
                s.push(' ');
294
3
                s.push_str(&pattern.lint());
295
3
                s.push(' ');
296
3
                s.push_str(&new_value.lint());
297
            }
298
3
            FilterValue::ToDate { fmt, .. } => {
299
3
                s.push(' ');
300
3
                s.push_str(&fmt.lint());
301
            }
302
3
            FilterValue::UrlQueryParam { param, .. } => {
303
3
                s.push(' ');
304
3
                s.push_str(&param.lint());
305
            }
306
3
            FilterValue::XPath { expr, .. } => {
307
3
                s.push(' ');
308
3
                s.push_str(&expr.lint());
309
            }
310
            FilterValue::Base64Decode
311
            | FilterValue::Base64Encode
312
            | FilterValue::Base64UrlSafeDecode
313
            | FilterValue::Base64UrlSafeEncode
314
            | FilterValue::Count
315
            | FilterValue::DaysAfterNow
316
            | FilterValue::DaysBeforeNow
317
            | FilterValue::First
318
            | FilterValue::HtmlEscape
319
            | FilterValue::HtmlUnescape
320
            | FilterValue::Last
321
            | FilterValue::Location
322
            | FilterValue::ToFloat
323
            | FilterValue::ToHex
324
            | FilterValue::ToInt
325
            | FilterValue::ToString
326
            | FilterValue::UrlDecode
327
            | FilterValue::UrlEncode
328
            | FilterValue::Utf8Decode
329
78
            | FilterValue::Utf8Encode => {}
330
        }
331
144
        s
332
    }
333
}
334

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

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

            
356
impl Lint for IntegerValue {
357
3
    fn lint(&self) -> String {
358
3
        match self {
359
3
            IntegerValue::Literal(value) => value.lint(),
360
            IntegerValue::Placeholder(value) => value.lint(),
361
        }
362
    }
363
}
364

            
365
impl Lint for I64 {
366
3
    fn lint(&self) -> String {
367
3
        self.to_source().to_string()
368
    }
369
}
370

            
371
impl Lint for JsonValue {
372
6
    fn lint(&self) -> String {
373
6
        self.to_source().to_string()
374
    }
375
}
376

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

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

            
413
impl Lint for Method {
414
282
    fn lint(&self) -> String {
415
282
        self.to_source().to_string()
416
    }
417
}
418

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

            
430
impl Lint for MultilineString {
431
60
    fn lint(&self) -> String {
432
60
        self.to_source().to_string()
433
    }
434
}
435

            
436
impl Lint for NaturalOption {
437
6
    fn lint(&self) -> String {
438
6
        match self {
439
3
            NaturalOption::Literal(value) => value.lint(),
440
3
            NaturalOption::Placeholder(value) => value.lint(),
441
        }
442
    }
443
}
444

            
445
impl Lint for Number {
446
108
    fn lint(&self) -> String {
447
108
        self.to_source().to_string()
448
    }
449
}
450

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

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

            
556
impl Lint for Placeholder {
557
81
    fn lint(&self) -> String {
558
81
        self.to_source().to_string()
559
    }
560
}
561

            
562
impl Lint for Predicate {
563
381
    fn lint(&self) -> String {
564
381
        let mut s = String::new();
565
381
        if self.not {
566
3
            s.push_str("not");
567
3
            s.push(' ');
568
        }
569
381
        s.push_str(&self.predicate_func.value.lint());
570
381
        s
571
    }
572
}
573

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

            
643
impl Lint for PredicateValue {
644
321
    fn lint(&self) -> String {
645
321
        match self {
646
3
            PredicateValue::Base64(value) => value.lint(),
647
3
            PredicateValue::Bool(value) => value.to_string(),
648
3
            PredicateValue::File(value) => value.lint(),
649
27
            PredicateValue::Hex(value) => value.lint(),
650
18
            PredicateValue::MultilineString(value) => value.lint(),
651
3
            PredicateValue::Null => "null".to_string(),
652
108
            PredicateValue::Number(value) => value.lint(),
653
3
            PredicateValue::Placeholder(value) => value.lint(),
654
6
            PredicateValue::Regex(value) => value.lint(),
655
147
            PredicateValue::String(value) => value.lint(),
656
        }
657
    }
658
}
659

            
660
impl Lint for Regex {
661
12
    fn lint(&self) -> String {
662
12
        self.to_source().to_string()
663
    }
664
}
665

            
666
impl Lint for RegexValue {
667
9
    fn lint(&self) -> String {
668
9
        match self {
669
3
            RegexValue::Template(value) => value.lint(),
670
6
            RegexValue::Regex(value) => value.lint(),
671
        }
672
    }
673
}
674

            
675
impl Lint for Request {
676
282
    fn lint(&self) -> String {
677
282
        let mut s = String::new();
678
282
        self.line_terminators
679
282
            .iter()
680
346
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
681
282
        s.push_str(&self.method.lint());
682
282
        s.push(' ');
683
282
        s.push_str(&self.url.lint());
684
282
        s.push_str(&lint_lt(&self.line_terminator0, true));
685

            
686
307
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
687

            
688
        // We rewrite our file and reorder the various section.
689
282
        if let Some(section) = get_option_section(self) {
690
42
            s.push_str(&section.lint());
691
        }
692
282
        if let Some(section) = get_query_params_section(self) {
693
12
            s.push_str(&section.lint());
694
        }
695
282
        if let Some(section) = get_basic_auth_section(self) {
696
3
            s.push_str(&section.lint());
697
        }
698
282
        if let Some(section) = get_form_params_section(self) {
699
6
            s.push_str(&section.lint());
700
        }
701
282
        if let Some(section) = get_multipart_section(self) {
702
6
            s.push_str(&section.lint());
703
        }
704
282
        if let Some(section) = get_cookies_section(self) {
705
9
            s.push_str(&section.lint());
706
        }
707
282
        if let Some(body) = &self.body {
708
54
            s.push_str(&body.lint());
709
        }
710
282
        s
711
    }
712
}
713

            
714
impl Lint for Response {
715
114
    fn lint(&self) -> String {
716
114
        let mut s = String::new();
717
114
        self.line_terminators
718
114
            .iter()
719
116
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
720
114
        s.push_str(&self.version.value.lint());
721
114
        s.push(' ');
722
114
        s.push_str(&self.status.value.lint());
723
114
        s.push_str(&lint_lt(&self.line_terminator0, true));
724

            
725
119
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
726

            
727
114
        if let Some(section) = get_captures_section(self) {
728
12
            s.push_str(&section.lint());
729
        }
730
114
        if let Some(section) = get_asserts_section(self) {
731
48
            s.push_str(&section.lint());
732
        }
733
114
        if let Some(body) = &self.body {
734
45
            s.push_str(&body.lint());
735
        }
736
114
        s
737
    }
738
}
739

            
740
impl Lint for Section {
741
138
    fn lint(&self) -> String {
742
138
        let mut s = String::new();
743
138
        self.line_terminators
744
138
            .iter()
745
148
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
746
138
        s.push('[');
747
138
        s.push_str(self.identifier());
748
138
        s.push(']');
749
138
        s.push_str(&lint_lt(&self.line_terminator0, true));
750
138
        s.push_str(&self.value.lint());
751
138
        s
752
    }
753
}
754

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

            
789
impl Lint for StatusValue {
790
114
    fn lint(&self) -> String {
791
114
        self.to_source().to_string()
792
    }
793
}
794

            
795
impl Lint for Template {
796
1188
    fn lint(&self) -> String {
797
1188
        self.to_source().to_string()
798
    }
799
}
800

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

            
807
impl Lint for VariableDefinition {
808
27
    fn lint(&self) -> String {
809
27
        let mut s = String::new();
810
27
        s.push_str(&self.name);
811
27
        s.push('=');
812
27
        s.push_str(&self.value.lint());
813
27
        s
814
    }
815
}
816

            
817
impl Lint for VariableValue {
818
27
    fn lint(&self) -> String {
819
27
        self.to_source().to_string()
820
    }
821
}
822

            
823
impl Lint for VersionValue {
824
114
    fn lint(&self) -> String {
825
114
        self.to_source().to_string()
826
    }
827
}
828

            
829
114
fn get_asserts_section(response: &Response) -> Option<&Section> {
830
126
    for s in &response.sections {
831
60
        if let SectionValue::Asserts(_) = s.value {
832
48
            return Some(s);
833
        }
834
    }
835
66
    None
836
}
837

            
838
114
fn get_captures_section(response: &Response) -> Option<&Section> {
839
153
    for s in &response.sections {
840
51
        if let SectionValue::Captures(_) = s.value {
841
12
            return Some(s);
842
        }
843
    }
844
102
    None
845
}
846

            
847
282
fn get_cookies_section(request: &Request) -> Option<&Section> {
848
342
    for s in &request.sections {
849
69
        if let SectionValue::Cookies(_) = s.value {
850
9
            return Some(s);
851
        }
852
    }
853
273
    None
854
}
855

            
856
282
fn get_form_params_section(request: &Request) -> Option<&Section> {
857
336
    for s in &request.sections {
858
60
        if let SectionValue::FormParams(_, _) = s.value {
859
6
            return Some(s);
860
        }
861
    }
862
276
    None
863
}
864

            
865
282
fn get_option_section(request: &Request) -> Option<&Section> {
866
318
    for s in &request.sections {
867
78
        if let SectionValue::Options(_) = s.value {
868
42
            return Some(s);
869
        }
870
    }
871
240
    None
872
}
873

            
874
282
fn get_multipart_section(request: &Request) -> Option<&Section> {
875
342
    for s in &request.sections {
876
66
        if let SectionValue::MultipartFormData(_, _) = s.value {
877
6
            return Some(s);
878
        }
879
    }
880
276
    None
881
}
882

            
883
282
fn get_query_params_section(request: &Request) -> Option<&Section> {
884
321
    for s in &request.sections {
885
51
        if let SectionValue::QueryParams(_, _) = s.value {
886
12
            return Some(s);
887
        }
888
    }
889
270
    None
890
}
891

            
892
282
fn get_basic_auth_section(request: &Request) -> Option<&Section> {
893
345
    for s in &request.sections {
894
3
        if let SectionValue::BasicAuth(Some(_)) = s.value {
895
3
            return Some(s);
896
        }
897
    }
898
279
    None
899
}
900

            
901
42
fn lint_duration_option(option: &DurationOption, default_unit: DurationUnit) -> String {
902
42
    match option {
903
30
        DurationOption::Literal(duration) => lint_duration(duration, default_unit),
904
12
        DurationOption::Placeholder(expr) => expr.lint(),
905
    }
906
}
907

            
908
30
fn lint_duration(duration: &Duration, default_unit: DurationUnit) -> String {
909
30
    let mut s = String::new();
910
30
    s.push_str(&duration.value.lint());
911
30
    let unit = duration.unit.unwrap_or(default_unit);
912
30
    s.push_str(&unit.to_string());
913
30
    s
914
}
915

            
916
#[cfg(test)]
917
mod tests {
918
    use crate::linter::lint_hurl_file;
919
    use hurl_core::parser;
920

            
921
    #[test]
922
    fn test_lint_hurl_file() {
923
        let src = r#"
924
    # comment 1
925
  #comment 2 with trailing spaces    
926
  GET   https://foo.com
927
[Form]
928
  bar : baz
929
  [Options]
930
 location : true
931
HTTP   200"#;
932
        let file = parser::parse_hurl_file(src).unwrap();
933
        let linted = lint_hurl_file(&file);
934
        assert_eq!(
935
            linted,
936
            r#"
937
# comment 1
938
#comment 2 with trailing spaces
939
GET https://foo.com
940
[Options]
941
location: true
942
[Form]
943
bar: baz
944
HTTP 200
945
"#
946
        );
947
    }
948
}