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
141
    fn lint(&self) -> String {
87
141
        match self {
88
81
            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
345
    fn lint(&self) -> String {
198
345
        let mut s = String::new();
199
345
        self.line_terminators
200
345
            .iter()
201
346
            .for_each(|lt| s.push_str(&lint_lt(lt, false)));
202
345
        s.push_str(&self.kind.lint());
203
345
        s.push_str(&lint_lt(&self.line_terminator0, true));
204
345
        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
1815
fn lint_lt(lt: &LineTerminator, is_trailing: bool) -> String {
399
1815
    let mut s = String::new();
400
1815
    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
1497
    };
408
    // We always terminate a file by a newline
409
1815
    if lt.newline.value.is_empty() {
410
3
        s.push('\n');
411
1812
    } else {
412
1812
        s.push_str(&lt.newline.value);
413
    }
414
1815
    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
345
    fn lint(&self) -> String {
457
345
        let mut s = String::new();
458
345
        s.push_str(self.identifier());
459
345
        s.push(':');
460
345
        s.push(' ');
461
345
        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
6
            OptionKind::Http3(value) => value.lint(),
479
9
            OptionKind::Insecure(value) => value.lint(),
480
6
            OptionKind::IpV4(value) => value.lint(),
481
6
            OptionKind::IpV6(value) => value.lint(),
482
9
            OptionKind::FollowLocation(value) => value.lint(),
483
6
            OptionKind::FollowLocationTrusted(value) => value.lint(),
484
6
            OptionKind::LimitRate(value) => value.lint(),
485
6
            OptionKind::MaxRedirect(value) => value.lint(),
486
6
            OptionKind::MaxTime(value) => lint_duration_option(value, DurationUnit::MilliSecond),
487
9
            OptionKind::Negotiate(value) => value.lint(),
488
6
            OptionKind::NetRc(value) => value.lint(),
489
6
            OptionKind::NetRcFile(value) => value.lint(),
490
6
            OptionKind::NetRcOptional(value) => value.lint(),
491
6
            OptionKind::NoHeader(value) => value.lint(),
492
9
            OptionKind::Ntlm(value) => value.lint(),
493
6
            OptionKind::Output(value) => value.lint(),
494
6
            OptionKind::PathAsIs(value) => value.lint(),
495
6
            OptionKind::PinnedPublicKey(value) => value.lint(),
496
6
            OptionKind::Proxy(value) => value.lint(),
497
9
            OptionKind::Repeat(value) => value.lint(),
498
6
            OptionKind::Resolve(value) => value.lint(),
499
15
            OptionKind::Retry(value) => value.lint(),
500
12
            OptionKind::RetryInterval(value) => {
501
12
                lint_duration_option(value, DurationUnit::MilliSecond)
502
            }
503
6
            OptionKind::Skip(value) => value.lint(),
504
6
            OptionKind::UnixSocket(value) => value.lint(),
505
12
            OptionKind::User(value) => value.lint(),
506
27
            OptionKind::Variable(value) => value.lint(),
507
15
            OptionKind::Verbose(value) => value.lint(),
508
6
            OptionKind::Verbosity(value) => value.lint(),
509
6
            OptionKind::VeryVerbose(value) => value.lint(),
510
        };
511
345
        s.push_str(&value);
512
345
        s
513
    }
514
}
515

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

            
565
impl Lint for Placeholder {
566
87
    fn lint(&self) -> String {
567
87
        self.to_source().to_string()
568
    }
569
}
570

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

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

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

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

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

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

            
695
312
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
696

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

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

            
734
119
        self.headers.iter().for_each(|h| s.push_str(&h.lint()));
735

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

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

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

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

            
804
impl Lint for Template {
805
1215
    fn lint(&self) -> String {
806
1215
        self.to_source().to_string()
807
    }
808
}
809

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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