granit-parser 2.0.0

A YAML parser with comment, tag and style support, written in pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use granit_parser::{
    BufferedInput, ErrorKind, Event, FallibleBufferedInput, Marker, Parser, ScalarStyle, ScanError,
    Scanner, StrInput, StructureStyle, Tag,
};
use std::borrow::Cow;

fn events_with_inputs(
    yaml: &str,
    strict_indentation: bool,
) -> Vec<Result<Vec<Event<'_>>, ScanError>> {
    let options = granit_parser::options! { strict_indentation: strict_indentation };
    [
        Parser::with_options(StrInput::new(yaml), options.clone()).collect::<Result<Vec<_>, _>>(),
        Parser::new_from_str_with_options(yaml, options.clone()).collect::<Result<Vec<_>, _>>(),
        Parser::new_from_iter_with_options(yaml.chars(), options.clone())
            .collect::<Result<Vec<_>, _>>(),
        Parser::new_from_fallible_iter_with_options(yaml.chars().map(Ok), options)
            .collect::<Result<Vec<_>, _>>(),
    ]
    .into_iter()
    .map(|result| result.map(|events| events.into_iter().map(|(event, _)| event).collect()))
    .collect()
}

fn assert_relaxed_only(yaml: &str, reference: &str, line: usize, col: usize) {
    assert_relaxed_only_with_kind(yaml, reference, line, col, &ErrorKind::InvalidIndentation);
}

fn assert_relaxed_only_with_kind(
    yaml: &str,
    reference: &str,
    line: usize,
    col: usize,
    kind: &ErrorKind,
) {
    let expected = Parser::new_from_str(reference)
        .map(|result| result.expect("valid reference").0)
        .collect::<Vec<_>>();
    let default = Parser::new_from_str(yaml)
        .map(|result| {
            result
                .unwrap_or_else(|error| panic!("default rejected {yaml:?}: {error}"))
                .0
        })
        .collect::<Vec<_>>();
    assert_eq!(default, expected, "default changed events for {yaml:?}");

    for result in events_with_inputs(yaml, false) {
        assert_eq!(
            result.unwrap_or_else(|error| panic!("relaxed mode rejected {yaml:?}: {error}")),
            expected,
            "relaxed mode changed events for {yaml:?}"
        );
    }
    Scanner::new(StrInput::new(yaml))
        .collect::<Result<Vec<_>, _>>()
        .expect("default scanner should accept relaxed indentation");
    Scanner::with_options(
        StrInput::new(yaml),
        granit_parser::options! { strict_indentation: false },
    )
    .collect::<Result<Vec<_>, _>>()
    .expect("explicit relaxed scanner should accept relaxed indentation");

    for result in events_with_inputs(yaml, true) {
        assert_indent_error(
            &result.expect_err("strict parser accepted relaxed indentation"),
            yaml,
            line,
            col,
            kind,
        );
    }
    let error = Scanner::with_options(
        StrInput::new(yaml),
        granit_parser::options! { strict_indentation: true },
    )
    .find_map(Result::err)
    .expect("strict scanner accepted relaxed indentation");
    assert_indent_error(&error, yaml, line, col, kind);

    // The corresponding conforming spelling must also work through every input backend.
    for result in events_with_inputs(reference, true) {
        assert_eq!(
            result.unwrap_or_else(|error| panic!(
                "strict mode rejected reference {reference:?}: {error}"
            )),
            expected
        );
    }
}

fn assert_indent_error(error: &ScanError, yaml: &str, line: usize, col: usize, kind: &ErrorKind) {
    assert_eq!(error.kind(), kind, "input: {yaml:?}");
    assert_eq!(
        (error.marker().line(), error.marker().col()),
        (line, col),
        "input: {yaml:?}"
    );
}

#[test]
fn strict_rejects_under_indented_flow_delimiters() {
    for (yaml, reference, line, col) in [
        ("key: [\n]\n", "key: [\n ]\n", 2, 0),
        ("key: {\n}\n", "key: {\n }\n", 2, 0),
        ("key: [\n a\n, b\n ]\n", "key: [\n a\n , b\n ]\n", 3, 0),
        ("outer:\n  key: [\n  ]\n", "outer:\n  key: [\n   ]\n", 3, 2),
        ("outer:\n  key: {\n }\n", "outer:\n  key: {\n   }\n", 3, 1),
        (
            "outer:\n  key: [one\n  , two]\n",
            "outer:\n  key: [one\n   , two]\n",
            3,
            2,
        ),
    ] {
        assert_relaxed_only(yaml, reference, line, col);
    }
}

