rlsp-yaml-parser 0.11.1

Spec-faithful streaming YAML 1.2 parser
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
// SPDX-License-Identifier: MIT
//
// Duplicate key and error reporting tests — verifies parser behavior for
// duplicate keys (accepted silently), error detection, error positions,
// error recovery (stream stops after first error), and merge key handling.

#![expect(
    clippy::unwrap_used,
    clippy::indexing_slicing,
    missing_docs,
    reason = "test code"
)]

use rstest::rstest;

use rlsp_yaml_parser::loader::LoadError;
use rlsp_yaml_parser::node::Node;
use rlsp_yaml_parser::{Error, ErrorKind, Event, load, parse_events};

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Returns the first parse error from `parse_events`, if any.
fn first_error(input: &str) -> Option<Error> {
    parse_events(input).find_map(Result::err)
}

/// Load input and extract scalar key-value pairs from the root mapping.
fn mapping_key_values(input: &str) -> Vec<(String, String)> {
    let docs = load(input).unwrap();
    match &docs[0].root {
        Node::Mapping { entries, .. } => entries
            .iter()
            .filter_map(|(k, v)| {
                let key = match k {
                    Node::Scalar { value, .. } => value.clone(),
                    Node::Mapping { .. } | Node::Sequence { .. } | Node::Alias { .. } => {
                        return None;
                    }
                };
                let val = match v {
                    Node::Scalar { value, .. } => value.clone(),
                    Node::Mapping { .. } | Node::Sequence { .. } | Node::Alias { .. } => {
                        return None;
                    }
                };
                Some((key, val))
            })
            .collect(),
        Node::Scalar { .. } | Node::Sequence { .. } | Node::Alias { .. } => vec![],
    }
}

// ===========================================================================
// Spike
// ===========================================================================

#[test]
fn duplicate_keys_both_entries_present_in_mapping() {
    let input = "key: first\nkey: second\n";
    let result = load(input);
    assert!(result.is_ok(), "load failed: {result:?}");
    let kvs = mapping_key_values(input);
    assert_eq!(kvs.len(), 2);
    assert!(kvs.contains(&("key".into(), "first".into())));
    assert!(kvs.contains(&("key".into(), "second".into())));
}

// ===========================================================================
// Group A: Duplicate keys — two entries present (uniform shape)
//
// input → mapping_key_values → assert len==2 + contains both pairs
// ===========================================================================

#[rstest]
#[case::plain_scalar("name: Alice\nname: Bob\n", ("name", "Alice"), ("name", "Bob"))]
#[case::flow_mapping("{a: 1, a: 2}\n", ("a", "1"), ("a", "2"))]
fn duplicate_keys_two_entries(
    #[case] input: &str,
    #[case] pair_a: (&str, &str),
    #[case] pair_b: (&str, &str),
) {
    let kvs = mapping_key_values(input);
    assert_eq!(kvs.len(), 2);
    assert!(kvs.contains(&(pair_a.0.into(), pair_a.1.into())));
    assert!(kvs.contains(&(pair_b.0.into(), pair_b.1.into())));
}

#[test]
fn duplicate_quoted_and_unquoted_keys_both_entries_present() {
    let input = "key: unquoted\n'key': single-quoted\n\"key\": double-quoted\n";
    let kvs = mapping_key_values(input);
    assert_eq!(kvs.len(), 3);
    assert!(kvs.iter().any(|(_, v)| v == "unquoted"));
    assert!(kvs.iter().any(|(_, v)| v == "single-quoted"));
    assert!(kvs.iter().any(|(_, v)| v == "double-quoted"));
}

