1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
use hurl_core::ast::{
19
    Assert, Base64, Body, BooleanOption, Bytes, Capture, CertificateAttributeName, Comment, Cookie,
20
    CookiePath, CountOption, Duration, DurationOption, Entry, EntryOption, File, FilenameParam,
21
    FilenameValue, FilterValue, Hex, HurlFile, I64, IntegerValue, JsonValue, KeyValue,
22
    LineTerminator, Method, MultilineString, MultipartParam, NaturalOption, Number, OptionKind,
23
    Placeholder, Predicate, PredicateFuncValue, PredicateValue, Query, QueryValue, Regex,
24
    RegexValue, Request, Response, Section, SectionValue, StatusValue, Template, U64,
25
    VariableDefinition, VariableValue, VerbosityOption, VersionValue,
26
};
27
use hurl_core::types::{Count, DurationUnit, ToSource};
28

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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