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
//! Walker traverses an AST in depth-first order. Each overridden visit method has full control over
19
//! what happens with its node, it can do its own traversal of the node's children, call `visit::walk_*`
20
//! to apply the default traversal algorithm, or prevent deeper traversal by doing nothing.
21
//!
22
//! Code heavily inspired from <https://github.com/rust-lang/rust/blob/master/compiler/rustc_ast/src/visit.rs>
23
use crate::ast::{
24
    Assert, Base64, Body, BooleanOption, Bytes, Capture, Comment, Cookie, CookiePath, CountOption,
25
    Duration, DurationOption, Entry, EntryOption, File, FilenameParam, FilenameValue, Filter,
26
    FilterValue, Hex, HurlFile, IntegerValue, JsonValue, KeyValue, LineTerminator, Method,
27
    MultilineString, MultipartParam, NaturalOption, Number, OptionKind, Placeholder, Predicate,
28
    PredicateFuncValue, PredicateValue, Query, QueryValue, Regex, RegexValue, Request, Response,
29
    Section, SectionValue, StatusValue, Template, U64, VariableDefinition, VariableValue,
30
    VerbosityOption, VersionValue, Whitespace,
31
};
32
use crate::types::{Count, DurationUnit, SourceString, ToSource};
33

            
34
/// Each method of the `Visitor` trait is a hook to be potentially overridden. Each method's default
35
/// implementation recursively visits the substructure of the input via the corresponding `walk` method;
36
/// e.g., the `visit_item` method by default calls `visit::walk_item`.
37
#[allow(unused_variables)]
38
pub trait Visitor: Sized {
39
3336
    fn visit_assert(&mut self, assert: &Assert) {
40
3336
        walk_assert(self, assert);
41
    }
42

            
43
62
    fn visit_base64(&mut self, value: &Base64) {
44
62
        walk_base64(self, value);
45
    }
46

            
47
    fn visit_base64_value(&mut self, value: &[u8], source: &SourceString) {}
48

            
49
628
    fn visit_body(&mut self, body: &Body) {
50
628
        walk_body(self, body);
51
    }
52

            
53
    fn visit_bool(&mut self, value: bool) {}
54

            
55
502
    fn visit_bool_option(&mut self, option: &BooleanOption) {
56
502
        walk_bool_option(self, option);
57
    }
58

            
59
248
    fn visit_capture(&mut self, capture: &Capture) {
60
248
        walk_capture(self, capture);
61
    }
62

            
63
54
    fn visit_cookie(&mut self, cookie: &Cookie) {
64
54
        walk_cookie(self, cookie);
65
    }
66

            
67
    fn visit_cookie_path(&mut self, path: &CookiePath) {}
68

            
69
    fn visit_comment(&mut self, comment: &Comment) {}
70

            
71
62
    fn visit_count(&mut self, count: Count) {
72
62
        walk_count(self, count);
73
    }
74

            
75
84
    fn visit_count_option(&mut self, option: &CountOption) {
76
84
        walk_count_option(self, option);
77
    }
78

            
79
68
    fn visit_duration(&mut self, duration: &Duration) {
80
68
        walk_duration(self, duration);
81
    }
82

            
83
92
    fn visit_duration_option(&mut self, option: &DurationOption) {
84
92
        walk_duration_option(self, option);
85
    }
86

            
87
    fn visit_duration_unit(&mut self, unit: DurationUnit) {}
88

            
89
182
    fn visit_entry(&mut self, entry: &Entry) {
90
182
        walk_entry(self, entry);
91
    }
92

            
93
1022
    fn visit_entry_option(&mut self, option: &EntryOption) {
94
1022
        walk_entry_option(self, option);
95
    }
96

            
97
78
    fn visit_file(&mut self, file: &File) {
98
78
        walk_file(self, file);
99
    }
100

            
101
40
    fn visit_filename_param(&mut self, param: &FilenameParam) {
102
40
        walk_filename_param(self, param);
103
    }
104

            
105
40
    fn visit_filename_value(&mut self, value: &FilenameValue) {
106
40
        walk_filename_value(self, value);
107
    }
108

            
109
    fn visit_filename(&mut self, filename: &Template) {}
110

            
111
1330
    fn visit_filter(&mut self, filter: &Filter) {
112
1330
        walk_filter(self, filter);
113
    }
114

            
115
    fn visit_filter_kind(&mut self, kind: &FilterValue) {}
116

            
117
    fn visit_header(&mut self, header: &KeyValue) {
118
        walk_header(self, header);
119
    }
120

            
121
300
    fn visit_hex(&mut self, hex: &Hex) {
122
300
        walk_hex(self, hex);
123
    }
124

            
125
    fn visit_hex_value(&mut self, value: &[u8], source: &SourceString) {}
126

            
127
48
    fn visit_hurl_file(&mut self, file: &HurlFile) {
128
48
        walk_hurl_file(self, file);
129
    }
130

            
131
134
    fn visit_integer_value(&mut self, n: &IntegerValue) {
132
134
        walk_integer_value(self, n);
133
    }
134

            
135
    fn visit_i64(&mut self, n: i64) {}
136

            
137
    fn visit_json_body(&mut self, json: &JsonValue) {}
138

            
139
836
    fn visit_kv(&mut self, kv: &KeyValue) {
140
836
        walk_kv(self, kv);
141
    }
142

            
143
12970
    fn visit_lt(&mut self, lt: &LineTerminator) {
144
12970
        walk_lt(self, lt);
145
    }
146

            
147
    fn visit_literal(&mut self, lit: &'static str) {}
148

            
149
    fn visit_method(&mut self, method: &Method) {}
150

            
151
    fn visit_multiline_string(&mut self, string: &MultilineString) {}
152

            
153
12
    fn visit_natural_option(&mut self, option: &NaturalOption) {
154
12
        walk_natural_option(self, option);
155
    }
156

            
157
    fn visit_not(&mut self, identifier: &'static str) {}
158

            
159
    fn visit_null(&mut self, identifier: &'static str) {}
160

            
161
    fn visit_number(&mut self, number: &Number) {}
162

            
163
    fn visit_placeholder(&mut self, placeholder: &Placeholder) {}
164

            
165
3336
    fn visit_predicate(&mut self, predicate: &Predicate) {
166
3336
        walk_predicate(self, predicate);
167
    }
168

            
169
    fn visit_predicate_kind(&mut self, kind: &PredicateFuncValue) {}
170

            
171
2776
    fn visit_predicate_value(&mut self, value: &PredicateValue) {
172
2776
        walk_predicate_value(self, value);
173
    }
174

            
175
3584
    fn visit_query(&mut self, query: &Query) {
176
3584
        walk_query(self, query);
177
    }
178

            
179
    fn visit_query_kind(&mut self, kind: &QueryValue) {}
180

            
181
182
    fn visit_request(&mut self, request: &Request) {
182
182
        walk_request(self, request);
183
    }
184

            
185
76
    fn visit_response(&mut self, response: &Response) {
186
76
        walk_response(self, response);
187
    }
188

            
189
    fn visit_regex(&mut self, regex: &Regex) {}
190

            
191
1196
    fn visit_section(&mut self, section: &Section) {
192
1196
        walk_section(self, section);
193
    }
194

            
195
    fn visit_status(&mut self, value: &StatusValue) {}
196

            
197
    fn visit_string(&mut self, value: &str) {}
198

            
199
    fn visit_section_header(&mut self, name: &str) {}
200

            
201
1196
    fn visit_section_value(&mut self, section_value: &SectionValue) {
202
1196
        walk_section_value(self, section_value);
203
    }
204

            
205
    fn visit_template(&mut self, template: &Template) {}
206

            
207
    fn visit_url(&mut self, url: &Template) {}
208

            
209
    fn visit_u64(&mut self, n: &U64) {}
210

            
211
    fn visit_usize(&mut self, n: usize) {}
212

            
213
114
    fn visit_variable_def(&mut self, def: &VariableDefinition) {
214
114
        walk_variable_def(self, def);
215
    }
216

            
217
    fn visit_variable_name(&mut self, name: &str) {}
218

            
219
114
    fn visit_variable_value(&mut self, value: &VariableValue) {
220
114
        walk_variable_value(self, value);
221
    }
222

            
223
12
    fn visit_verbosity_option(&mut self, value: &VerbosityOption) {
224
12
        walk_verbosity_option(self, value);
225
    }
226

            
227
    fn visit_version(&mut self, value: &VersionValue) {}
228

            
229
    fn visit_xml_body(&mut self, xml: &str) {}
230

            
231
    fn visit_whitespace(&mut self, ws: &Whitespace) {}
232
}
233

            
234
3336
pub fn walk_assert<V: Visitor>(visitor: &mut V, assert: &Assert) {
235
3336
    assert.line_terminators.iter().for_each(|lt| {
236
80
        visitor.visit_lt(lt);
237
80
    });
238
3336
    visitor.visit_whitespace(&assert.space0);
239
3336
    visitor.visit_query(&assert.query);
240
3336
    for (space, filter) in assert.filters.iter() {
241
1284
        visitor.visit_whitespace(space);
242
1284
        visitor.visit_filter(filter);
243
    }
244
3336
    visitor.visit_whitespace(&assert.space1);
245
3336
    visitor.visit_predicate(&assert.predicate);
246
3336
    visitor.visit_lt(&assert.line_terminator0);
247
}
248

            
249
62
pub fn walk_base64<V: Visitor>(visitor: &mut V, base64: &Base64) {
250
62
    visitor.visit_literal("base64,");
251
62
    visitor.visit_whitespace(&base64.space0);
252
62
    visitor.visit_base64_value(&base64.value, &base64.source);
253
62
    visitor.visit_whitespace(&base64.space1);
254
62
    visitor.visit_literal(";");
255
}
256

            
257
628
pub fn walk_body<V: Visitor>(visitor: &mut V, body: &Body) {
258
628
    body.line_terminators.iter().for_each(|lt| {
259
76
        visitor.visit_lt(lt);
260
76
    });
261
628
    visitor.visit_whitespace(&body.space0);
262
628
    match &body.value {
263
68
        Bytes::Json(value) => visitor.visit_json_body(value),
264
22
        Bytes::Xml(value) => visitor.visit_xml_body(value),
265
182
        Bytes::MultilineString(value) => visitor.visit_multiline_string(value),
266
210
        Bytes::OnelineString(value) => visitor.visit_template(value),
267
52
        Bytes::Base64(value) => visitor.visit_base64(value),
268
64
        Bytes::File(value) => visitor.visit_file(value),
269
30
        Bytes::Hex(value) => visitor.visit_hex(value),
270
    }
271
628
    visitor.visit_lt(&body.line_terminator0);
272
}
273

            
274
502
pub fn walk_bool_option<V: Visitor>(visitor: &mut V, option: &BooleanOption) {
275
502
    match option {
276
378
        BooleanOption::Literal(value) => visitor.visit_bool(*value),
277
124
        BooleanOption::Placeholder(value) => visitor.visit_placeholder(value),
278
    }
279
}
280

            
281
248
pub fn walk_capture<V: Visitor>(visitor: &mut V, capture: &Capture) {
282
248
    capture.line_terminators.iter().for_each(|lt| {
283
8
        visitor.visit_lt(lt);
284
8
    });
285
248
    visitor.visit_whitespace(&capture.space0);
286
248
    visitor.visit_template(&capture.name);
287
248
    visitor.visit_whitespace(&capture.space1);
288
248
    visitor.visit_literal(":");
289
248
    visitor.visit_whitespace(&capture.space2);
290
248
    visitor.visit_query(&capture.query);
291
248
    for (space, filter) in capture.filters.iter() {
292
46
        visitor.visit_whitespace(space);
293
46
        visitor.visit_filter(filter);
294
    }
295
248
    if capture.redacted {
296
28
        visitor.visit_whitespace(&capture.space3);
297
28
        // The next node should have been literal to be more correct
298
28
        // we visit a string instead to be comptaible with <= 6.1.1 HTML export
299
28
        // visitor.visit_literal("redact");
300
28
        visitor.visit_string("redact");
301
    }
302
248
    visitor.visit_lt(&capture.line_terminator0);
303
}
304

            
305
54
pub fn walk_cookie<V: Visitor>(visitor: &mut V, cookie: &Cookie) {
306
54
    cookie.line_terminators.iter().for_each(|lt| {
307
        visitor.visit_lt(lt);
308
    });
309
54
    visitor.visit_whitespace(&cookie.space0);
310
54
    visitor.visit_template(&cookie.name);
311
54
    visitor.visit_whitespace(&cookie.space1);
312
54
    visitor.visit_literal(":");
313
54
    visitor.visit_whitespace(&cookie.space2);
314
54
    visitor.visit_template(&cookie.value);
315
54
    visitor.visit_lt(&cookie.line_terminator0);
316
}
317

            
318
62
pub fn walk_count<V: Visitor>(visitor: &mut V, count: Count) {
319
62
    match count {
320
50
        Count::Finite(count) => visitor.visit_usize(count),
321
12
        Count::Infinite => visitor.visit_i64(-1),
322
    }
323
}
324

            
325
84
pub fn walk_count_option<V: Visitor>(visitor: &mut V, option: &CountOption) {
326
84
    match option {
327
62
        CountOption::Literal(value) => visitor.visit_count(*value),
328
22
        CountOption::Placeholder(value) => visitor.visit_placeholder(value),
329
    }
330
}
331

            
332
68
pub fn walk_duration<V: Visitor>(visitor: &mut V, duration: &Duration) {
333
68
    visitor.visit_u64(&duration.value);
334
68
    if let Some(unit) = duration.unit {
335
60
        visitor.visit_duration_unit(unit);
336
    }
337
}
338

            
339
92
pub fn walk_duration_option<V: Visitor>(visitor: &mut V, option: &DurationOption) {
340
92
    match option {
341
68
        DurationOption::Literal(value) => visitor.visit_duration(value),
342
24
        DurationOption::Placeholder(value) => visitor.visit_placeholder(value),
343
    }
344
}
345

            
346
1522
pub fn walk_entry<V: Visitor>(visitor: &mut V, entry: &Entry) {
347
1522
    visitor.visit_request(&entry.request);
348
1522
    if let Some(ref response) = entry.response {
349
1244
        visitor.visit_response(response);
350
    }
351
}
352

            
353
1022
pub fn walk_entry_option<V: Visitor>(visitor: &mut V, option: &EntryOption) {
354
1022
    option.line_terminators.iter().for_each(|lt| {
355
10
        visitor.visit_lt(lt);
356
10
    });
357
1022
    visitor.visit_whitespace(&option.space0);
358
1022
    visitor.visit_string(option.kind.identifier());
359
1022
    visitor.visit_whitespace(&option.space1);
360
1022
    visitor.visit_literal(":");
361
1022
    visitor.visit_whitespace(&option.space2);
362
1022
    match &option.kind {
363
12
        OptionKind::AwsSigV4(value) => visitor.visit_template(value),
364
12
        OptionKind::CaCertificate(filename) => visitor.visit_filename(filename),
365
18
        OptionKind::ClientCert(filename) => visitor.visit_filename(filename),
366
12
        OptionKind::ClientKey(filename) => visitor.visit_filename(filename),
367
88
        OptionKind::Compressed(value) => visitor.visit_bool_option(value),
368
12
        OptionKind::ConnectTo(value) => visitor.visit_template(value),
369
12
        OptionKind::ConnectTimeout(value) => visitor.visit_duration_option(value),
370
36
        OptionKind::Delay(value) => visitor.visit_duration_option(value),
371
12
        OptionKind::Digest(value) => visitor.visit_bool_option(value),
372
12
        OptionKind::FailWithBody(value) => visitor.visit_bool_option(value),
373
90
        OptionKind::FollowLocation(value) => visitor.visit_bool_option(value),
374
16
        OptionKind::FollowLocationTrusted(value) => visitor.visit_bool_option(value),
375
12
        OptionKind::Header(value) => visitor.visit_template(value),
376
36
        OptionKind::Http10(value) => visitor.visit_bool_option(value),
377
28
        OptionKind::Http11(value) => visitor.visit_bool_option(value),
378
12
        OptionKind::Http2(value) => visitor.visit_bool_option(value),
379
6
        OptionKind::Http2PriorKnowledge(value) => visitor.visit_bool_option(value),
380
12
        OptionKind::Http3(value) => visitor.visit_bool_option(value),
381
22
        OptionKind::Insecure(value) => visitor.visit_bool_option(value),
382
12
        OptionKind::IpV4(value) => visitor.visit_bool_option(value),
383
12
        OptionKind::IpV6(value) => visitor.visit_bool_option(value),
384
12
        OptionKind::LimitRate(value) => visitor.visit_natural_option(value),
385
20
        OptionKind::MaxRedirect(value) => visitor.visit_count_option(value),
386
12
        OptionKind::MaxTime(value) => visitor.visit_duration_option(value),
387
14
        OptionKind::Negotiate(value) => visitor.visit_bool_option(value),
388
12
        OptionKind::NetRc(value) => visitor.visit_bool_option(value),
389
12
        OptionKind::NetRcFile(filename) => visitor.visit_filename(filename),
390
12
        OptionKind::NetRcOptional(value) => visitor.visit_bool_option(value),
391
12
        OptionKind::NoHeader(value) => visitor.visit_template(value),
392
6
        OptionKind::NoJsonpathCoercion(value) => visitor.visit_bool_option(value),
393
14
        OptionKind::Ntlm(value) => visitor.visit_bool_option(value),
394
12
        OptionKind::Output(filename) => visitor.visit_filename(filename),
395
12
        OptionKind::PathAsIs(value) => visitor.visit_bool_option(value),
396
12
        OptionKind::PinnedPublicKey(value) => visitor.visit_template(value),
397
20
        OptionKind::Proxy(value) => visitor.visit_template(value),
398
26
        OptionKind::Repeat(value) => visitor.visit_count_option(value),
399
12
        OptionKind::Resolve(value) => visitor.visit_template(value),
400
38
        OptionKind::Retry(value) => visitor.visit_count_option(value),
401
32
        OptionKind::RetryInterval(value) => visitor.visit_duration_option(value),
402
12
        OptionKind::Skip(value) => visitor.visit_bool_option(value),
403
12
        OptionKind::TlsMax(value) => visitor.visit_template(value),
404
12
        OptionKind::UnixSocket(value) => visitor.visit_filename(value),
405
24
        OptionKind::User(value) => visitor.visit_template(value),
406
114
        OptionKind::Variable(value) => visitor.visit_variable_def(value),
407
46
        OptionKind::Verbose(value) => visitor.visit_bool_option(value),
408
12
        OptionKind::Verbosity(value) => visitor.visit_verbosity_option(value),
409
16
        OptionKind::VeryVerbose(value) => visitor.visit_bool_option(value),
410
    };
411
1022
    visitor.visit_lt(&option.line_terminator0);
412
}
413

            
414
78
pub fn walk_file<V: Visitor>(visitor: &mut V, file: &File) {
415
78
    visitor.visit_literal("file,");
416
78
    visitor.visit_whitespace(&file.space0);
417
78
    visitor.visit_filename(&file.filename);
418
78
    visitor.visit_whitespace(&file.space1);
419
78
    visitor.visit_literal(";");
420
}
421

            
422
1330
pub fn walk_filter<V: Visitor>(visitor: &mut V, filter: &Filter) {
423
1330
    visitor.visit_filter_kind(&filter.value);
424
1330
    match &filter.value {
425
40
        FilterValue::Base64Decode => {}
426
18
        FilterValue::Base64Encode => {}
427
30
        FilterValue::Base64UrlSafeDecode => {}
428
18
        FilterValue::Base64UrlSafeEncode => {}
429
40
        FilterValue::CharsetDecode { space0, encoding } => {
430
40
            visitor.visit_whitespace(space0);
431
40
            visitor.visit_template(encoding);
432
        }
433
314
        FilterValue::Count => {}
434
10
        FilterValue::DaysAfterNow => {}
435
20
        FilterValue::DaysBeforeNow => {}
436
6
        FilterValue::Decode { space0, encoding } => {
437
6
            visitor.visit_whitespace(space0);
438
6
            visitor.visit_template(encoding);
439
        }
440
22
        FilterValue::First => {}
441
20
        FilterValue::Format { space0, fmt } => {
442
20
            visitor.visit_whitespace(space0);
443
20
            visitor.visit_template(fmt);
444
        }
445
50
        FilterValue::DateFormat { space0, fmt } => {
446
50
            visitor.visit_whitespace(space0);
447
50
            visitor.visit_template(fmt);
448
        }
449
18
        FilterValue::HtmlEscape => {}
450
26
        FilterValue::HtmlUnescape => {}
451
34
        FilterValue::JsonPath { space0, expr } => {
452
34
            visitor.visit_whitespace(space0);
453
34
            visitor.visit_template(expr);
454
        }
455
22
        FilterValue::Last => {}
456
68
        FilterValue::Location => {}
457
134
        FilterValue::Nth { space0, n } => {
458
134
            visitor.visit_whitespace(space0);
459
134
            visitor.visit_integer_value(n);
460
        }
461
38
        FilterValue::Regex { space0, value } => {
462
38
            visitor.visit_whitespace(space0);
463
38
            match value {
464
8
                RegexValue::Template(value) => visitor.visit_template(value),
465
30
                RegexValue::Regex(regex) => visitor.visit_regex(regex),
466
            }
467
        }
468
        FilterValue::Replace {
469
42
            space0,
470
42
            old_value,
471
42
            space1,
472
42
            new_value,
473
42
        } => {
474
42
            visitor.visit_whitespace(space0);
475
42
            visitor.visit_template(old_value);
476
42
            visitor.visit_whitespace(space1);
477
42
            visitor.visit_template(new_value);
478
        }
479
        FilterValue::ReplaceRegex {
480
26
            space0,
481
26
            pattern,
482
26
            space1,
483
26
            new_value,
484
        } => {
485
26
            visitor.visit_whitespace(space0);
486
26
            match pattern {
487
8
                RegexValue::Template(value) => visitor.visit_template(value),
488
18
                RegexValue::Regex(regex) => visitor.visit_regex(regex),
489
            }
490
26
            visitor.visit_whitespace(space1);
491
26
            visitor.visit_template(new_value);
492
        }
493
22
        FilterValue::Split { space0, sep } => {
494
22
            visitor.visit_whitespace(space0);
495
22
            visitor.visit_template(sep);
496
        }
497
86
        FilterValue::ToDate { space0, fmt } => {
498
86
            visitor.visit_whitespace(space0);
499
86
            visitor.visit_template(fmt);
500
        }
501
38
        FilterValue::ToFloat => {}
502
32
        FilterValue::ToHex => {}
503
38
        FilterValue::ToInt => {}
504
14
        FilterValue::ToString => {}
505
18
        FilterValue::UrlDecode => {}
506
18
        FilterValue::UrlEncode => {}
507
26
        FilterValue::UrlQueryParam { space0, param } => {
508
26
            visitor.visit_whitespace(space0);
509
26
            visitor.visit_template(param);
510
        }
511
10
        FilterValue::Utf8Decode => {}
512
10
        FilterValue::Utf8Encode => {}
513
22
        FilterValue::XPath { space0, expr } => {
514
22
            visitor.visit_whitespace(space0);
515
22
            visitor.visit_template(expr);
516
        }
517
    }
518
}
519

            
520
40
pub fn walk_filename_param<V: Visitor>(visitor: &mut V, param: &FilenameParam) {
521
40
    param.line_terminators.iter().for_each(|lt| {
522
        visitor.visit_lt(lt);
523
    });
524
40
    visitor.visit_whitespace(&param.space0);
525
40
    visitor.visit_template(&param.key);
526
40
    visitor.visit_whitespace(&param.space1);
527
40
    visitor.visit_literal(":");
528
40
    visitor.visit_whitespace(&param.space2);
529
40
    visitor.visit_filename_value(&param.value);
530
40
    visitor.visit_lt(&param.line_terminator0);
531
}
532

            
533
40
pub fn walk_filename_value<V: Visitor>(visitor: &mut V, value: &FilenameValue) {
534
40
    visitor.visit_literal("file,");
535
40
    visitor.visit_whitespace(&value.space0);
536
40
    visitor.visit_filename(&value.filename);
537
40
    visitor.visit_whitespace(&value.space1);
538
40
    visitor.visit_literal(";");
539
40
    visitor.visit_whitespace(&value.space2);
540
40
    if let Some(content_type) = &value.content_type {
541
14
        visitor.visit_template(content_type);
542
    }
543
}
544

            
545
pub fn walk_header<V: Visitor>(visitor: &mut V, header: &KeyValue) {
546
    visitor.visit_kv(header);
547
}
548

            
549
300
pub fn walk_hex<V: Visitor>(visitor: &mut V, hex: &Hex) {
550
300
    visitor.visit_literal("hex,");
551
300
    visitor.visit_whitespace(&hex.space0);
552
300
    visitor.visit_hex_value(&hex.value, &hex.source);
553
300
    visitor.visit_whitespace(&hex.space1);
554
300
    visitor.visit_literal(";");
555
}
556

            
557
528
pub fn walk_hurl_file<V: Visitor>(visitor: &mut V, file: &HurlFile) {
558
1522
    file.entries.iter().for_each(|e| visitor.visit_entry(e));
559
528
    file.line_terminators.iter().for_each(|lt| {
560
236
        visitor.visit_lt(lt);
561
236
    });
562
}
563

            
564
134
pub fn walk_integer_value<V: Visitor>(visitor: &mut V, n: &IntegerValue) {
565
134
    match n {
566
126
        IntegerValue::Literal(value) => visitor.visit_i64(value.as_i64()),
567
8
        IntegerValue::Placeholder(value) => visitor.visit_placeholder(value),
568
    }
569
}
570

            
571
836
pub fn walk_kv<V: Visitor>(visitor: &mut V, kv: &KeyValue) {
572
836
    kv.line_terminators.iter().for_each(|lt| {
573
22
        visitor.visit_lt(lt);
574
22
    });
575
836
    visitor.visit_whitespace(&kv.space0);
576
836
    visitor.visit_template(&kv.key);
577
836
    visitor.visit_whitespace(&kv.space1);
578
836
    visitor.visit_literal(":");
579
836
    visitor.visit_whitespace(&kv.space2);
580
836
    visitor.visit_template(&kv.value);
581
836
    visitor.visit_lt(&kv.line_terminator0);
582
}
583

            
584
12970
pub fn walk_lt<V: Visitor>(visitor: &mut V, lt: &LineTerminator) {
585
12970
    visitor.visit_whitespace(&lt.space0);
586
12970
    if let Some(ref comment) = lt.comment {
587
1676
        visitor.visit_comment(comment);
588
    }
589
12970
    visitor.visit_whitespace(&lt.newline);
590
}
591

            
592
12
pub fn walk_natural_option<V: Visitor>(visitor: &mut V, option: &NaturalOption) {
593
12
    match option {
594
6
        NaturalOption::Literal(value) => visitor.visit_u64(value),
595
6
        NaturalOption::Placeholder(value) => visitor.visit_placeholder(value),
596
    }
597
}
598

            
599
3584
pub fn walk_query<V: Visitor>(visitor: &mut V, query: &Query) {
600
3584
    visitor.visit_query_kind(&query.value);
601

            
602
3584
    match &query.value {
603
288
        QueryValue::Header { space0, name } => {
604
288
            visitor.visit_whitespace(space0);
605
288
            visitor.visit_template(name);
606
        }
607
70
        QueryValue::Cookie { space0, expr } => {
608
70
            visitor.visit_whitespace(space0);
609
70
            visitor.visit_cookie_path(expr);
610
        }
611
118
        QueryValue::Xpath { space0, expr } => {
612
118
            visitor.visit_whitespace(space0);
613
118
            visitor.visit_template(expr);
614
        }
615
1918
        QueryValue::Jsonpath { space0, expr } => {
616
1918
            visitor.visit_whitespace(space0);
617
1918
            visitor.visit_template(expr);
618
        }
619
34
        QueryValue::Regex { space0, value } => {
620
34
            visitor.visit_whitespace(space0);
621
34
            match value {
622
18
                RegexValue::Template(t) => visitor.visit_template(t),
623
16
                RegexValue::Regex(r) => visitor.visit_regex(r),
624
            }
625
        }
626
198
        QueryValue::Variable { space0, name } => {
627
198
            visitor.visit_whitespace(space0);
628
198
            visitor.visit_template(name);
629
        }
630
        QueryValue::Certificate {
631
66
            space0,
632
66
            attribute_name,
633
66
        } => {
634
66
            visitor.visit_whitespace(space0);
635
66
            visitor.visit_string(attribute_name.to_source().as_str());
636
        }
637
        QueryValue::Body
638
        | QueryValue::Status
639
        | QueryValue::Url
640
        | QueryValue::Duration
641
        | QueryValue::Bytes
642
        | QueryValue::RawBytes
643
        | QueryValue::Sha256
644
        | QueryValue::Md5
645
        | QueryValue::Version
646
        | QueryValue::Ip
647
892
        | QueryValue::Redirects => {}
648
    }
649
}
650

            
651
3336
pub fn walk_predicate<V: Visitor>(visitor: &mut V, pred: &Predicate) {
652
3336
    if pred.not {
653
234
        visitor.visit_not("not");
654
234
        visitor.visit_whitespace(&pred.space0);
655
    }
656
3336
    let kind = &pred.predicate_func.value;
657
3336
    visitor.visit_predicate_kind(kind);
658
3336
    match kind {
659
2152
        PredicateFuncValue::Equal { space0, value } => {
660
2152
            visitor.visit_whitespace(space0);
661
2152
            visitor.visit_predicate_value(value);
662
        }
663
66
        PredicateFuncValue::NotEqual { space0, value } => {
664
66
            visitor.visit_whitespace(space0);
665
66
            visitor.visit_predicate_value(value);
666
        }
667
66
        PredicateFuncValue::GreaterThan { space0, value } => {
668
66
            visitor.visit_whitespace(space0);
669
66
            visitor.visit_predicate_value(value);
670
        }
671
18
        PredicateFuncValue::GreaterThanOrEqual { space0, value } => {
672
18
            visitor.visit_whitespace(space0);
673
18
            visitor.visit_predicate_value(value);
674
        }
675
52
        PredicateFuncValue::LessThan { space0, value } => {
676
52
            visitor.visit_whitespace(space0);
677
52
            visitor.visit_predicate_value(value);
678
        }
679
26
        PredicateFuncValue::LessThanOrEqual { space0, value } => {
680
26
            visitor.visit_whitespace(space0);
681
26
            visitor.visit_predicate_value(value);
682
        }
683
142
        PredicateFuncValue::StartWith { space0, value } => {
684
142
            visitor.visit_whitespace(space0);
685
142
            visitor.visit_predicate_value(value);
686
        }
687
36
        PredicateFuncValue::EndWith { space0, value } => {
688
36
            visitor.visit_whitespace(space0);
689
36
            visitor.visit_predicate_value(value);
690
        }
691
114
        PredicateFuncValue::Contain { space0, value } => {
692
114
            visitor.visit_whitespace(space0);
693
114
            visitor.visit_predicate_value(value);
694
        }
695
6
        PredicateFuncValue::Include { space0, value } => {
696
6
            visitor.visit_whitespace(space0);
697
6
            visitor.visit_predicate_value(value);
698
        }
699
98
        PredicateFuncValue::Match { space0, value } => {
700
98
            visitor.visit_whitespace(space0);
701
98
            visitor.visit_predicate_value(value);
702
        }
703
        PredicateFuncValue::Exist
704
        | PredicateFuncValue::IsBoolean
705
        | PredicateFuncValue::IsCollection
706
        | PredicateFuncValue::IsDate
707
        | PredicateFuncValue::IsEmpty
708
        | PredicateFuncValue::IsFloat
709
        | PredicateFuncValue::IsInteger
710
        | PredicateFuncValue::IsIpv4
711
        | PredicateFuncValue::IsIpv6
712
        | PredicateFuncValue::IsIsoDate
713
        | PredicateFuncValue::IsList
714
        | PredicateFuncValue::IsNumber
715
        | PredicateFuncValue::IsObject
716
        | PredicateFuncValue::IsString
717
560
        | PredicateFuncValue::IsUuid => {}
718
    }
719
}
720

            
721
2776
pub fn walk_predicate_value<V: Visitor>(visitor: &mut V, pred_value: &PredicateValue) {
722
2776
    match pred_value {
723
10
        PredicateValue::Base64(value) => visitor.visit_base64(value),
724
50
        PredicateValue::Bool(value) => visitor.visit_bool(*value),
725
14
        PredicateValue::File(value) => visitor.visit_file(value),
726
270
        PredicateValue::Hex(value) => visitor.visit_hex(value),
727
52
        PredicateValue::MultilineString(value) => visitor.visit_multiline_string(value),
728
22
        PredicateValue::Null => visitor.visit_null("null"),
729
884
        PredicateValue::Number(value) => visitor.visit_number(value),
730
106
        PredicateValue::Placeholder(placeholder) => visitor.visit_placeholder(placeholder),
731
68
        PredicateValue::Regex(value) => visitor.visit_regex(value),
732
1300
        PredicateValue::String(value) => visitor.visit_template(value),
733
    }
734
}
735

            
736
1522
pub fn walk_request<V: Visitor>(visitor: &mut V, request: &Request) {
737
2238
    request.line_terminators.iter().for_each(|lt| {
738
2238
        visitor.visit_lt(lt);
739
2238
    });
740
1522
    visitor.visit_whitespace(&request.space0);
741
1522
    visitor.visit_method(&request.method);
742
1522
    visitor.visit_whitespace(&request.space1);
743
1522
    visitor.visit_url(&request.url);
744
1522
    visitor.visit_lt(&request.line_terminator0);
745
1522
    request.headers.iter().for_each(|h| visitor.visit_kv(h));
746
1522
    request
747
1522
        .sections
748
1522
        .iter()
749
1522
        .for_each(|s| visitor.visit_section(s));
750
1522
    if let Some(body) = &request.body {
751
236
        visitor.visit_body(body);
752
    }
753
}
754

            
755
1244
pub fn walk_response<V: Visitor>(visitor: &mut V, response: &Response) {
756
1244
    response.line_terminators.iter().for_each(|lt| {
757
26
        visitor.visit_lt(lt);
758
26
    });
759
1244
    visitor.visit_whitespace(&response.space0);
760
1244
    visitor.visit_version(&response.version.value);
761
1244
    visitor.visit_whitespace(&response.space1);
762
1244
    visitor.visit_status(&response.status.value);
763
1244
    visitor.visit_lt(&response.line_terminator0);
764
1244
    response.headers.iter().for_each(|h| visitor.visit_kv(h));
765
1244
    response
766
1244
        .sections
767
1244
        .iter()
768
1244
        .for_each(|s| visitor.visit_section(s));
769
1244
    if let Some(body) = &response.body {
770
392
        visitor.visit_body(body);
771
    }
772
}
773

            
774
1196
pub fn walk_section<V: Visitor>(visitor: &mut V, section: &Section) {
775
1196
    section.line_terminators.iter().for_each(|lt| {
776
148
        visitor.visit_lt(lt);
777
148
    });
778
1196
    visitor.visit_whitespace(&section.space0);
779
1196
    let name = format!("[{}]", section.identifier());
780
1196
    visitor.visit_section_header(&name);
781
1196
    visitor.visit_lt(&section.line_terminator0);
782
1196
    visitor.visit_section_value(&section.value);
783
}
784

            
785
1196
pub fn walk_section_value<V: Visitor>(visitor: &mut V, section_value: &SectionValue) {
786
14
    match section_value {
787
3336
        SectionValue::Asserts(asserts) => asserts.iter().for_each(|a| visitor.visit_assert(a)),
788
14
        SectionValue::BasicAuth(Some(auth)) => visitor.visit_kv(auth),
789
        SectionValue::BasicAuth(_) => {}
790
248
        SectionValue::Captures(captures) => captures.iter().for_each(|c| visitor.visit_capture(c)),
791
54
        SectionValue::Cookies(cookies) => cookies.iter().for_each(|c| visitor.visit_cookie(c)),
792
80
        SectionValue::FormParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
793
58
        SectionValue::MultipartFormData(params, _) => params.iter().for_each(|p| match p {
794
18
            MultipartParam::Param(param) => visitor.visit_kv(param),
795
40
            MultipartParam::FilenameParam(param) => visitor.visit_filename_param(param),
796
58
        }),
797
312
        SectionValue::Options(options) => {
798
1022
            options.iter().for_each(|o| visitor.visit_entry_option(o));
799
        }
800
160
        SectionValue::QueryParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
801
    }
802
}
803

            
804
114
pub fn walk_variable_def<V: Visitor>(visitor: &mut V, def: &VariableDefinition) {
805
114
    visitor.visit_variable_name(&def.name);
806
114
    visitor.visit_whitespace(&def.space0);
807
114
    visitor.visit_literal("=");
808
114
    visitor.visit_whitespace(&def.space1);
809
114
    visitor.visit_variable_value(&def.value);
810
}
811

            
812
114
pub fn walk_variable_value<V: Visitor>(visitor: &mut V, value: &VariableValue) {
813
114
    match value {
814
6
        VariableValue::Null => visitor.visit_null("null"),
815
6
        VariableValue::Bool(value) => visitor.visit_bool(*value),
816
44
        VariableValue::Number(value) => visitor.visit_number(value),
817
58
        VariableValue::String(value) => visitor.visit_template(value),
818
    }
819
}
820

            
821
12
pub fn walk_verbosity_option<V: Visitor>(visitor: &mut V, value: &VerbosityOption) {
822
12
    visitor.visit_string(value.identifier());
823
}
824

            
825
#[cfg(test)]
826
mod tests {
827
    use crate::ast::Assert;
828
    use crate::ast::visit::Visitor;
829
    use crate::parser;
830

            
831
    #[test]
832
    fn test_walk_assert() {
833
        struct AssertWalker {
834
            count: usize,
835
        }
836

            
837
        impl Visitor for AssertWalker {
838
            fn visit_assert(&mut self, _assert: &Assert) {
839
                self.count += 1;
840
            }
841
        }
842

            
843
        let mut walker = AssertWalker { count: 0 };
844
        let content = r#"
845
GET https://foo.com
846
HTTP 200
847
[Asserts]
848
jsonpath "$.toto[0]" == "tata"
849
jsonpath "$.toto[1]" == "toto"
850
jsonpath "$.toto[2]" == "titi"
851
jsonpath "$.toto[3]" == "tata"
852
jsonpath "$.toto[4]" == "tutu"
853

            
854
GET https://foo.com
855
HTTP 200
856
[Asserts]
857
status == 200
858
header "Location" not exists
859
"#;
860
        let file = parser::parse_hurl_file(content).unwrap();
861
        walker.visit_hurl_file(&file);
862
        assert_eq!(walker.count, 7);
863
    }
864
}