#[test]
fn strict_rejects_under_indented_flow_entries_in_all_scalar_styles() {
    // The flow indentation depends on its enclosing block, including a compact mapping
    // inside a sequence. Equal indentation is insufficient in strict YAML.
    for (prefix, parent_indent, suffix) in [
        ("key: [", 0, "]\nafter: done\n"),
        ("- targets: [", 2, "]\n  after: done\n- next\n"),
        ("outer:\n  inner: [", 2, "]\n  after: done\nlast: end\n"),
        ("key:\n  [", 0, "]\nafter: done\n"),
    ] {
        for quote in ["", "'", "\""] {
            let value = format!("{quote}192.168.1.1:9100{quote}");
            let reference = format!("{prefix}{value}{suffix}");
            let line = prefix.lines().count() + 1;
            let closing_indent = " ".repeat(parent_indent + 1);
            for indent in 0..=parent_indent {
                let spaces = " ".repeat(indent);
                let yaml = format!("{prefix}\n{spaces}{value}\n{closing_indent}{suffix}");
                assert_relaxed_only(&yaml, &reference, line, indent);
            }
        }
    }
}

#[test]
fn strict_rejects_under_indented_mapping_entries_and_nested_collections() {
    for (yaml, reference, line, col) in [
        ("key: {\none: value\n }\n", "key: {one: value}\n", 2, 0),
        (
            "outer:\n  key: {\n  \"one\": value\n   }\n",
            "outer:\n  key: {\"one\": value}\n",
            3,
            2,
        ),
        ("key: {one:\nvalue\n }\n", "key: {one: value}\n", 2, 0),
        ("key: [\n[nested]\n ]\n", "key: [[nested]]\n", 2, 0),
        (
            "key: [\n{nested: value}\n ]\n",
            "key: [{nested: value}]\n",
            2,
            0,
        ),
        (
            "key: {\n? name: value\n }\n",
            "key: {? name: value}\n",
            2,
            0,
        ),
        (
            "key: { ? name\n:\n value\n }\n",
            "key: {? name: value}\n",
            2,
            0,
        ),
    ] {
        assert_relaxed_only(yaml, reference, line, col);
    }
}

#[test]
fn strict_rejects_under_indented_node_properties_and_aliases() {
    for properties in ["&a", "!custom", "&a !custom", "!custom &a"] {
        let yaml = format!("key: [\n{properties} value\n ]\n");
        let reference = format!("key: [{properties} value]\n");
        assert_relaxed_only(&yaml, &reference, 2, 0);

        let yaml = format!("key: [ {properties}\nvalue\n ]\n");
        assert_relaxed_only(&yaml, &reference, 2, 0);
    }
    assert_relaxed_only("key: [&a value,\n*a\n ]\n", "key: [&a value, *a]\n", 2, 0);
}

#[test]
fn strict_rejects_under_indented_multiline_scalar_content() {
    for (yaml, reference) in [
        ("key: [first\nsecond]\n", "key: [first second]\n"),
        ("key: ['first\nsecond']\n", "key: ['first second']\n"),
        ("key: [\"first\nsecond\"]\n", "key: [\"first second\"]\n"),
        ("key: [\"first\\\nsecond\"]\n", "key: [\"firstsecond\"]\n"),
        ("key: ['first\n']\n", "key: ['first ']\n"),
        ("key: [\"first\n\"]\n", "key: [\"first \"]\n"),
        ("key: [\"first\\\n\"]\n", "key: [\"first\"]\n"),
    ] {
        assert_relaxed_only(yaml, reference, 2, 0);
    }
}

