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
375
    fn lint(&self) -> String {
41
375
        let mut s = String::new();
42
375
        self.line_terminators
43
375
            .iter()
44
375
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
45
375
        s.push_str(&self.query.lint());
46
375
        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
375
        s.push(' ');
57
375
        s.push_str(&self.predicate.lint());
58
375
        s.push_str(&lint_lt(&self.line_terminator0, true));
59
375
        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
300
    fn lint(&self) -> String {
166
300
        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
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
1755
fn lint_lt(lt: &LineTerminator, is_trailing: bool) -> String {
395
1755
    let mut s = String::new();
396
1755
    if let Some(comment) = &lt.comment {
397
300
        if is_trailing {
398
216
            // if line terminator is a trailing terminator, we keep the leading whitespaces
399
216
            // to keep user alignment.
400
216
            s.push_str(lt.space0.as_str());
401
        }
402
300
        s.push_str(&comment.lint());
403
1455
    };
404
    // We always terminate a file by a newline
405
1755
    if lt.newline.value.is_empty() {
406
3
        s.push('\n');
407
1752
    } else {
408
1752
        s.push_str(&lt.newline.value);
409
    }
410
1755
    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
315
    fn lint(&self) -> String {
453
315
        let mut s = String::new();
454
315
        s.push_str(self.identifier());
455
315
        s.push(':');
456
315
        s.push(' ');
457
315
        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
12
            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
315
        s.push_str(&value);
504
315
        s
505
    }
506
}
507

            
508
impl Lint for Query {
509
393
    fn lint(&self) -> String {
510
393
        let mut s = String::new();
511
393
        s.push_str(self.value.identifier());
512
393
        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
237
            QueryValue::Jsonpath { expr, .. } => {
530
237
                s.push(' ');
531
237
                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
393
        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
375
    fn lint(&self) -> String {
564
375
        let mut s = String::new();
565
375
        if self.not {
566
3
            s.push_str("not");
567
3
            s.push(' ');
568
        }
569
375
        s.push_str(&self.predicate_func.value.lint());
570
375
        s
571
    }
572
}
573

            
574
impl Lint for PredicateFuncValue {
575
375
    fn lint(&self) -> String {
576
375
        let mut s = String::new();
577
375
        s.push_str(self.identifier());
578
375
        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::IsInteger
624
            | PredicateFuncValue::IsFloat
625
            | PredicateFuncValue::IsBoolean
626
            | PredicateFuncValue::IsString
627
            | PredicateFuncValue::IsCollection
628
            | PredicateFuncValue::IsDate
629
            | PredicateFuncValue::IsIsoDate
630
            | PredicateFuncValue::Exist
631
            | PredicateFuncValue::IsEmpty
632
            | PredicateFuncValue::IsNumber
633
            | PredicateFuncValue::IsIpv4
634
            | PredicateFuncValue::IsIpv6
635
54
            | PredicateFuncValue::IsUuid => {}
636
        }
637
375
        s
638
    }
639
}
640

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

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

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

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

            
684
307
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
685

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

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

            
723
119
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
724

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

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

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

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

            
793
impl Lint for Template {
794
1182
    fn lint(&self) -> String {
795
1182
        self.to_source().to_string()
796
    }
797
}
798

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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