1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
//! 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
    DurationOption, Entry, EntryOption, File, FilenameParam, FilenameValue, Filter, FilterValue,
26
    Hex, HurlFile, IntegerValue, JsonValue, KeyValue, LineTerminator, Method, MultilineString,
27
    MultipartParam, NaturalOption, Number, OptionKind, Placeholder, Predicate, PredicateFuncValue,
28
    PredicateValue, Query, QueryValue, Regex, RegexValue, Request, Response, Section, SectionValue,
29
    StatusValue, Template, VariableDefinition, VariableValue, VersionValue, Whitespace, U64,
30
};
31
use crate::typing::{Count, Duration, DurationUnit, SourceString, ToSource};
32

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
222
    fn visit_version(&mut self, value: &VersionValue) {}
223

            
224
    fn visit_xml_body(&mut self, xml: &str) {}
225

            
226
    fn visit_whitespace(&mut self, ws: &Whitespace) {}
227
}
228

            
229
3909
pub fn walk_assert<V: Visitor>(visitor: &mut V, assert: &Assert) {
230
3909
    assert.line_terminators.iter().for_each(|lt| {
231
70
        visitor.visit_lt(lt);
232
70
    });
233
3909
    visitor.visit_whitespace(&assert.space0);
234
3909
    visitor.visit_query(&assert.query);
235
3909
    for (space, filter) in assert.filters.iter() {
236
1514
        visitor.visit_whitespace(space);
237
1514
        visitor.visit_filter(filter);
238
    }
239
3909
    visitor.visit_whitespace(&assert.space1);
240
3909
    visitor.visit_predicate(&assert.predicate);
241
3909
    visitor.visit_lt(&assert.line_terminator0);
242
}
243

            
244
80
pub fn walk_base64<V: Visitor>(visitor: &mut V, base64: &Base64) {
245
80
    visitor.visit_literal("base64,");
246
80
    visitor.visit_whitespace(&base64.space0);
247
80
    visitor.visit_base64_value(&base64.value, &base64.source);
248
80
    visitor.visit_whitespace(&base64.space1);
249
80
    visitor.visit_literal(";");
250
}
251

            
252
754
pub fn walk_body<V: Visitor>(visitor: &mut V, body: &Body) {
253
754
    body.line_terminators.iter().for_each(|lt| {
254
90
        visitor.visit_lt(lt);
255
90
    });
256
754
    visitor.visit_whitespace(&body.space0);
257
754
    match &body.value {
258
76
        Bytes::Json(value) => visitor.visit_json_body(value),
259
28
        Bytes::Xml(value) => visitor.visit_xml_body(value),
260
197
        Bytes::MultilineString(value) => visitor.visit_multiline_string(value),
261
265
        Bytes::OnelineString(value) => visitor.visit_template(value),
262
67
        Bytes::Base64(value) => visitor.visit_base64(value),
263
82
        Bytes::File(value) => visitor.visit_file(value),
264
39
        Bytes::Hex(value) => visitor.visit_hex(value),
265
    }
266
754
    visitor.visit_lt(&body.line_terminator0);
267
}
268

            
269
599
pub fn walk_bool_option<V: Visitor>(visitor: &mut V, option: &BooleanOption) {
270
599
    match option {
271
450
        BooleanOption::Literal(value) => visitor.visit_bool(*value),
272
149
        BooleanOption::Placeholder(value) => visitor.visit_placeholder(value),
273
    }
274
}
275

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

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

            
313
81
pub fn walk_count<V: Visitor>(visitor: &mut V, count: Count) {
314
81
    match count {
315
65
        Count::Finite(count) => visitor.visit_usize(count),
316
16
        Count::Infinite => visitor.visit_i64(-1),
317
    }
318
}
319

            
320
110
pub fn walk_count_option<V: Visitor>(visitor: &mut V, option: &CountOption) {
321
110
    match option {
322
81
        CountOption::Literal(value) => visitor.visit_count(*value),
323
29
        CountOption::Placeholder(value) => visitor.visit_placeholder(value),
324
    }
325
}
326

            
327
74
pub fn walk_duration<V: Visitor>(visitor: &mut V, duration: &Duration) {
328
74
    visitor.visit_u64(&duration.value);
329
74
    if let Some(unit) = duration.unit {
330
64
        visitor.visit_duration_unit(unit);
331
    }
332
}
333

            
334
106
pub fn walk_duration_option<V: Visitor>(visitor: &mut V, option: &DurationOption) {
335
106
    match option {
336
74
        DurationOption::Literal(value) => visitor.visit_duration(value),
337
32
        DurationOption::Placeholder(value) => visitor.visit_placeholder(value),
338
    }
339
}
340

            
341
1915
pub fn walk_entry<V: Visitor>(visitor: &mut V, entry: &Entry) {
342
1915
    visitor.visit_request(&entry.request);
343
1915
    if let Some(ref response) = entry.response {
344
1539
        visitor.visit_response(response);
345
    }
346
}
347

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

            
402
100
pub fn walk_file<V: Visitor>(visitor: &mut V, file: &File) {
403
100
    visitor.visit_literal("file,");
404
100
    visitor.visit_whitespace(&file.space0);
405
100
    visitor.visit_filename(&file.filename);
406
100
    visitor.visit_whitespace(&file.space1);
407
100
    visitor.visit_literal(";");
408
}
409

            
410
1572
pub fn walk_filter<V: Visitor>(visitor: &mut V, filter: &Filter) {
411
1572
    visitor.visit_filter_kind(&filter.value);
412
1572
    match &filter.value {
413
38
        FilterValue::Base64Decode => {}
414
23
        FilterValue::Base64Encode => {}
415
38
        FilterValue::Base64UrlSafeDecode => {}
416
23
        FilterValue::Base64UrlSafeEncode => {}
417
325
        FilterValue::Count => {}
418
13
        FilterValue::DaysAfterNow => {}
419
26
        FilterValue::DaysBeforeNow => {}
420
51
        FilterValue::Decode { space0, encoding } => {
421
51
            visitor.visit_whitespace(space0);
422
51
            visitor.visit_template(encoding);
423
        }
424
28
        FilterValue::First => {}
425
26
        FilterValue::Format { space0, fmt } => {
426
26
            visitor.visit_whitespace(space0);
427
26
            visitor.visit_template(fmt);
428
        }
429
49
        FilterValue::DateFormat { space0, fmt } => {
430
49
            visitor.visit_whitespace(space0);
431
49
            visitor.visit_template(fmt);
432
        }
433
23
        FilterValue::HtmlEscape => {}
434
33
        FilterValue::HtmlUnescape => {}
435
44
        FilterValue::JsonPath { space0, expr } => {
436
44
            visitor.visit_whitespace(space0);
437
44
            visitor.visit_template(expr);
438
        }
439
28
        FilterValue::Last => {}
440
80
        FilterValue::Location => {}
441
218
        FilterValue::Nth { space0, n } => {
442
218
            visitor.visit_whitespace(space0);
443
218
            visitor.visit_integer_value(n);
444
        }
445
48
        FilterValue::Regex { space0, value } => {
446
48
            visitor.visit_whitespace(space0);
447
48
            match value {
448
10
                RegexValue::Template(value) => visitor.visit_template(value),
449
38
                RegexValue::Regex(regex) => visitor.visit_regex(regex),
450
            }
451
        }
452
        FilterValue::Replace {
453
55
            space0,
454
55
            old_value,
455
55
            space1,
456
55
            new_value,
457
55
        } => {
458
55
            visitor.visit_whitespace(space0);
459
55
            visitor.visit_template(old_value);
460
55
            visitor.visit_whitespace(space1);
461
55
            visitor.visit_template(new_value);
462
        }
463
        FilterValue::ReplaceRegex {
464
33
            space0,
465
33
            pattern,
466
33
            space1,
467
33
            new_value,
468
        } => {
469
33
            visitor.visit_whitespace(space0);
470
33
            match pattern {
471
10
                RegexValue::Template(value) => visitor.visit_template(value),
472
23
                RegexValue::Regex(regex) => visitor.visit_regex(regex),
473
            }
474
33
            visitor.visit_whitespace(space1);
475
33
            visitor.visit_template(new_value);
476
        }
477
28
        FilterValue::Split { space0, sep } => {
478
28
            visitor.visit_whitespace(space0);
479
28
            visitor.visit_template(sep);
480
        }
481
93
        FilterValue::ToDate { space0, fmt } => {
482
93
            visitor.visit_whitespace(space0);
483
93
            visitor.visit_template(fmt);
484
        }
485
48
        FilterValue::ToFloat => {}
486
28
        FilterValue::ToHex => {}
487
48
        FilterValue::ToInt => {}
488
23
        FilterValue::UrlDecode => {}
489
23
        FilterValue::UrlEncode => {}
490
28
        FilterValue::XPath { space0, expr } => {
491
28
            visitor.visit_whitespace(space0);
492
28
            visitor.visit_template(expr);
493
        }
494
18
        FilterValue::ToString => {}
495
33
        FilterValue::UrlQueryParam { space0, param } => {
496
33
            visitor.visit_whitespace(space0);
497
33
            visitor.visit_template(param);
498
        }
499
    }
500
}
501

            
502
51
pub fn walk_filename_param<V: Visitor>(visitor: &mut V, param: &FilenameParam) {
503
51
    param.line_terminators.iter().for_each(|lt| {
504
        visitor.visit_lt(lt);
505
    });
506
51
    visitor.visit_whitespace(&param.space0);
507
51
    visitor.visit_template(&param.key);
508
51
    visitor.visit_whitespace(&param.space1);
509
51
    visitor.visit_literal(":");
510
51
    visitor.visit_whitespace(&param.space2);
511
51
    visitor.visit_filename_value(&param.value);
512
51
    visitor.visit_lt(&param.line_terminator0);
513
}
514

            
515
51
pub fn walk_filename_value<V: Visitor>(visitor: &mut V, value: &FilenameValue) {
516
51
    visitor.visit_literal("file,");
517
51
    visitor.visit_whitespace(&value.space0);
518
51
    visitor.visit_filename(&value.filename);
519
51
    visitor.visit_whitespace(&value.space1);
520
51
    visitor.visit_literal(";");
521
51
    visitor.visit_whitespace(&value.space2);
522
51
    if let Some(content_type) = &value.content_type {
523
18
        visitor.visit_template(content_type);
524
    }
525
}
526

            
527
pub fn walk_header<V: Visitor>(visitor: &mut V, header: &KeyValue) {
528
    visitor.visit_kv(header);
529
}
530

            
531
346
pub fn walk_hex<V: Visitor>(visitor: &mut V, hex: &Hex) {
532
346
    visitor.visit_literal("hex,");
533
346
    visitor.visit_whitespace(&hex.space0);
534
346
    visitor.visit_hex_value(&hex.value, &hex.source);
535
346
    visitor.visit_whitespace(&hex.space1);
536
346
    visitor.visit_literal(";");
537
}
538

            
539
667
pub fn walk_hurl_file<V: Visitor>(visitor: &mut V, file: &HurlFile) {
540
1915
    file.entries.iter().for_each(|e| visitor.visit_entry(e));
541
667
    file.line_terminators.iter().for_each(|lt| {
542
304
        visitor.visit_lt(lt);
543
304
    });
544
}
545

            
546
218
pub fn walk_integer_value<V: Visitor>(visitor: &mut V, n: &IntegerValue) {
547
218
    match n {
548
208
        IntegerValue::Literal(value) => visitor.visit_i64(value.as_i64()),
549
10
        IntegerValue::Placeholder(value) => visitor.visit_placeholder(value),
550
    }
551
}
552

            
553
1041
pub fn walk_kv<V: Visitor>(visitor: &mut V, kv: &KeyValue) {
554
1041
    kv.line_terminators.iter().for_each(|lt| {
555
23
        visitor.visit_lt(lt);
556
23
    });
557
1041
    visitor.visit_whitespace(&kv.space0);
558
1041
    visitor.visit_template(&kv.key);
559
1041
    visitor.visit_whitespace(&kv.space1);
560
1041
    visitor.visit_literal(":");
561
1041
    visitor.visit_whitespace(&kv.space2);
562
1041
    visitor.visit_template(&kv.value);
563
1041
    visitor.visit_lt(&kv.line_terminator0);
564
}
565

            
566
15772
pub fn walk_lt<V: Visitor>(visitor: &mut V, lt: &LineTerminator) {
567
15772
    visitor.visit_whitespace(&lt.space0);
568
15772
    if let Some(ref comment) = lt.comment {
569
1959
        visitor.visit_comment(comment);
570
    }
571
15772
    visitor.visit_whitespace(&lt.newline);
572
}
573

            
574
16
pub fn walk_natural_option<V: Visitor>(visitor: &mut V, option: &NaturalOption) {
575
16
    match option {
576
8
        NaturalOption::Literal(value) => visitor.visit_u64(value),
577
8
        NaturalOption::Placeholder(value) => visitor.visit_placeholder(value),
578
    }
579
}
580

            
581
4217
pub fn walk_query<V: Visitor>(visitor: &mut V, query: &Query) {
582
4217
    visitor.visit_query_kind(&query.value);
583

            
584
4217
    match &query.value {
585
342
        QueryValue::Header { space0, name } => {
586
342
            visitor.visit_whitespace(space0);
587
342
            visitor.visit_template(name);
588
        }
589
89
        QueryValue::Cookie { space0, expr } => {
590
89
            visitor.visit_whitespace(space0);
591
89
            visitor.visit_cookie_path(expr);
592
        }
593
148
        QueryValue::Xpath { space0, expr } => {
594
148
            visitor.visit_whitespace(space0);
595
148
            visitor.visit_template(expr);
596
        }
597
2236
        QueryValue::Jsonpath { space0, expr } => {
598
2236
            visitor.visit_whitespace(space0);
599
2236
            visitor.visit_template(expr);
600
        }
601
43
        QueryValue::Regex { space0, value } => {
602
43
            visitor.visit_whitespace(space0);
603
43
            match value {
604
23
                RegexValue::Template(t) => visitor.visit_template(t),
605
20
                RegexValue::Regex(r) => visitor.visit_regex(r),
606
            }
607
        }
608
239
        QueryValue::Variable { space0, name } => {
609
239
            visitor.visit_whitespace(space0);
610
239
            visitor.visit_template(name);
611
        }
612
        QueryValue::Certificate {
613
88
            space0,
614
88
            attribute_name,
615
88
        } => {
616
88
            visitor.visit_whitespace(space0);
617
88
            visitor.visit_string(attribute_name.to_source().as_str());
618
        }
619
        QueryValue::Body
620
        | QueryValue::Status
621
        | QueryValue::Url
622
        | QueryValue::Duration
623
        | QueryValue::Bytes
624
        | QueryValue::Sha256
625
        | QueryValue::Md5
626
        | QueryValue::Version
627
        | QueryValue::Ip
628
1032
        | QueryValue::Redirects => {}
629
    }
630
}
631

            
632
3909
pub fn walk_predicate<V: Visitor>(visitor: &mut V, pred: &Predicate) {
633
3909
    if pred.not {
634
213
        visitor.visit_not("not");
635
213
        visitor.visit_whitespace(&pred.space0);
636
    }
637
3909
    let kind = &pred.predicate_func.value;
638
3909
    visitor.visit_predicate_kind(kind);
639
3909
    match kind {
640
2573
        PredicateFuncValue::Equal { space0, value } => {
641
2573
            visitor.visit_whitespace(space0);
642
2573
            visitor.visit_predicate_value(value);
643
        }
644
69
        PredicateFuncValue::NotEqual { space0, value } => {
645
69
            visitor.visit_whitespace(space0);
646
69
            visitor.visit_predicate_value(value);
647
        }
648
84
        PredicateFuncValue::GreaterThan { space0, value } => {
649
84
            visitor.visit_whitespace(space0);
650
84
            visitor.visit_predicate_value(value);
651
        }
652
23
        PredicateFuncValue::GreaterThanOrEqual { space0, value } => {
653
23
            visitor.visit_whitespace(space0);
654
23
            visitor.visit_predicate_value(value);
655
        }
656
67
        PredicateFuncValue::LessThan { space0, value } => {
657
67
            visitor.visit_whitespace(space0);
658
67
            visitor.visit_predicate_value(value);
659
        }
660
33
        PredicateFuncValue::LessThanOrEqual { space0, value } => {
661
33
            visitor.visit_whitespace(space0);
662
33
            visitor.visit_predicate_value(value);
663
        }
664
144
        PredicateFuncValue::StartWith { space0, value } => {
665
144
            visitor.visit_whitespace(space0);
666
144
            visitor.visit_predicate_value(value);
667
        }
668
46
        PredicateFuncValue::EndWith { space0, value } => {
669
46
            visitor.visit_whitespace(space0);
670
46
            visitor.visit_predicate_value(value);
671
        }
672
144
        PredicateFuncValue::Contain { space0, value } => {
673
144
            visitor.visit_whitespace(space0);
674
144
            visitor.visit_predicate_value(value);
675
        }
676
8
        PredicateFuncValue::Include { space0, value } => {
677
8
            visitor.visit_whitespace(space0);
678
8
            visitor.visit_predicate_value(value);
679
        }
680
124
        PredicateFuncValue::Match { space0, value } => {
681
124
            visitor.visit_whitespace(space0);
682
124
            visitor.visit_predicate_value(value);
683
        }
684
        PredicateFuncValue::IsInteger
685
        | PredicateFuncValue::IsFloat
686
        | PredicateFuncValue::IsBoolean
687
        | PredicateFuncValue::IsString
688
        | PredicateFuncValue::IsCollection
689
        | PredicateFuncValue::IsDate
690
        | PredicateFuncValue::IsIsoDate
691
        | PredicateFuncValue::Exist
692
        | PredicateFuncValue::IsEmpty
693
        | PredicateFuncValue::IsNumber
694
        | PredicateFuncValue::IsIpv4
695
        | PredicateFuncValue::IsIpv6
696
594
        | PredicateFuncValue::IsUuid => {}
697
    }
698
}
699

            
700
3315
pub fn walk_predicate_value<V: Visitor>(visitor: &mut V, pred_value: &PredicateValue) {
701
3315
    match pred_value {
702
13
        PredicateValue::Base64(value) => visitor.visit_base64(value),
703
63
        PredicateValue::Bool(value) => visitor.visit_bool(*value),
704
18
        PredicateValue::File(value) => visitor.visit_file(value),
705
307
        PredicateValue::Hex(value) => visitor.visit_hex(value),
706
68
        PredicateValue::MultilineString(value) => visitor.visit_multiline_string(value),
707
28
        PredicateValue::Null => visitor.visit_null("null"),
708
998
        PredicateValue::Number(value) => visitor.visit_number(value),
709
133
        PredicateValue::Placeholder(placeholder) => visitor.visit_placeholder(placeholder),
710
86
        PredicateValue::Regex(value) => visitor.visit_regex(value),
711
1601
        PredicateValue::String(value) => visitor.visit_template(value),
712
    }
713
}
714

            
715
1915
pub fn walk_request<V: Visitor>(visitor: &mut V, request: &Request) {
716
2793
    request.line_terminators.iter().for_each(|lt| {
717
2767
        visitor.visit_lt(lt);
718
2767
    });
719
1915
    visitor.visit_whitespace(&request.space0);
720
1915
    visitor.visit_method(&request.method);
721
1915
    visitor.visit_whitespace(&request.space1);
722
1915
    visitor.visit_url(&request.url);
723
1915
    visitor.visit_lt(&request.line_terminator0);
724
1915
    request.headers.iter().for_each(|h| visitor.visit_kv(h));
725
1915
    request
726
1915
        .sections
727
1915
        .iter()
728
1915
        .for_each(|s| visitor.visit_section(s));
729
1915
    if let Some(body) = &request.body {
730
289
        visitor.visit_body(body);
731
    }
732
}
733

            
734
1539
pub fn walk_response<V: Visitor>(visitor: &mut V, response: &Response) {
735
1539
    response.line_terminators.iter().for_each(|lt| {
736
41
        visitor.visit_lt(lt);
737
41
    });
738
1539
    visitor.visit_whitespace(&response.space0);
739
1539
    visitor.visit_version(&response.version.value);
740
1539
    visitor.visit_whitespace(&response.space1);
741
1539
    visitor.visit_status(&response.status.value);
742
1539
    visitor.visit_lt(&response.line_terminator0);
743
1539
    response.headers.iter().for_each(|h| visitor.visit_kv(h));
744
1539
    response
745
1539
        .sections
746
1539
        .iter()
747
1539
        .for_each(|s| visitor.visit_section(s));
748
1539
    if let Some(body) = &response.body {
749
465
        visitor.visit_body(body);
750
    }
751
}
752

            
753
1478
pub fn walk_section<V: Visitor>(visitor: &mut V, section: &Section) {
754
1478
    section.line_terminators.iter().for_each(|lt| {
755
175
        visitor.visit_lt(lt);
756
175
    });
757
1478
    visitor.visit_whitespace(&section.space0);
758
1478
    let name = format!("[{}]", section.identifier());
759
1478
    visitor.visit_section_header(&name);
760
1478
    visitor.visit_lt(&section.line_terminator0);
761
1478
    visitor.visit_section_value(&section.value);
762
}
763

            
764
1478
pub fn walk_section_value<V: Visitor>(visitor: &mut V, section_value: &SectionValue) {
765
18
    match section_value {
766
3909
        SectionValue::Asserts(asserts) => asserts.iter().for_each(|a| visitor.visit_assert(a)),
767
18
        SectionValue::BasicAuth(Some(auth)) => visitor.visit_kv(auth),
768
        SectionValue::BasicAuth(_) => {}
769
308
        SectionValue::Captures(captures) => captures.iter().for_each(|c| visitor.visit_capture(c)),
770
69
        SectionValue::Cookies(cookies) => cookies.iter().for_each(|c| visitor.visit_cookie(c)),
771
92
        SectionValue::FormParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
772
74
        SectionValue::MultipartFormData(params, _) => params.iter().for_each(|p| match p {
773
23
            MultipartParam::Param(param) => visitor.visit_kv(param),
774
51
            MultipartParam::FilenameParam(param) => visitor.visit_filename_param(param),
775
74
        }),
776
387
        SectionValue::Options(options) => {
777
1215
            options.iter().for_each(|o| visitor.visit_entry_option(o));
778
        }
779
203
        SectionValue::QueryParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
780
    }
781
}
782

            
783
142
pub fn walk_variable_def<V: Visitor>(visitor: &mut V, def: &VariableDefinition) {
784
142
    visitor.visit_variable_name(&def.name);
785
142
    visitor.visit_whitespace(&def.space0);
786
142
    visitor.visit_literal("=");
787
142
    visitor.visit_whitespace(&def.space1);
788
142
    visitor.visit_variable_value(&def.value);
789
}
790

            
791
142
pub fn walk_variable_value<V: Visitor>(visitor: &mut V, value: &VariableValue) {
792
142
    match value {
793
8
        VariableValue::Null => visitor.visit_null("null"),
794
8
        VariableValue::Bool(value) => visitor.visit_bool(*value),
795
56
        VariableValue::Number(value) => visitor.visit_number(value),
796
70
        VariableValue::String(value) => visitor.visit_template(value),
797
    }
798
}
799

            
800
#[cfg(test)]
801
mod tests {
802
    use crate::ast::visit::Visitor;
803
    use crate::ast::Assert;
804
    use crate::parser;
805

            
806
    #[test]
807
    fn test_walk_assert() {
808
        struct AssertWalker {
809
            count: usize,
810
        }
811

            
812
        impl Visitor for AssertWalker {
813
            fn visit_assert(&mut self, _assert: &Assert) {
814
                self.count += 1;
815
            }
816
        }
817

            
818
        let mut walker = AssertWalker { count: 0 };
819
        let content = r#"
820
GET https://foo.com
821
HTTP 200
822
[Asserts]
823
jsonpath "$.toto[0]" == "tata"
824
jsonpath "$.toto[1]" == "toto"
825
jsonpath "$.toto[2]" == "titi"
826
jsonpath "$.toto[3]" == "tata"
827
jsonpath "$.toto[4]" == "tutu"
828

            
829
GET https://foo.com
830
HTTP 200
831
[Asserts]
832
status == 200
833
header "Location" not exists
834
"#;
835
        let file = parser::parse_hurl_file(content).unwrap();
836
        walker.visit_hurl_file(&file);
837
        assert_eq!(walker.count, 7);
838
    }
839
}