#[test]
fn strict_rejects_under_indented_escaped_line_breaks_in_block_context() {
    // An escaped line break in a double-quoted block value is followed by
    // s-flow-line-prefix(n), like a folded break.
    for (yaml, reference, line, col) in [
        ("key: \"first\\\nsecond\"\n", "key: \"firstsecond\"\n", 2, 0),
        ("- \"first\\\nsecond\"\n", "- \"firstsecond\"\n", 2, 0),
        ("key: \"first\\\n\"\n", "key: \"first\"\n", 2, 0),
        ("- \"first\\\n\"\n", "- \"first\"\n", 2, 0),
        (
            "outer:\n  key: \"first\\\n  \"\n",
            "outer:\n  key: \"first\"\n",
            3,
            2,
        ),
        ("- key: \"first\\\n  \"\n", "- key: \"first\"\n", 2, 2),
        (
            "outer:\n  key: \"first\\\n  second\"\n",
            "outer:\n  key: \"firstsecond\"\n",
            3,
            2,
        ),
        (
            "key: \"first\\\n\nsecond\"\n",
            "key: \"first\\nsecond\"\n",
            3,
            0,
        ),
        ("key: \"first\\\n\n\"\n", "key: \"first\\n\"\n", 3, 0),
        ("key: \"first\\\r\n\"\r\n", "key: \"first\"\n", 2, 0),
    ] {
        assert_relaxed_only(yaml, reference, line, col);
    }
    // Indented continuations and closing quotes stay valid; root scalars need no indentation.
    for yaml in [
        "key: \"first\\\n second\"\n",
        "key: \"first\\\n \"\n",
        "outer:\n  key: \"first\\\n   \"\n",
        "- key: \"first\\\n   \"\n",
        "\"first\\\n\"\n",
    ] {
        for result in events_with_inputs(yaml, true) {
            result.unwrap_or_else(|error| panic!("strict rejected {yaml:?}: {error}"));
        }
    }
}

#[test]
fn strict_rejects_under_indented_closing_quotes_after_folded_line_breaks() {
    for quote in ['\'', '"'] {
        for (prefix, indent, line) in [("key: ", 0, 2), ("- ", 0, 2), ("outer:\n  key: ", 2, 3)] {
            let spaces = " ".repeat(indent);
            let yaml = format!("{prefix}{quote}first\n{spaces}{quote}\n");
            let reference = format!("{prefix}{quote}first\n{spaces} {quote}\n");
            assert_relaxed_only_with_kind(
                &yaml,
                &reference,
                line,
                indent,
                &ErrorKind::InvalidQuotedScalarIndent,
            );
        }
    }
}

#[test]
fn strict_rejects_tabs_used_instead_of_flow_indentation() {
    // A preceding plain scalar removes the scanner's temporary indentation level.
    // Tabs still cannot replace the spaces required by the enclosing block.
    for (yaml, kind, line) in [
        (
            "key: [first,\n\tsecond\n ]\n",
            ErrorKind::TabInBlockIndentation,
            2,
        ),
        ("key: [first,\n\t]\n", ErrorKind::TabInBlockIndentation, 2),
        (
            "key: {first: value,\n\t}\n",
            ErrorKind::TabInBlockIndentation,
            2,
        ),
        (
            "key: [first, \"second\"\n\t, third]\n",
            ErrorKind::TabInBlockIndentation,
            2,
        ),
        (
            "outer:\n  key: [first,\n  \tsecond\n   ]\n",
            ErrorKind::TabInBlockIndentation,
            3,
        ),
        (
            "key: [first, 'a\n\tb'\n ]\n",
            ErrorKind::TabInIndentation,
            2,
        ),
        (
            "key: [first, \"a\n\tb\"\n ]\n",
            ErrorKind::TabInIndentation,
            2,
        ),
        (
            "key: [first, \"a\\\n\tb\"\n ]\n",
            ErrorKind::TabInIndentation,
            2,
        ),
        ("key: [first, 'a\n\t']\n", ErrorKind::TabInIndentation, 2),
        (
            "key: [first, \"a\\\n\t\"]\n",
            ErrorKind::TabInIndentation,
            2,
        ),
    ] {
        let expected = Parser::new_from_str(yaml)
            .map(|result| {
                result
                    .expect("default mode should preserve relaxed tab handling")
                    .0
            })
            .collect::<Vec<_>>();
        for result in events_with_inputs(yaml, false) {
            assert_eq!(result.expect("relaxed mode should parse"), expected);
        }
        for result in events_with_inputs(yaml, true) {
            let error = result.expect_err("strict mode accepted tab indentation");
            assert_eq!(error.kind(), &kind, "input: {yaml:?}");
            assert_eq!(error.marker().line(), line, "input: {yaml:?}");
        }
        Scanner::new(StrInput::new(yaml))
            .collect::<Result<Vec<_>, _>>()
            .expect("default scanner should preserve relaxed tab handling");
        Scanner::with_options(
            StrInput::new(yaml),
            granit_parser::options! { strict_indentation: false },
        )
        .collect::<Result<Vec<_>, _>>()
        .expect("relaxed scanner should parse");
        let error = Scanner::with_options(
            StrInput::new(yaml),
            granit_parser::options! { strict_indentation: true },
        )
        .find_map(Result::err)
        .expect("strict scanner accepted tab indentation");
        assert_eq!(error.kind(), &kind, "scanner input: {yaml:?}");
        assert_eq!(error.marker().line(), line, "scanner input: {yaml:?}");
    }
}

