granit-parser 3.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
mod support;
use granit_parser::{
    BufferedInput, ErrorKind, Event, FallibleBufferedInput, Options, Parser, Scanner, StrInput,
    TokenType,
};

fn assert_comment_free_value(events: &[(Event<'_>, granit_parser::Span)]) {
    assert!(events
        .iter()
        .all(|(event, _)| !support::is_comment_event(event)));
    assert!(events
        .iter()
        .any(|(event, _)| { matches!(event, Event::Scalar { value, .. } if value == "value") }));
}

#[test]
fn options_macro_starts_with_defaults_and_applies_fields() {
    let defaults = Options::default();
    assert!(!defaults.strict_indentation);
    assert!(!defaults.disallow_directives);
    assert!(!defaults.bom_inside_document_as_content);
    assert!(!defaults.block_scalar_eof_without_line_feed);
    assert!(!defaults.block_scalar_indent_at_least_one);
    assert!(!defaults.emit_unresolved_aliases);
    assert_eq!(defaults.emit_comments, cfg!(feature = "comments"));
    #[cfg(feature = "comments")]
    assert_eq!(defaults.max_buffered_comment_events, 96);
    assert_eq!(defaults.simple_key_max_lookahead, 1024);
    assert_eq!(defaults.flow_nesting_limit, 255);
    assert_eq!(defaults.block_nesting_limit, 255);
    assert_eq!(defaults.max_directive_bytes, 1024);
    assert_eq!(defaults.max_reserved_directive_params, 16);
    assert_eq!(granit_parser::options! {}, defaults);

    let options = granit_parser::options! {
        strict_indentation: true,
        disallow_directives: true,
        emit_unresolved_aliases: true,
        emit_comments: false,
        #[cfg(feature = "comments")]
        max_buffered_comment_events: 7,
        simple_key_max_lookahead: 11,
        flow_nesting_limit: 13,
        block_nesting_limit: 17,
        max_directive_bytes: 19,
        max_reserved_directive_params: 23,
    };

    assert!(options.strict_indentation);
    assert!(options.disallow_directives);
    assert!(options.emit_unresolved_aliases);
    assert!(!options.emit_comments);
    #[cfg(feature = "comments")]
    assert_eq!(options.max_buffered_comment_events, 7);
    assert_eq!(options.simple_key_max_lookahead, 11);
    assert_eq!(options.flow_nesting_limit, 13);
    assert_eq!(options.block_nesting_limit, 17);
    assert_eq!(options.max_directive_bytes, 19);
    assert_eq!(options.max_reserved_directive_params, 23);
}

#[test]
fn options_macro_configures_parser() {
    let options = granit_parser::options! {
        flow_nesting_limit: 1,
    };
    let error = Parser::with_options(StrInput::new("[[]]"), options)
        .find_map(Result::err)
        .expect("macro-configured flow limit should be enforced");

    assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);
}

#[test]
fn comment_emission_option_is_available_with_and_without_comments() {
    let yaml = "# before\nvalue # after\n";
    let mut setter_options = Options::default();

    for emit_comments in [false, true, false] {
        let options = granit_parser::options! { emit_comments: emit_comments };
        setter_options.set_emit_comments(emit_comments);
        assert_eq!(options, setter_options);
        let expect_comments = cfg!(feature = "comments") && emit_comments;

        for events in [
            Parser::new_from_str_with_options(yaml, options.clone()).collect::<Result<Vec<_>, _>>(),
            Parser::new_from_iter_with_options(yaml.chars(), setter_options.clone())
                .collect::<Result<Vec<_>, _>>(),
            Parser::new_from_fallible_iter_with_options(
                yaml.chars().map(Ok),
                setter_options.clone(),
            )
            .collect::<Result<Vec<_>, _>>(),
        ] {
            let events = events.expect("comment emission must not affect parsing");
            assert_eq!(
                events
                    .iter()
                    .filter(|(event, _)| support::is_comment_event(event))
                    .count(),
                if expect_comments { 2 } else { 0 },
            );
            assert!(events.iter().any(
                |(event, _)| matches!(event, Event::Scalar { value, .. } if value == "value")
            ));
        }

        for tokens in [
            Scanner::with_options(StrInput::new(yaml), options.clone())
                .collect::<Result<Vec<_>, _>>(),
            Scanner::with_options(BufferedInput::new(yaml.chars()), setter_options.clone())
                .collect::<Result<Vec<_>, _>>(),
            Scanner::with_options(
                FallibleBufferedInput::new(yaml.chars().map(Ok)),
                setter_options.clone(),
            )
            .collect::<Result<Vec<_>, _>>(),
        ] {
            let tokens = tokens.expect("comment emission must not affect scanning");
            assert_eq!(
                tokens
                    .iter()
                    .filter(|token| support::is_comment_token(token.token_type()))
                    .count(),
                if expect_comments { 2 } else { 0 },
            );
        }
    }
}

#[test]
fn new_uses_default_options() {
    let yaml = "root: [a, {b: c}]\n";
    let from_new: Vec<_> = Parser::new(StrInput::new(yaml)).collect();
    let from_options: Vec<_> =
        Parser::with_options(StrInput::new(yaml), Options::default()).collect();

    assert_eq!(from_new, from_options);
}

#[test]
fn common_input_constructors_accept_options() {
    let yaml = String::from("# ignored\nvalue\n");
    let options = granit_parser::options! {
        emit_comments: false,
    };

    let string_events = Parser::new_from_str_with_options(&yaml, options.clone())
        .collect::<Result<Vec<_>, _>>()
        .expect("string constructor should parse with custom options");
    assert_comment_free_value(&string_events);

    let iterator_events = Parser::new_from_iter_with_options(yaml.chars(), options.clone())
        .collect::<Result<Vec<_>, _>>()
        .expect("iterator constructor should parse with custom options");
    assert_comment_free_value(&iterator_events);

    let fallible_events =
        Parser::new_from_fallible_iter_with_options(yaml.chars().map(Ok), options)
            .collect::<Result<Vec<_>, _>>()
            .expect("fallible iterator constructor should parse with custom options");
    assert_comment_free_value(&fallible_events);
}

#[test]
#[cfg(feature = "comments")]
fn buffered_comment_limit_can_be_raised() {
    let default_limit = Options::default().max_buffered_comment_events;
    let raised_limit = default_limit + 1;
    let mut yaml = String::from("key: # c0\n");
    for index in 1..=default_limit {
        yaml.push_str("# c");
        yaml.push_str(&index.to_string());
        yaml.push('\n');
    }
    yaml.push_str("next: value\n");

    let error = Parser::new(StrInput::new(&yaml))
        .find_map(Result::err)
        .expect("default options should reject the comment above their limit");
    assert_eq!(error.kind(), &ErrorKind::TooManyComments);

    let options = granit_parser::options! {
        max_buffered_comment_events: raised_limit,
    };
    let events = Parser::with_options(StrInput::new(&yaml), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("custom options should accept the raised buffered-comment limit");

    assert_eq!(
        events
            .iter()
            .filter(|(event, _)| support::is_comment_event(event))
            .count(),
        raised_limit
    );
}

#[test]
#[cfg(feature = "comments")]
fn buffered_comment_limit_honors_lower_and_zero_boundaries() {
    let one_comment = "key: # one\nnext: value\n";
    let options = granit_parser::options! {
        max_buffered_comment_events: 1,
    };
    Parser::with_options(StrInput::new(one_comment), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("one buffered comment should be accepted at limit one");

    let two_comments = "key: # one\n# two\nnext: value\n";
    let options = granit_parser::options! {
        max_buffered_comment_events: 1,
    };
    let error = Parser::with_options(StrInput::new(two_comments), options)
        .find_map(Result::err)
        .expect("the second buffered comment should exceed limit one");
    assert_eq!(error.kind(), &ErrorKind::TooManyComments);

    let options = granit_parser::options! {
        max_buffered_comment_events: 0,
    };
    let error = Parser::with_options(StrInput::new(one_comment), options)
        .find_map(Result::err)
        .expect("the first buffered comment should exceed limit zero");
    assert_eq!(error.kind(), &ErrorKind::TooManyComments);
}

#[test]
#[cfg(feature = "comments")]
fn zero_comment_limit_still_precedes_indentless_sequence_start() {
    let options = granit_parser::options! {
        max_buffered_comment_events: 0,
    };
    let mut events = Vec::new();
    let error = Parser::with_options(StrInput::new("a:\n- # one\n"), options)
        .find_map(|result| match result {
            Ok((event, _)) => {
                events.push(event);
                None
            }
            Err(error) => Some(error),
        })
        .expect("the first buffered comment should exceed limit zero");

    assert_eq!(error.kind(), &ErrorKind::TooManyComments);
    assert!(
        events
            .iter()
            .all(|event| !matches!(event, Event::SequenceStart { .. })),
        "the resource-limit error must not be deferred behind the sequence start"
    );
}

#[test]
fn simple_key_lookahead_can_be_raised() {
    let key = "k".repeat(1025);
    let yaml = format!("a: b\n{key}: value\n");

    let error = Parser::new(StrInput::new(&yaml))
        .find_map(Result::err)
        .expect("default options should reject a 1025-character simple key");
    assert_eq!(error.kind(), &ErrorKind::SimpleKeyExpected);

    let options = granit_parser::options! {
        simple_key_max_lookahead: 1025,
    };
    Parser::with_options(StrInput::new(&yaml), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("custom options should accept a 1025-character simple key");
}

#[test]
fn simple_key_lookahead_honors_configured_boundary() {
    let yaml = "a: b\nlong: value\n";
    let options = granit_parser::options! {
        simple_key_max_lookahead: 4,
    };
    Parser::with_options(StrInput::new(yaml), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("a simple key at the configured limit should be accepted");

    let options = granit_parser::options! {
        simple_key_max_lookahead: 3,
    };
    let error = Parser::with_options(StrInput::new(yaml), options)
        .find_map(Result::err)
        .expect("a simple key beyond the configured limit should be rejected");
    assert_eq!(error.kind(), &ErrorKind::SimpleKeyExpected);
}

#[test]
fn flow_nesting_limit_can_be_raised_above_previous_type_limit() {
    let yaml = format!("{}{}", "[".repeat(256), "]".repeat(256));

    let error = Parser::new(StrInput::new(&yaml))
        .find_map(Result::err)
        .expect("default options should reject flow nesting level 256");
    assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);

    let options = granit_parser::options! {
        flow_nesting_limit: 256,
    };
    let events = Parser::with_options(StrInput::new(&yaml), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("custom options should accept flow nesting level 256");

    assert_eq!(
        events
            .iter()
            .filter(|(event, _)| matches!(event, Event::SequenceStart { .. }))
            .count(),
        256
    );
}

#[test]
fn flow_nesting_limit_honors_lower_and_zero_boundaries() {
    let options = granit_parser::options! {
        flow_nesting_limit: 1,
    };
    Parser::with_options(StrInput::new("[]"), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("one flow collection should be accepted at limit one");

    let options = granit_parser::options! {
        flow_nesting_limit: 1,
    };
    let error = Parser::with_options(StrInput::new("[[]]"), options)
        .find_map(Result::err)
        .expect("the second flow collection should exceed limit one");
    assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);

    let options = granit_parser::options! {
        flow_nesting_limit: 0,
    };
    let error = Parser::with_options(StrInput::new("[]"), options)
        .find_map(Result::err)
        .expect("the first flow collection should exceed limit zero");
    assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);
}

#[test]
fn default_options_reject_excessive_compact_block_nesting() {
    let yaml = format!("{}value", "- ".repeat(256));
    let mut starts = 0;

    let error = Parser::new(StrInput::new(&yaml))
        .find_map(|result| match result {
            Ok((Event::SequenceStart { .. }, _)) => {
                starts += 1;
                None
            }
            Ok(_) => None,
            Err(error) => Some(error),
        })
        .expect("default options should reject block nesting level 256");

    assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);
    assert_eq!(starts, 255, "the over-limit collection must not start");
}

#[test]
fn block_nesting_limit_can_be_raised() {
    let yaml = format!("{}value", "- ".repeat(256));
    let options = granit_parser::options! {
        block_nesting_limit: 256,
    };

    let events = Parser::with_options(StrInput::new(&yaml), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("custom options should accept block nesting level 256");

    assert_eq!(
        events
            .iter()
            .filter(|(event, _)| matches!(event, Event::SequenceStart { .. }))
            .count(),
        256
    );
}

#[test]
fn block_nesting_limit_honors_collection_and_zero_boundaries() {
    let options = granit_parser::options! {
        block_nesting_limit: 1,
    };
    Parser::with_options(StrInput::new("- value"), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("one block collection should be accepted at limit one");

    for yaml in ["root:\n- value\n", "root:\n  - value\n"] {
        let options = granit_parser::options! {
            block_nesting_limit: 1,
        };
        let error = Parser::with_options(StrInput::new(yaml), options)
            .find_map(Result::err)
            .expect("a nested block collection should exceed limit one");
        assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);
    }

    let options = granit_parser::options! {
        block_nesting_limit: 0,
    };
    let error = Parser::with_options(StrInput::new("key: value"), options)
        .find_map(Result::err)
        .expect("the first block collection should exceed limit zero");
    assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);
}

#[test]
fn scanner_enforces_block_nesting_limit_for_compact_collections() {
    for yaml in ["- - value", "? ? value", ": : value"] {
        let options = granit_parser::options! {
            block_nesting_limit: 1,
        };
        let mut starts = 0;
        let error = Scanner::with_options(StrInput::new(yaml), options)
            .find_map(|result| match result {
                Ok(token)
                    if matches!(
                        token.token_type(),
                        TokenType::BlockSequenceStart { .. } | TokenType::BlockMappingStart { .. }
                    ) =>
                {
                    starts += 1;
                    None
                }
                Ok(_) => None,
                Err(error) => Some(error),
            })
            .expect("the scanner should reject the second compact block collection");

        assert_eq!(error.kind(), &ErrorKind::RecursionLimitExceeded);
        assert_eq!(starts, 1, "the over-limit collection must not start");
    }
}

#[test]
fn block_nesting_limit_is_reused_after_collections_close() {
    let yaml = "---\n- first\n...\n---\nkey: value\n";
    let options = granit_parser::options! {
        block_nesting_limit: 1,
    };

    Parser::with_options(StrInput::new(yaml), options)
        .collect::<Result<Vec<_>, _>>()
        .expect("closed collections should release their nesting budget");
}