#[test]
fn duplicate_keys_in_nested_mappings_are_independent() {
    let input = "outer:\n  key: inner_value\nkey: outer_value\n";
    let docs = load(input).unwrap();
    match &docs[0].root {
        Node::Mapping { entries, .. } => {
            assert_eq!(entries.len(), 2);
            // Find the "outer" entry and verify its nested mapping.
            let outer_entry = entries
                .iter()
                .find(|(k, _)| matches!(k, Node::Scalar { value, .. } if value == "outer"))
                .expect("expected 'outer' key");
            match &outer_entry.1 {
                Node::Mapping { entries: inner, .. } => {
                    assert_eq!(inner.len(), 1);
                    assert!(matches!(&inner[0].0, Node::Scalar { value, .. } if value == "key"));
                    assert!(matches!(
                        &inner[0].1,
                        Node::Scalar { value, .. } if value == "inner_value"
                    ));
                }
                other @ (Node::Scalar { .. } | Node::Sequence { .. } | Node::Alias { .. }) => {
                    panic!("expected nested mapping, got: {other:?}")
                }
            }
            // Find the root "key" entry.
            let root_key = entries
                .iter()
                .find(|(k, _)| matches!(k, Node::Scalar { value, .. } if value == "key"))
                .expect("expected root 'key' entry");
            assert!(matches!(
                &root_key.1,
                Node::Scalar { value, .. } if value == "outer_value"
            ));
        }
        other @ (Node::Scalar { .. } | Node::Sequence { .. } | Node::Alias { .. }) => {
            panic!("expected root mapping, got: {other:?}")
        }
    }
}

#[test]
fn duplicate_keys_with_numeric_and_string_representations_both_present() {
    // 1 and '1' both parse as string value "1" — the parser does not
    // type-convert keys. Both entries should be present.
    let input = "1: numeric_key\n'1': quoted_key\n";
    let kvs = mapping_key_values(input);
    assert_eq!(kvs.len(), 2);
    assert!(kvs.iter().any(|(_, v)| v == "numeric_key"));
    assert!(kvs.iter().any(|(_, v)| v == "quoted_key"));
}

// ===========================================================================
// Group B: Error detection — unterminated quoted scalars (uniform shape)
//
// input → first_error().is_some() + load() is Err(LoadError::Parse)
// ===========================================================================

#[rstest]
#[case::single_quoted("key: 'unterminated\n")]
#[case::double_quoted("key: \"unterminated\n")]
fn unterminated_quoted_scalar_produces_parse_error(#[case] input: &str) {
    assert!(first_error(input).is_some(), "expected parse error");
    assert!(
        matches!(load(input), Err(LoadError::Parse { .. })),
        "expected LoadError::Parse"
    );
}

// ===========================================================================
// Group C: Error detection — unterminated flow collections (uniform shape)
//
// input → first_error().is_some()
// ===========================================================================

#[rstest]
#[case::sequence("[a, b, c\n")]
#[case::mapping("{a: 1, b: 2\n")]
fn unterminated_flow_collection_produces_error(#[case] input: &str) {
    assert!(
        first_error(input).is_some(),
        "expected parse error for unterminated flow collection"
    );
}

#[test]
fn load_returns_parse_error_for_invalid_input() {
    let input = "\t bad: indentation\n";
    let result = load(input);
    assert!(
        matches!(result, Err(LoadError::Parse { .. })),
        "expected LoadError::Parse, got: {result:?}"
    );
}

#[test]
fn parse_events_error_has_non_empty_message() {
    let input = "key: 'unterminated\n";
    let err = first_error(input).expect("expected parse error");
    assert!(!err.message.is_empty(), "error message should not be empty");
}

// ===========================================================================
// Group C: Error position — byte offset is meaningful
// ===========================================================================

#[test]
fn error_position_byte_offset_nonzero_for_error_after_content() {
    let input = "valid_key: value\n\t bad: indentation\n";
    let err = first_error(input).expect("expected parse error");
    assert!(
        err.pos.byte_offset > 0,
        "byte_offset should be > 0 for error after content, got: {:?}",
        err.pos
    );
}

#[test]
fn load_parse_error_carries_byte_offset() {
    let input = "a: 1\nb: 'unterminated\n";
    match load(input) {
        Err(LoadError::Parse { pos, .. }) => {
            assert!(
                pos.byte_offset > 4,
                "byte_offset should be past first line, got: {pos:?}"
            );
        }
        other => panic!("expected LoadError::Parse, got: {other:?}"),
    }
}