#[test]
fn conforming_flow_indentation_has_identical_events_in_both_modes() {
    for yaml in [
        "'first\n'\n",
        "\"first\n\"\n",
        "[\nvalue,\n{key: value}\n]\n",
        "key: [\n 1, 2, 3,\n 4, 5, 6\n ]\n",
        "key:\n  [\n value\n ]\n",
        "key: {\n \"one\":\n 'value',\n other: [\n nested\n ]\n }\n",
        "outer:\n  key: [\n   &a !custom value,\n   *a,\n   {? nested: [one, two]}\n   ]\n  after: done\n",
        "- targets: [\n   value,\n   another\n   ]\n  after: done\n- next\n",
        "key: [first\n second, 'third\n fourth', \"fifth\\\n sixth\"]\n",
        "key: [ # start\n# comment indentation is independent\n value\n# before closing delimiter\n ]\n",
        "key: [\n\n value,\n\n ]\n",
        "key: [first,\n \tsecond,\n \t]\n",
        "key: [first, 'a\n \tb', \"c\\\n \td\"]\n",
        "[first,\n\tsecond,\n\t]\n",
        "[first, 'a\n\tb', \"c\\\n\td\"]\n",
    ] {
        let expected = Parser::new_from_str(yaml)
            .map(|result| result.expect("valid conforming YAML").0)
            .collect::<Vec<_>>();
        for strict_indentation in [false, true] {
            for result in events_with_inputs(yaml, strict_indentation) {
                assert_eq!(
                    result.unwrap_or_else(|error| panic!("strict_indentation={strict_indentation}, input {yaml:?}: {error}")),
                    expected
                );
            }
            Scanner::with_options(StrInput::new(yaml), granit_parser::options! { strict_indentation: strict_indentation })
                .collect::<Result<Vec<_>, _>>()
                .unwrap_or_else(|error| panic!("strict_indentation={strict_indentation}, scanner input {yaml:?}: {error}"));
        }
    }
}

#[test]
fn strict_option_preserves_existing_invalid_structure_checks() {
    for yaml in [
        "key: [\n value\n [nested]\n ]\n",
        "key: {\n one: value\n other: value\n }\n",
        "key: [\n value\n }\n",
        "key: {\nname\n : value\n }\n",
        "key: { &a\nname\n : value\n }\n",
    ] {
        for strict_indentation in [false, true] {
            for result in events_with_inputs(yaml, strict_indentation) {
                assert!(
                    result.is_err(),
                    "strict_indentation={strict_indentation} accepted invalid input {yaml:?}"
                );
            }
        }
    }
}

