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
3565
    fn visit_assert(&mut self, assert: &Assert) {
39
3565
        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
744
    fn visit_body(&mut self, body: &Body) {
49
744
        walk_body(self, body);
50
    }
51

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

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

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

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

            
74
105
    fn visit_count_option(&mut self, option: &CountOption) {
75
105
        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
258
    fn visit_entry(&mut self, entry: &Entry) {
89
258
        walk_entry(self, entry);
90
    }
91

            
92
1151
    fn visit_entry_option(&mut self, option: &EntryOption) {
93
1151
        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
1221
    fn visit_filter(&mut self, filter: &Filter) {
111
1221
        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
138
    fn visit_integer_value(&mut self, n: &IntegerValue) {
131
138
        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
1011
    fn visit_kv(&mut self, kv: &KeyValue) {
139
1011
        walk_kv(self, kv);
140
    }
141

            
142
15023
    fn visit_lt(&mut self, lt: &LineTerminator) {
143
15023
        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
3565
    fn visit_predicate(&mut self, predicate: &Predicate) {
165
3565
        walk_predicate(self, predicate);
166
    }
167

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

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

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

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

            
180
258
    fn visit_request(&mut self, request: &Request) {
181
258
        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
1401
    fn visit_section(&mut self, section: &Section) {
191
1401
        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
1401
    fn visit_section_value(&mut self, section_value: &SectionValue) {
201
1401
        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
3565
pub fn walk_assert<V: Visitor>(visitor: &mut V, assert: &Assert) {
230
3565
    assert.line_terminators.iter().for_each(|lt| {
231
60
        visitor.visit_lt(lt);
232
60
    });
233
3565
    visitor.visit_whitespace(&assert.space0);
234
3565
    visitor.visit_query(&assert.query);
235
3565
    for (space, filter) in assert.filters.iter() {
236
1168
        visitor.visit_whitespace(space);
237
1168
        visitor.visit_filter(filter);
238
    }
239
3565
    visitor.visit_whitespace(&assert.space1);
240
3565
    visitor.visit_predicate(&assert.predicate);
241
3565
    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
744
pub fn walk_body<V: Visitor>(visitor: &mut V, body: &Body) {
253
744
    body.line_terminators.iter().for_each(|lt| {
254
90
        visitor.visit_lt(lt);
255
90
    });
256
744
    visitor.visit_whitespace(&body.space0);
257
744
    match &body.value {
258
71
        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
260
        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
744
    visitor.visit_lt(&body.line_terminator0);
267
}
268

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

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

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

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

            
320
105
pub fn walk_count_option<V: Visitor>(visitor: &mut V, option: &CountOption) {
321
105
    match option {
322
76
        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
1873
pub fn walk_entry<V: Visitor>(visitor: &mut V, entry: &Entry) {
342
1873
    visitor.visit_request(&entry.request);
343
1873
    if let Some(ref response) = entry.response {
344
1509
        visitor.visit_response(response);
345
    }
346
}
347

            
348
1151
pub fn walk_entry_option<V: Visitor>(visitor: &mut V, option: &EntryOption) {
349
1151
    option.line_terminators.iter().for_each(|lt| {
350
13
        visitor.visit_lt(lt);
351
13
    });
352
1151
    visitor.visit_whitespace(&option.space0);
353
1151
    visitor.visit_string(option.kind.identifier());
354
1151
    visitor.visit_whitespace(&option.space1);
355
1151
    visitor.visit_literal(":");
356
1151
    visitor.visit_whitespace(&option.space2);
357
1151
    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
104
        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
16
        OptionKind::NetRc(value) => visitor.visit_bool_option(value),
380
16
        OptionKind::NetRcFile(filename) => visitor.visit_filename(filename),
381
16
        OptionKind::NetRcOptional(value) => visitor.visit_bool_option(value),
382
16
        OptionKind::Output(filename) => visitor.visit_filename(filename),
383
16
        OptionKind::PathAsIs(value) => visitor.visit_bool_option(value),
384
16
        OptionKind::PinnedPublicKey(value) => visitor.visit_template(value),
385
26
        OptionKind::Proxy(value) => visitor.visit_template(value),
386
29
        OptionKind::Repeat(value) => visitor.visit_count_option(value),
387
16
        OptionKind::Resolve(value) => visitor.visit_template(value),
388
50
        OptionKind::Retry(value) => visitor.visit_count_option(value),
389
42
        OptionKind::RetryInterval(value) => visitor.visit_duration_option(value),
390
16
        OptionKind::Skip(value) => visitor.visit_bool_option(value),
391
16
        OptionKind::UnixSocket(value) => visitor.visit_filename(value),
392
26
        OptionKind::User(value) => visitor.visit_template(value),
393
142
        OptionKind::Variable(value) => visitor.visit_variable_def(value),
394
50
        OptionKind::Verbose(value) => visitor.visit_bool_option(value),
395
21
        OptionKind::VeryVerbose(value) => visitor.visit_bool_option(value),
396
    };
397
1151
    visitor.visit_lt(&option.line_terminator0);
398
}
399

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

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

            
496
51
pub fn walk_filename_param<V: Visitor>(visitor: &mut V, param: &FilenameParam) {
497
51
    param.line_terminators.iter().for_each(|lt| {
498
        visitor.visit_lt(lt);
499
    });
500
51
    visitor.visit_whitespace(&param.space0);
501
51
    visitor.visit_template(&param.key);
502
51
    visitor.visit_whitespace(&param.space1);
503
51
    visitor.visit_literal(":");
504
51
    visitor.visit_whitespace(&param.space2);
505
51
    visitor.visit_filename_value(&param.value);
506
51
    visitor.visit_lt(&param.line_terminator0);
507
}
508

            
509
51
pub fn walk_filename_value<V: Visitor>(visitor: &mut V, value: &FilenameValue) {
510
51
    visitor.visit_literal("file,");
511
51
    visitor.visit_whitespace(&value.space0);
512
51
    visitor.visit_filename(&value.filename);
513
51
    visitor.visit_whitespace(&value.space1);
514
51
    visitor.visit_literal(";");
515
51
    visitor.visit_whitespace(&value.space2);
516
51
    if let Some(content_type) = &value.content_type {
517
18
        visitor.visit_template(content_type);
518
    }
519
}
520

            
521
pub fn walk_header<V: Visitor>(visitor: &mut V, header: &KeyValue) {
522
    visitor.visit_kv(header);
523
}
524

            
525
346
pub fn walk_hex<V: Visitor>(visitor: &mut V, hex: &Hex) {
526
346
    visitor.visit_literal("hex,");
527
346
    visitor.visit_whitespace(&hex.space0);
528
346
    visitor.visit_hex_value(&hex.value, &hex.source);
529
346
    visitor.visit_whitespace(&hex.space1);
530
346
    visitor.visit_literal(";");
531
}
532

            
533
662
pub fn walk_hurl_file<V: Visitor>(visitor: &mut V, file: &HurlFile) {
534
1873
    file.entries.iter().for_each(|e| visitor.visit_entry(e));
535
662
    file.line_terminators.iter().for_each(|lt| {
536
299
        visitor.visit_lt(lt);
537
299
    });
538
}
539

            
540
138
pub fn walk_integer_value<V: Visitor>(visitor: &mut V, n: &IntegerValue) {
541
138
    match n {
542
128
        IntegerValue::Literal(value) => visitor.visit_i64(value.as_i64()),
543
10
        IntegerValue::Placeholder(value) => visitor.visit_placeholder(value),
544
    }
545
}
546

            
547
1011
pub fn walk_kv<V: Visitor>(visitor: &mut V, kv: &KeyValue) {
548
1011
    kv.line_terminators.iter().for_each(|lt| {
549
18
        visitor.visit_lt(lt);
550
18
    });
551
1011
    visitor.visit_whitespace(&kv.space0);
552
1011
    visitor.visit_template(&kv.key);
553
1011
    visitor.visit_whitespace(&kv.space1);
554
1011
    visitor.visit_literal(":");
555
1011
    visitor.visit_whitespace(&kv.space2);
556
1011
    visitor.visit_template(&kv.value);
557
1011
    visitor.visit_lt(&kv.line_terminator0);
558
}
559

            
560
15023
pub fn walk_lt<V: Visitor>(visitor: &mut V, lt: &LineTerminator) {
561
15023
    visitor.visit_whitespace(&lt.space0);
562
15023
    if let Some(ref comment) = lt.comment {
563
1883
        visitor.visit_comment(comment);
564
    }
565
15023
    visitor.visit_whitespace(&lt.newline);
566
}
567

            
568
16
pub fn walk_natural_option<V: Visitor>(visitor: &mut V, option: &NaturalOption) {
569
16
    match option {
570
8
        NaturalOption::Literal(value) => visitor.visit_u64(value),
571
8
        NaturalOption::Placeholder(value) => visitor.visit_placeholder(value),
572
    }
573
}
574

            
575
3853
pub fn walk_query<V: Visitor>(visitor: &mut V, query: &Query) {
576
3853
    visitor.visit_query_kind(&query.value);
577

            
578
3853
    match &query.value {
579
327
        QueryValue::Header { space0, name } => {
580
327
            visitor.visit_whitespace(space0);
581
327
            visitor.visit_template(name);
582
        }
583
81
        QueryValue::Cookie { space0, expr } => {
584
81
            visitor.visit_whitespace(space0);
585
81
            visitor.visit_cookie_path(expr);
586
        }
587
148
        QueryValue::Xpath { space0, expr } => {
588
148
            visitor.visit_whitespace(space0);
589
148
            visitor.visit_template(expr);
590
        }
591
2148
        QueryValue::Jsonpath { space0, expr } => {
592
2148
            visitor.visit_whitespace(space0);
593
2148
            visitor.visit_template(expr);
594
        }
595
38
        QueryValue::Regex { space0, value } => {
596
38
            visitor.visit_whitespace(space0);
597
38
            match value {
598
23
                RegexValue::Template(t) => visitor.visit_template(t),
599
15
                RegexValue::Regex(r) => visitor.visit_regex(r),
600
            }
601
        }
602
234
        QueryValue::Variable { space0, name } => {
603
234
            visitor.visit_whitespace(space0);
604
234
            visitor.visit_template(name);
605
        }
606
        QueryValue::Certificate {
607
80
            space0,
608
80
            attribute_name,
609
80
        } => {
610
80
            visitor.visit_whitespace(space0);
611
80
            visitor.visit_string(attribute_name.to_source().as_str());
612
        }
613
        QueryValue::Body
614
        | QueryValue::Status
615
        | QueryValue::Url
616
        | QueryValue::Duration
617
        | QueryValue::Bytes
618
        | QueryValue::Sha256
619
        | QueryValue::Md5
620
        | QueryValue::Version
621
        | QueryValue::Ip
622
797
        | QueryValue::Redirects => {}
623
    }
624
}
625

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

            
693
3009
pub fn walk_predicate_value<V: Visitor>(visitor: &mut V, pred_value: &PredicateValue) {
694
3009
    match pred_value {
695
13
        PredicateValue::Base64(value) => visitor.visit_base64(value),
696
63
        PredicateValue::Bool(value) => visitor.visit_bool(*value),
697
18
        PredicateValue::File(value) => visitor.visit_file(value),
698
307
        PredicateValue::Hex(value) => visitor.visit_hex(value),
699
68
        PredicateValue::MultilineString(value) => visitor.visit_multiline_string(value),
700
28
        PredicateValue::Null => visitor.visit_null("null"),
701
893
        PredicateValue::Number(value) => visitor.visit_number(value),
702
133
        PredicateValue::Placeholder(placeholder) => visitor.visit_placeholder(placeholder),
703
76
        PredicateValue::Regex(value) => visitor.visit_regex(value),
704
1410
        PredicateValue::String(value) => visitor.visit_template(value),
705
    }
706
}
707

            
708
1873
pub fn walk_request<V: Visitor>(visitor: &mut V, request: &Request) {
709
2711
    request.line_terminators.iter().for_each(|lt| {
710
2685
        visitor.visit_lt(lt);
711
2685
    });
712
1873
    visitor.visit_whitespace(&request.space0);
713
1873
    visitor.visit_method(&request.method);
714
1873
    visitor.visit_whitespace(&request.space1);
715
1873
    visitor.visit_url(&request.url);
716
1873
    visitor.visit_lt(&request.line_terminator0);
717
1873
    request.headers.iter().for_each(|h| visitor.visit_kv(h));
718
1873
    request
719
1873
        .sections
720
1873
        .iter()
721
1873
        .for_each(|s| visitor.visit_section(s));
722
1873
    if let Some(body) = &request.body {
723
284
        visitor.visit_body(body);
724
    }
725
}
726

            
727
1509
pub fn walk_response<V: Visitor>(visitor: &mut V, response: &Response) {
728
1509
    response.line_terminators.iter().for_each(|lt| {
729
41
        visitor.visit_lt(lt);
730
41
    });
731
1509
    visitor.visit_whitespace(&response.space0);
732
1509
    visitor.visit_version(&response.version.value);
733
1509
    visitor.visit_whitespace(&response.space1);
734
1509
    visitor.visit_status(&response.status.value);
735
1509
    visitor.visit_lt(&response.line_terminator0);
736
1509
    response.headers.iter().for_each(|h| visitor.visit_kv(h));
737
1509
    response
738
1509
        .sections
739
1509
        .iter()
740
1509
        .for_each(|s| visitor.visit_section(s));
741
1509
    if let Some(body) = &response.body {
742
460
        visitor.visit_body(body);
743
    }
744
}
745

            
746
1401
pub fn walk_section<V: Visitor>(visitor: &mut V, section: &Section) {
747
1401
    section.line_terminators.iter().for_each(|lt| {
748
150
        visitor.visit_lt(lt);
749
150
    });
750
1401
    visitor.visit_whitespace(&section.space0);
751
1401
    let name = format!("[{}]", section.identifier());
752
1401
    visitor.visit_section_header(&name);
753
1401
    visitor.visit_lt(&section.line_terminator0);
754
1401
    visitor.visit_section_value(&section.value);
755
}
756

            
757
1401
pub fn walk_section_value<V: Visitor>(visitor: &mut V, section_value: &SectionValue) {
758
18
    match section_value {
759
3565
        SectionValue::Asserts(asserts) => asserts.iter().for_each(|a| visitor.visit_assert(a)),
760
18
        SectionValue::BasicAuth(Some(auth)) => visitor.visit_kv(auth),
761
        SectionValue::BasicAuth(_) => {}
762
288
        SectionValue::Captures(captures) => captures.iter().for_each(|c| visitor.visit_capture(c)),
763
64
        SectionValue::Cookies(cookies) => cookies.iter().for_each(|c| visitor.visit_cookie(c)),
764
92
        SectionValue::FormParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
765
74
        SectionValue::MultipartFormData(params, _) => params.iter().for_each(|p| match p {
766
23
            MultipartParam::Param(param) => visitor.visit_kv(param),
767
51
            MultipartParam::FilenameParam(param) => visitor.visit_filename_param(param),
768
74
        }),
769
360
        SectionValue::Options(options) => {
770
1151
            options.iter().for_each(|o| visitor.visit_entry_option(o));
771
        }
772
193
        SectionValue::QueryParams(params, _) => params.iter().for_each(|p| visitor.visit_kv(p)),
773
    }
774
}
775

            
776
142
pub fn walk_variable_def<V: Visitor>(visitor: &mut V, def: &VariableDefinition) {
777
142
    visitor.visit_variable_name(&def.name);
778
142
    visitor.visit_whitespace(&def.space0);
779
142
    visitor.visit_literal("=");
780
142
    visitor.visit_whitespace(&def.space1);
781
142
    visitor.visit_variable_value(&def.value);
782
}
783

            
784
142
pub fn walk_variable_value<V: Visitor>(visitor: &mut V, value: &VariableValue) {
785
142
    match value {
786
8
        VariableValue::Null => visitor.visit_null("null"),
787
8
        VariableValue::Bool(value) => visitor.visit_bool(*value),
788
56
        VariableValue::Number(value) => visitor.visit_number(value),
789
70
        VariableValue::String(value) => visitor.visit_template(value),
790
    }
791
}
792

            
793
#[cfg(test)]
794
mod tests {
795
    use crate::ast::visit::Visitor;
796
    use crate::ast::Assert;
797
    use crate::parser;
798

            
799
    #[test]
800
    fn test_walk_assert() {
801
        struct AssertWalker {
802
            count: usize,
803
        }
804

            
805
        impl Visitor for AssertWalker {
806
            fn visit_assert(&mut self, _assert: &Assert) {
807
                self.count += 1;
808
            }
809
        }
810

            
811
        let mut walker = AssertWalker { count: 0 };
812
        let content = r#"
813
GET https://foo.com
814
HTTP 200
815
[Asserts]
816
jsonpath "$.toto[0]" == "tata"
817
jsonpath "$.toto[1]" == "toto"
818
jsonpath "$.toto[2]" == "titi"
819
jsonpath "$.toto[3]" == "tata"
820
jsonpath "$.toto[4]" == "tutu"
821

            
822
GET https://foo.com
823
HTTP 200
824
[Asserts]
825
status == 200
826
header "Location" not exists
827
"#;
828
        let file = parser::parse_hurl_file(content).unwrap();
829
        walker.visit_hurl_file(&file);
830
        assert_eq!(walker.count, 7);
831
    }
832
}