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
4214
    fn visit_assert(&mut self, assert: &Assert) {
40
4214
        walk_assert(self, assert);
41
    }
42

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

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

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

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

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

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

            
63
69
    fn visit_cookie(&mut self, cookie: &Cookie) {
64
69
        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
81
    fn visit_count(&mut self, count: Count) {
72
81
        walk_count(self, count);
73
    }
74

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

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

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

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

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

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

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

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

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

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

            
111
1687
    fn visit_filter(&mut self, filter: &Filter) {
112
1687
        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
381
    fn visit_hex(&mut self, hex: &Hex) {
122
381
        walk_hex(self, hex);
123
    }
124

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

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

            
131
168
    fn visit_integer_value(&mut self, n: &IntegerValue) {
132
168
        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
1062
    fn visit_kv(&mut self, kv: &KeyValue) {
140
1062
        walk_kv(self, kv);
141
    }
142

            
143
16411
    fn visit_lt(&mut self, lt: &LineTerminator) {
144
16411
        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
16
    fn visit_natural_option(&mut self, option: &NaturalOption) {
154
16
        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
4214
    fn visit_predicate(&mut self, predicate: &Predicate) {
166
4214
        walk_predicate(self, predicate);
167
    }
168

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

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

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

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

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

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

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

            
191
1513
    fn visit_section(&mut self, section: &Section) {
192
1513
        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
1513
    fn visit_section_value(&mut self, section_value: &SectionValue) {
202
1513
        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
147
    fn visit_variable_def(&mut self, def: &VariableDefinition) {
214
147
        walk_variable_def(self, def);
215
    }
216

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

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

            
223
16
    fn visit_verbosity_option(&mut self, value: &VerbosityOption) {
224
16
        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
4214
pub fn walk_assert<V: Visitor>(visitor: &mut V, assert: &Assert) {
235
4214
    assert.line_terminators.iter().for_each(|lt| {
236
85
        visitor.visit_lt(lt);
237
85
    });
238
4214
    visitor.visit_whitespace(&assert.space0);
239
4214
    visitor.visit_query(&assert.query);
240
4214
    for (space, filter) in assert.filters.iter() {
241
1629
        visitor.visit_whitespace(space);
242
1629
        visitor.visit_filter(filter);
243
    }
244
4214
    visitor.visit_whitespace(&assert.space1);
245
4214
    visitor.visit_predicate(&assert.predicate);
246
4214
    visitor.visit_lt(&assert.line_terminator0);
247
}
248

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

            
257
803
pub fn walk_body<V: Visitor>(visitor: &mut V, body: &Body) {
258
803
    body.line_terminators.iter().for_each(|lt| {
259
98
        visitor.visit_lt(lt);
260
98
    });
261
803
    visitor.visit_whitespace(&body.space0);
262
803
    match &body.value {
263
86
        Bytes::Json(value) => visitor.visit_json_body(value),
264
28
        Bytes::Xml(value) => visitor.visit_xml_body(value),
265
236
        Bytes::MultilineString(value) => visitor.visit_multiline_string(value),
266
265
        Bytes::OnelineString(value) => visitor.visit_template(value),
267
67
        Bytes::Base64(value) => visitor.visit_base64(value),
268
82
        Bytes::File(value) => visitor.visit_file(value),
269
39
        Bytes::Hex(value) => visitor.visit_hex(value),
270
    }
271
803
    visitor.visit_lt(&body.line_terminator0);
272
}
273

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
555
667
pub fn walk_hurl_file<V: Visitor>(visitor: &mut V, file: &HurlFile) {
556
1938
    file.entries.iter().for_each(|e| visitor.visit_entry(e));
557
667
    file.line_terminators.iter().for_each(|lt| {
558
309
        visitor.visit_lt(lt);
559
309
    });
560
}
561

            
562
168
pub fn walk_integer_value<V: Visitor>(visitor: &mut V, n: &IntegerValue) {
563
168
    match n {
564
158
        IntegerValue::Literal(value) => visitor.visit_i64(value.as_i64()),
565
10
        IntegerValue::Placeholder(value) => visitor.visit_placeholder(value),
566
    }
567
}
568

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

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

            
590
16
pub fn walk_natural_option<V: Visitor>(visitor: &mut V, option: &NaturalOption) {
591
16
    match option {
592
8
        NaturalOption::Literal(value) => visitor.visit_u64(value),
593
8
        NaturalOption::Placeholder(value) => visitor.visit_placeholder(value),
594
    }
595
}
596

            
597
4522
pub fn walk_query<V: Visitor>(visitor: &mut V, query: &Query) {
598
4522
    visitor.visit_query_kind(&query.value);
599

            
600
4522
    match &query.value {
601
362
        QueryValue::Header { space0, name } => {
602
362
            visitor.visit_whitespace(space0);
603
362
            visitor.visit_template(name);
604
        }
605
89
        QueryValue::Cookie { space0, expr } => {
606
89
            visitor.visit_whitespace(space0);
607
89
            visitor.visit_cookie_path(expr);
608
        }
609
148
        QueryValue::Xpath { space0, expr } => {
610
148
            visitor.visit_whitespace(space0);
611
148
            visitor.visit_template(expr);
612
        }
613
2418
        QueryValue::Jsonpath { space0, expr } => {
614
2418
            visitor.visit_whitespace(space0);
615
2418
            visitor.visit_template(expr);
616
        }
617
43
        QueryValue::Regex { space0, value } => {
618
43
            visitor.visit_whitespace(space0);
619
43
            match value {
620
23
                RegexValue::Template(t) => visitor.visit_template(t),
621
20
                RegexValue::Regex(r) => visitor.visit_regex(r),
622
            }
623
        }
624
249
        QueryValue::Variable { space0, name } => {
625
249
            visitor.visit_whitespace(space0);
626
249
            visitor.visit_template(name);
627
        }
628
        QueryValue::Certificate {
629
88
            space0,
630
88
            attribute_name,
631
88
        } => {
632
88
            visitor.visit_whitespace(space0);
633
88
            visitor.visit_string(attribute_name.to_source().as_str());
634
        }
635
        QueryValue::Body
636
        | QueryValue::Status
637
        | QueryValue::Url
638
        | QueryValue::Duration
639
        | QueryValue::Bytes
640
        | QueryValue::RawBytes
641
        | QueryValue::Sha256
642
        | QueryValue::Md5
643
        | QueryValue::Version
644
        | QueryValue::Ip
645
1125
        | QueryValue::Redirects => {}
646
    }
647
}
648

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

            
719
3504
pub fn walk_predicate_value<V: Visitor>(visitor: &mut V, pred_value: &PredicateValue) {
720
3504
    match pred_value {
721
13
        PredicateValue::Base64(value) => visitor.visit_base64(value),
722
63
        PredicateValue::Bool(value) => visitor.visit_bool(*value),
723
18
        PredicateValue::File(value) => visitor.visit_file(value),
724
342
        PredicateValue::Hex(value) => visitor.visit_hex(value),
725
68
        PredicateValue::MultilineString(value) => visitor.visit_multiline_string(value),
726
28
        PredicateValue::Null => visitor.visit_null("null"),
727
1103
        PredicateValue::Number(value) => visitor.visit_number(value),
728
133
        PredicateValue::Placeholder(placeholder) => visitor.visit_placeholder(placeholder),
729
86
        PredicateValue::Regex(value) => visitor.visit_regex(value),
730
1650
        PredicateValue::String(value) => visitor.visit_template(value),
731
    }
732
}
733

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

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

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

            
783
1513
pub fn walk_section_value<V: Visitor>(visitor: &mut V, section_value: &SectionValue) {
784
18
    match section_value {
785
4214
        SectionValue::Asserts(asserts) => asserts.iter().for_each(|a| visitor.visit_assert(a)),
786
18
        SectionValue::BasicAuth(Some(auth)) => visitor.visit_kv(auth),
787
        SectionValue::BasicAuth(_) => {}
788
308
        SectionValue::Captures(captures) => captures.iter().for_each(|c| visitor.visit_capture(c)),
789
69
        SectionValue::Cookies(cookies) => cookies.iter().for_each(|c| visitor.visit_cookie(c)),
790
102
        SectionValue::FormParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
791
74
        SectionValue::MultipartFormData(params, _) => params.iter().for_each(|p| match p {
792
23
            MultipartParam::Param(param) => visitor.visit_kv(param),
793
51
            MultipartParam::FilenameParam(param) => visitor.visit_filename_param(param),
794
74
        }),
795
397
        SectionValue::Options(options) => {
796
1313
            options.iter().for_each(|o| visitor.visit_entry_option(o));
797
        }
798
203
        SectionValue::QueryParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
799
    }
800
}
801

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

            
810
147
pub fn walk_variable_value<V: Visitor>(visitor: &mut V, value: &VariableValue) {
811
147
    match value {
812
8
        VariableValue::Null => visitor.visit_null("null"),
813
8
        VariableValue::Bool(value) => visitor.visit_bool(*value),
814
56
        VariableValue::Number(value) => visitor.visit_number(value),
815
75
        VariableValue::String(value) => visitor.visit_template(value),
816
    }
817
}
818

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

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

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

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

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

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