#[test]
fn strict_errors_preserve_unicode_positions_and_terminate_iteration() {
    fn check<T>(
        mut items: impl Iterator<Item = Result<T, ScanError>>,
        kind: &ErrorKind,
        expected: Marker,
    ) {
        let error = items
            .by_ref()
            .find_map(Result::err)
            .expect("strict indentation must fail");
        assert_eq!(error.kind(), kind);
        assert_eq!(*error.marker(), expected);
        // Marker equality compares character positions; check optional byte offsets separately.
        assert_eq!(error.marker().byte_offset(), expected.byte_offset());
        assert!(items.next().is_none(), "error must terminate iteration");
        assert!(items.next().is_none(), "iterator must remain fused");
    }

    for (prefix, suffix, kind) in [
        ("clé: [😀,\n", "value\n ]\n", ErrorKind::InvalidIndentation),
        ("clé: \"😀\\\n", "value\"\n", ErrorKind::InvalidIndentation),
        ("clé: '😀\n", "'\n", ErrorKind::InvalidQuotedScalarIndent),
    ] {
        for line_break in ["\n", "\r\n"] {
            let prefix = prefix.replace('\n', line_break);
            let yaml = format!("{prefix}{}", suffix.replace('\n', line_break));
            let marker = Marker::new(prefix.chars().count(), 2, 0);
            let byte_marker = marker.with_byte_offset(Some(prefix.len()));
            let options = granit_parser::options! { strict_indentation: true };

            check(
                Parser::new_from_str_with_options(&yaml, options.clone()),
                &kind,
                byte_marker,
            );
            check(
                Parser::new_from_iter_with_options(yaml.chars(), options.clone()),
                &kind,
                marker,
            );
            check(
                Parser::new_from_fallible_iter_with_options(yaml.chars().map(Ok), options.clone()),
                &kind,
                marker,
            );
            check(
                Scanner::with_options(StrInput::new(&yaml), options.clone()),
                &kind,
                byte_marker,
            );
            check(
                Scanner::with_options(BufferedInput::new(yaml.chars()), options.clone()),
                &kind,
                marker,
            );
            check(
                Scanner::with_options(FallibleBufferedInput::new(yaml.chars().map(Ok)), options),
                &kind,
                marker,
            );
        }
    }
}

#[test]
fn strict_flow_indentation_follows_dedents_and_document_boundaries() {
    let yaml =
        "---\nouter:\n  inner: [\n   nested\n   ]\nshallow: [\n value\n ]\n...\n---\n[\nroot\n]\n";
    let reference = "---\nouter:\n  inner: [nested]\nshallow: [value]\n...\n---\n[root]\n";
    let expected = Parser::new_from_str(reference)
        .map(|result| result.unwrap().0)
        .collect::<Vec<_>>();
    for result in events_with_inputs(yaml, true) {
        assert_eq!(
            result.expect("indentation should follow the current block"),
            expected
        );
    }
}

#[test]
fn escaped_continuations_preserve_empty_lines_node_properties_and_aliases() {
    // YAML 1.2.2 [112] discards the escaped break but retains each l-empty [70].
    // Empty lines may contain fewer spaces than the required flow prefix [69].
    let expected = vec![
        Event::StreamStart,
        Event::DocumentStart(false, None),
        Event::MappingStart(StructureStyle::Block, 0, None),
        Event::Scalar("outer".into(), ScalarStyle::Plain, 0, None),
        Event::MappingStart(StructureStyle::Block, 0, None),
        Event::Scalar("key".into(), ScalarStyle::Plain, 0, None),
        Event::Scalar(
            "é\n字".into(),
            ScalarStyle::DoubleQuoted,
            1,
            Some(Cow::Owned(Tag::new("!", "custom"))),
        ),
        Event::Scalar("copy".into(), ScalarStyle::Plain, 0, None),
        Event::Alias(1),
        Event::MappingEnd,
        Event::MappingEnd,
        Event::DocumentEnd,
        Event::StreamEnd,
    ];
    for newline in ["\n", "\r\n"] {
        for empty_line in ["", " ", "   \t"] {
            let yaml = format!(
                "outer:{newline}  key: &a !custom \"é\\{newline}{empty_line}{newline}   字\"{newline}  copy: *a{newline}"
            );
            for strict in [false, true] {
                for result in events_with_inputs(&yaml, strict) {
                    assert_eq!(
                        result.unwrap_or_else(|error| panic!("{yaml:?}: {error}")),
                        expected,
                        "strict={strict}, {yaml:?}"
                    );
                }
            }
        }
    }
}

#[test]
fn strict_escaped_continuations_require_indentation_after_empty_lines_and_properties() {
    for newline in ["\n", "\r\n"] {
        for (prefix, suffix, line) in [
            ("outer:\n  key: &a !custom ", "\n", 4),
            ("- key: &a !custom ", "\n", 3),
            ("outer:\n  key: [&a !custom ", "]\n", 4),
        ] {
            for continuation in ["字", ""] {
                let yaml =
                    format!("{prefix}\"é\\\n \n  {continuation}\"{suffix}").replace('\n', newline);
                let reference = format!("{prefix}\"é\\n{continuation}\"{suffix}");
                assert_relaxed_only(&yaml, &reference, line, 2);
            }
        }
    }
}