#[test]
fn error_position_at_stream_start_for_leading_invalid_input() {
    // Use an unterminated flow sequence starting at byte 0.
    let input = "[\n";
    let err = first_error(input).expect("expected parse error");
    // The error should be at or near the start of the stream.
    assert!(
        err.pos.byte_offset <= 2,
        "expected byte_offset near start, got: {:?}",
        err.pos
    );
}

// ===========================================================================
// Group D: Error stops the stream
// ===========================================================================

#[test]
fn parse_events_stops_after_first_error() {
    let input = "key: 'unterminated\n";
    let items: Vec<_> = parse_events(input).collect();
    let err_count = items.iter().filter(|r| r.is_err()).count();
    assert_eq!(err_count, 1, "expected exactly one error, got: {err_count}");
    // The error should be the last item — no Ok items follow it.
    let last = items.last().expect("expected at least one item");
    assert!(last.is_err(), "last item should be the error");
}

#[test]
fn parse_events_emits_stream_start_before_error() {
    // Use unterminated quoted scalar — reliably produces an error.
    let input = "'unterminated\n";
    let items: Vec<_> = parse_events(input).collect();
    assert!(!items.is_empty(), "expected at least one event");
    assert!(
        matches!(&items[0], Ok((Event::StreamStart, _))),
        "first event should be StreamStart, got: {:?}",
        items[0]
    );
    assert!(
        items.iter().any(Result::is_err),
        "expected at least one error event"
    );
}

// ===========================================================================
// LE-1: LoadError::Parse Display includes a line number
// ===========================================================================

#[test]
fn load_error_parse_display_contains_line_info() {
    let input = "key: 'unterminated\n";
    let err = load(input).expect_err("expected LoadError::Parse");
    let display = format!("{err}");
    // The Display impl renders: "parse error at Pos { byte_offset: N, line: L, column: C }: …"
    // Assert that "line" appears (indicating position info is present) and that
    // a digit follows it (not just a bare word from the message text).
    assert!(
        display.contains("line"),
        "LoadError::Parse Display should contain 'line' (Pos info), got: {display:?}"
    );
    assert!(
        display.chars().any(|c| c.is_ascii_digit()),
        "LoadError::Parse Display should contain a digit after 'line', got: {display:?}"
    );
}

// ===========================================================================
// Group E: Merge key error
// ===========================================================================

#[test]
fn invalid_merge_key_value_is_accepted_or_errors_gracefully() {
    // The YAML spec says `<<` merge keys must reference mappings or sequences
    // of mappings. This parser does not enforce that constraint — `<<` is
    // treated as a regular scalar key. Assert actual behavior.
    let input = "<<: scalar_value\n";
    let result = load(input);
    // Either outcome is acceptable — the test documents the behavior.
    if let Ok(docs) = &result {
        assert_eq!(docs.len(), 1, "expected 1 document");
    }
    // If Err, the parser rejected merge key with scalar value — also acceptable.
}

// ===========================================================================
// Group F: ErrorKind — kind field is set correctly on parse errors
// ===========================================================================

