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::typing::{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
369
    fn lint(&self) -> String {
41
369
        let mut s = String::new();
42
369
        self.line_terminators
43
369
            .iter()
44
369
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
45
369
        s.push_str(&self.query.lint());
46
369
        if !self.filters.is_empty() {
47
108
            s.push(' ');
48
108
            let filters = self
49
108
                .filters
50
108
                .iter()
51
165
                .map(|(_, f)| f.value.lint())
52
108
                .collect::<Vec<_>>()
53
108
                .join(" ");
54
108
            s.push_str(&filters);
55
        }
56
369
        s.push(' ');
57
369
        s.push_str(&self.predicate.lint());
58
369
        s.push_str(&lint_lt(&self.line_terminator0, true));
59
369
        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
294
    fn lint(&self) -> String {
166
294
        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
315
    fn lint(&self) -> String {
198
315
        let mut s = String::new();
199
315
        self.line_terminators
200
315
            .iter()
201
316
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
202
315
        s.push_str(&self.kind.lint());
203
315
        s.push_str(&lint_lt(&self.line_terminator0, true));
204
315
        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
132
    fn lint(&self) -> String {
249
132
        let mut s = String::new();
250
132
        s.push_str(self.identifier());
251
132
        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
66
            | FilterValue::UrlEncode => {}
328
        }
329
132
        s
330
    }
331
}
332

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
656
impl Lint for Regex {
657
12
    fn lint(&self) -> String {
658
12
        self.to_source().to_string()
659
    }
660
}
661

            
662
impl Lint for RegexValue {
663
9
    fn lint(&self) -> String {
664
9
        match self {
665
3
            RegexValue::Template(value) => value.lint(),
666
6
            RegexValue::Regex(value) => value.lint(),
667
        }
668
    }
669
}
670

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

            
682
307
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
683

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

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

            
721
119
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
722

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

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

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

            
785
impl Lint for StatusValue {
786
114
    fn lint(&self) -> String {
787
114
        self.to_source().to_string()
788
    }
789
}
790

            
791
impl Lint for Template {
792
1170
    fn lint(&self) -> String {
793
1170
        self.to_source().to_string()
794
    }
795
}
796

            
797
impl Lint for U64 {
798
27
    fn lint(&self) -> String {
799
27
        self.to_source().to_string()
800
    }
801
}
802

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

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

            
819
impl Lint for VersionValue {
820
114
    fn lint(&self) -> String {
821
114
        self.to_source().to_string()
822
    }
823
}
824

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

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

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

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

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

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

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

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

            
897
36
fn lint_duration_option(option: &DurationOption, default_unit: DurationUnit) -> String {
898
36
    match option {
899
24
        DurationOption::Literal(duration) => lint_duration(duration, default_unit),
900
12
        DurationOption::Placeholder(expr) => expr.lint(),
901
    }
902
}
903

            
904
24
fn lint_duration(duration: &Duration, default_unit: DurationUnit) -> String {
905
24
    let mut s = String::new();
906
24
    s.push_str(&duration.value.lint());
907
24
    let unit = duration.unit.unwrap_or(default_unit);
908
24
    s.push_str(&unit.to_string());
909
24
    s
910
}
911

            
912
#[cfg(test)]
913
mod tests {
914
    use crate::linter::lint_hurl_file;
915
    use hurl_core::parser;
916

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