#[test]
fn error_kind_for_non_printable_in_plain_scalar_is_invalid_character() {
    // U+0001 (SOH) is a non-printable, non-c-printable character forbidden in
    // plain scalars. Inserting it as the second byte of the content triggers
    // an InvalidCharacter error.
    let input = "key: val\u{0001}ue\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in plain scalar, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_unterminated_single_quoted_scalar_is_syntax() {
    let input = "key: 'unterminated\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::Syntax,
        "expected Syntax for unterminated single-quoted scalar, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_unterminated_double_quoted_scalar_is_syntax() {
    let input = "key: \"unterminated\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::Syntax,
        "expected Syntax for unterminated double-quoted scalar, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_non_printable_in_double_quoted_scalar_is_invalid_character() {
    // U+0001 inside a double-quoted scalar (not as a \x01 escape, but literal).
    let input = "key: \"val\u{0001}ue\"\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in double-quoted scalar, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_non_printable_in_single_quoted_scalar_is_invalid_character() {
    // U+0001 inside a single-quoted scalar (literal byte).
    let input = "key: 'val\u{0001}ue'\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in single-quoted scalar, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_non_printable_in_block_scalar_is_invalid_character() {
    // U+0001 inside a literal block scalar body.
    let input = "key: |\n  val\u{0001}ue\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in block scalar, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_duplicate_yaml_directive_is_syntax() {
    // Two %YAML directives in one document is a structural/grammar error.
    let input = "%YAML 1.2\n%YAML 1.2\n---\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::Syntax,
        "expected Syntax for duplicate YAML directive, got: {:?}",
        err.kind
    );
}

#[test]
fn error_kind_for_non_printable_in_directive_name_is_invalid_character() {
    // U+0001 in a directive parameter triggers an InvalidCharacter error.
    // %TAG directives with a non-printable in the handle/prefix are rejected.
    let input = "%TAG !\u{0001} tag:example.com,2024:\n---\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in directive parameter, got: {:?}",
        err.kind
    );
}

#[test]
fn parse_events_comment_non_printable_produces_invalid_character_kind() {
    // SOH (U+0001) in a comment body — hits lexer/comment.rs non_printable_error_message path.
    let input = "# hello\x01world\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in comment, got: {:?}",
        err.kind
    );
}

#[test]
fn parse_events_quoted_scalar_escape_non_printable_produces_invalid_character_kind() {
    // \x01 escape sequence in a double-quoted scalar decodes to U+0001, which is
    // non-printable — hits decode_and_push_escape at lexer/quoted.rs:703.
    let input = "key: \"\\x01\"\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for \\x01 escape producing non-printable, got: {:?}",
        err.kind
    );
}

#[test]
fn parse_events_directive_parameter_non_printable_produces_invalid_character_kind() {
    // Non-printable in a reserved directive's parameter — hits directives.rs line 123
    // loop for reserved directives.
    let input = "%FOO bar\x01baz\n---\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in reserved directive parameter, got: {:?}",
        err.kind
    );
}

#[test]
fn parse_events_yaml_directive_parameter_non_printable_produces_invalid_character_kind() {
    // Non-printable in a %YAML directive parameter — hits directives.rs line 166
    // pre-validate loop for %YAML directives.
    let input = "%YAML 1\x01.2\n---\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::InvalidCharacter,
        "expected InvalidCharacter for non-printable in %YAML directive parameter, got: {:?}",
        err.kind
    );
}

#[test]
fn parse_events_unterminated_flow_sequence_produces_syntax_kind() {
    // Unterminated flow sequence — structural/grammar error, not a character violation.
    let input = "[a, b, c\n";
    let err = first_error(input).expect("expected parse error");
    assert_eq!(
        err.kind,
        ErrorKind::Syntax,
        "expected Syntax for unterminated flow sequence, got: {:?}",
        err.kind
    );
}

// ===========================================================================
// Group G: LoadError::Parse carries kind — load() API propagates ErrorKind
// ===========================================================================

#[test]
fn load_parse_error_carries_kind_invalid_character_for_non_printable_in_comment() {
    // U+0080 (PAD, a C1 control character) in a comment body is non-printable
    // and forbidden by YAML 1.2.2 c-printable. Feeding it through load()
    // verifies that LoadError::Parse.kind is forwarded from the event-stream Error.
    let input = "key: value # comment\u{0080}here\n";
    match load(input) {
        Err(LoadError::Parse { kind, .. }) => {
            assert_eq!(
                kind,
                ErrorKind::InvalidCharacter,
                "LoadError::Parse.kind should be InvalidCharacter for U+0080 in comment, got: {kind:?}"
            );
        }
        other => panic!("expected Err(LoadError::Parse), got: {other:?}"),
    }
}