noyalib 0.0.2

A pure Rust YAML library with zero unsafe code and full serde integration
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
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

// YAML spec: Error cases — invalid YAML that should be rejected

use std::collections::HashMap;

use noyalib::{from_str, Value};

#[test]
fn invalid_indentation() {
    let result: Result<Value, _> = from_str("a: 1\n b: 2\n");
    // This may parse differently than expected — at minimum should not panic
    let _ = result;
}

#[test]
fn unclosed_flow_sequence() {
    let result: Result<Vec<i64>, _> = from_str("[1, 2, 3");
    assert!(result.is_err());
}

#[test]
fn unclosed_flow_mapping() {
    let result: Result<HashMap<String, i64>, _> = from_str("{a: 1, b: 2");
    assert!(result.is_err());
}

#[test]
fn tab_as_indentation() {
    // YAML spec forbids tabs for indentation
    let result: Result<Value, _> = from_str("a:\n\tb: 1\n");
    assert!(result.is_err());
}

#[test]
fn type_mismatch_string_as_int() {
    let result: Result<i64, _> = from_str("hello");
    assert!(result.is_err());
}

#[test]
fn type_mismatch_mapping_as_seq() {
    let result: Result<Vec<String>, _> = from_str("a: 1\nb: 2\n");
    assert!(result.is_err());
}

#[test]
fn type_mismatch_seq_as_mapping() {
    let result: Result<HashMap<String, String>, _> = from_str("- a\n- b\n");
    assert!(result.is_err());
}

#[test]
fn empty_yaml_is_error() {
    let result: Result<i64, _> = from_str("");
    assert!(result.is_err());
}

#[test]
fn stray_scalar_after_mapping() {
    let result: Result<HashMap<String, String>, _> = from_str("foo: bar\ninvalid\n");
    // Should fail or produce unexpected results
    let _ = result;
}

#[test]
fn max_depth_exceeded() {
    use noyalib::{from_str_with_config, ParserConfig};

    // Create YAML that nests 10 levels deep, but set limit to 5
    let yaml = "a:\n  b:\n    c:\n      d:\n        e:\n          f: 1\n";
    let config = ParserConfig::new().max_depth(5);
    let result: Result<Value, _> = from_str_with_config(yaml, &config);
    assert!(result.is_err(), "should reject excessive nesting");
}

#[test]
fn max_document_length_exceeded() {
    use noyalib::{from_str_with_config, ParserConfig};

    let config = ParserConfig::new().max_document_length(10);
    let yaml = "this is more than 10 bytes";
    let result: Result<Value, _> = from_str_with_config(yaml, &config);
    assert!(result.is_err(), "should reject oversized document");
}

#[test]
fn missing_required_struct_field() {
    use serde::Deserialize;

    #[allow(dead_code)]
    #[derive(Debug, Deserialize)]
    struct Required {
        name: String,
        age: i64,
    }

    let result: Result<Required, _> = from_str("name: John\n");
    assert!(result.is_err());
}

#[test]
fn wrong_type_in_sequence() {
    let result: Result<Vec<i64>, _> = from_str("- 1\n- hello\n- 3\n");
    assert!(result.is_err());
}

#[test]
fn invalid_escape_in_double_quote() {
    let result: Result<String, _> = from_str("\"\\z\"");
    // Invalid escape — should error
    assert!(result.is_err());
}

#[test]
fn no_panic_on_any_input() {
    // Fuzz-like test: various malformed inputs should not panic
    let inputs = [
        "",
        "---",
        "...",
        "[",
        "{",
        "- - -",
        "!!",
        "&",
        "*",
        "---\n---",
        "key: [unclosed",
        "key: {unclosed",
        ":\n:",
        "- :\n  - :",
    ];

    for input in inputs {
        let _ = from_str::<Value>(input);
    }
}

// yaml-test-suite SU5Z — `#` adjacent to prior content is not a comment.
#[test]
fn comment_indicator_must_be_preceded_by_whitespace() {
    let result: Result<Value, _> = from_str("key: \"value\"# invalid comment\n");
    assert!(
        result.is_err(),
        "expected rejection of inline `#` without preceding whitespace"
    );
}

// yaml-test-suite X4QW — same rule inside the `>`/`|` header line.
#[test]
fn block_scalar_header_rejects_adjacent_hash() {
    let result: Result<Value, _> = from_str("block: >#comment\n  scalar\n");
    assert!(
        result.is_err(),
        "expected rejection of `>#` with no whitespace"
    );
}

// yaml-test-suite SF5V — at most one %YAML directive per document.
#[test]
fn duplicate_yaml_directive_rejected() {
    let result: Result<Value, _> = from_str("%YAML 1.2\n%YAML 1.2\n---\n");
    assert!(
        result.is_err(),
        "expected rejection of duplicate %YAML directive"
    );
}

// yaml-test-suite Y79Y :4..:7 — tab immediately before a block-structural
// indicator is forbidden (cannot stand in for indentation).
#[test]
fn tab_before_block_indicator_rejected() {
    for input in &["-\t-\n", "- \t-\n", "?\t-\n", "? -\n:\t-\n"] {
        let result: Result<Value, _> = from_str(input);
        assert!(
            result.is_err(),
            "expected rejection of tab-as-indentation in {input:?}"
        );
    }
}

// yaml-test-suite A2M4 (spec example 6.2) — tab as inline separation
// before plain content is *valid*; only tabs before another structural
// indicator are rejected.
#[test]
fn tab_as_inline_separation_accepted() {
    let v: Value = from_str("? a\n: -\tb\n  -  -\tc\n     - d\n").unwrap();
    let outer = v.as_mapping().expect("mapping");
    let seq = outer.get("a").expect("key 'a'").as_sequence().expect("seq");
    assert_eq!(seq[0].as_str(), Some("b"));
}

// yaml-test-suite 3HFZ — content after `...` document-end marker on the
// same line is invalid.
#[test]
fn document_end_marker_rejects_trailing_content() {
    let result: Result<Value, _> = from_str("---\nkey: value\n... invalid\n");
    assert!(result.is_err());
}

// yaml-test-suite RXY3 / 5TRB — a `---` or `...` document indicator at
// column 0 inside a multi-line quoted scalar is invalid (it would
// prematurely close the document).
#[test]
fn doc_marker_inside_quoted_scalar_rejected() {
    let r1: Result<Value, _> = from_str("---\n'\n...\n'\n");
    assert!(r1.is_err(), "single-quoted scalar containing `...`");
    let r2: Result<Value, _> = from_str("---\n\"\n---\n\"\n");
    assert!(r2.is_err(), "double-quoted scalar containing `---`");
}

// yaml-test-suite 2G84 — block scalar indent indicator must be a single
// digit 1..9; `0` and multi-digit forms are rejected.
#[test]
fn block_scalar_indent_indicator_validation() {
    let r1: Result<Value, _> = from_str("--- |0\n");
    assert!(r1.is_err(), "indent indicator 0 is invalid");
    let r2: Result<Value, _> = from_str("--- |10\n");
    assert!(r2.is_err(), "two-digit indent indicator is invalid");
}

// yaml-test-suite 4H7K — a stray `]` outside any flow sequence is an
// error.
#[test]
fn stray_flow_close_outside_flow_rejected() {
    let r1: Result<Value, _> = from_str("[ a, b, c ] ]\n");
    assert!(r1.is_err());
    let r2: Result<Value, _> = from_str("{ a: 1 } }\n");
    assert!(r2.is_err());
}

// yaml-test-suite H7TQ — non-numeric trailing content after the version
// of a `%YAML` directive is rejected. (Numeric continuations are
// accepted as a lenient extension; see ZYU8.)
#[test]
fn yaml_directive_rejects_non_numeric_extras() {
    let r: Result<Value, _> = from_str("%YAML 1.2 foo\n---\n");
    assert!(r.is_err());
    // Numeric-looking trailing token still parses.
    let _: Value = from_str("%YAML 1.1 1.2\n---\n").unwrap();
}

// yaml-test-suite 4HVU / EW3V / DMG6 / N4JP / U44R — block-context
// content at a column that does not match any open block scope
// (i.e. "between levels") is rejected. The check fires only at
// positions where a *new* mapping key or sequence entry could
// start, so separator tokens like `:` mid-pair are unaffected.
#[test]
fn between_levels_indentation_rejected() {
    // 4HVU — sequence entries at col 3 then a `-` at col 2.
    let r: Result<Value, _> = from_str("key:\n   - ok\n   - also ok\n  - wrong\n");
    assert!(r.is_err());

    // EW3V — second mapping key at col 1, parent at col 0.
    let r: Result<Value, _> = from_str("k1: v1\n k2: v2\n");
    assert!(r.is_err());

    // DMG6 — nested mapping then over-indented sibling.
    let r: Result<Value, _> = from_str("key:\n  ok: 1\n wrong: 2\n");
    assert!(r.is_err());
}

// Counter-examples: nested blocks (each level deeper) and sibling
// alignments (each at the same level) must continue to parse.
#[test]
fn correctly_indented_blocks_still_parse() {
    let _: Value = from_str("a:\n  b:\n    c: 1\n").unwrap();
    let _: Value = from_str("a: 1\nb: 2\nc: 3\n").unwrap();
    let _: Value = from_str("xs:\n  - 1\n  - 2\n  - 3\n").unwrap();
    let _: Value = from_str("# comment\nkey: value\n").unwrap();
}

// yaml-test-suite QB6E — continuation lines of a multi-line quoted
// scalar in block context must be indented more than the parent.
#[test]
fn quoted_scalar_continuation_must_be_indented() {
    // Continuation at col 0 inside an indented mapping → reject.
    let r: Result<Value, _> = from_str("---\nquoted: \"a\nb\nc\"\n");
    assert!(r.is_err());
    // Indented continuation parses.
    let _: Value = from_str("---\nquoted: \"a\n  b\n  c\"\n").unwrap();
}

// yaml-test-suite 7LBH / D49Q / G7JE — implicit (`?`-less) keys in
// block context must fit on a single line. Quoted, single-quoted,
// and plain-scalar variants all reject.
#[test]
fn multiline_implicit_key_rejected_in_block_context() {
    // 7LBH — double-quoted multi-line key.
    let r: Result<Value, _> = from_str("\"a\\nb\": 1\n\"c\n d\": 1\n");
    assert!(r.is_err());
    // D49Q — single-quoted multi-line key.
    let r: Result<Value, _> = from_str("'a\\nb': 1\n'c\n d': 1\n");
    assert!(r.is_err());
    // G7JE — plain multi-line key.
    let r: Result<Value, _> = from_str("a\\nb: 1\nc\n d: 1\n");
    assert!(r.is_err());
}

// yaml-test-suite 6M2F — `&b b\n: *a` is *valid*: the `:` on the next
// line is an empty implicit key indicator (a new pair), not a value
// separator for the anchored scalar above. The strict implicit-key
// check must distinguish this from a genuinely multi-line key.
#[test]
fn empty_implicit_key_after_anchored_value_parses() {
    let _: Value = from_str("? &a a\n: &b b\n: *a\n").unwrap();
}

// yaml-test-suite CXX2 / 9KBC — block-structural indicators (`:`,
// `?`, `-`) cannot open a collection on the same line as `---`. The
// `---` indicator may share a line only with a scalar or flow node.
#[test]
fn block_collection_inline_with_doc_start_rejected() {
    // CXX2 — anchor + key + `:` inline with `---`.
    let r: Result<Value, _> = from_str("--- &anchor a: b\n");
    assert!(r.is_err());
    // 9KBC — bare key + `:` inline with `---`.
    let r: Result<Value, _> = from_str("--- key1: value1\n    key2: value2\n");
    assert!(r.is_err());
    // Counter-examples: scalar / flow node inline with `---` is fine.
    let _: Value = from_str("--- text\n").unwrap();
    let _: Value = from_str("--- {a: 1}\n").unwrap();
}

// yaml-test-suite 9MMA / B63P — directives must be followed by an
// explicit `---` document indicator. A directive with no document is
// invalid.
#[test]
fn directive_without_document_rejected() {
    let r: Result<Value, _> = from_str("%YAML 1.2\n");
    assert!(r.is_err());
}

// yaml-test-suite RHX7 / 9HCY / MUS6:1 — directives must not appear
// between document content and the next `---` without an intervening
// `...` to close the previous document.
//
// EB22 (`---\nscalar1\n%YAML 1.2\n---\nscalar2`) is an adjacent
// case: per the spec, the parser would need lookahead to tell
// whether `%YAML` is plain-scalar continuation (XLQ9-style) or a
// directive announcing a new doc. We accept the lenient reading
// here for now.
#[test]
fn directive_without_doc_end_marker_rejected() {
    // RHX7 — second `%YAML` after a mapping with no `...`.
    let r: Result<Value, _> = from_str("---\nkey: value\n%YAML 1.2\n---\n");
    assert!(r.is_err());
    // 9HCY — implicit doc + `%TAG` without `...`.
    let r: Result<Value, _> =
        from_str("!foo \"bar\"\n%TAG ! tag:example.com,2000:app/\n---\n!foo \"bar\"\n");
    assert!(r.is_err());
    // Counter-example: directive after `...` is fine.
    let _: Vec<Value> =
        noyalib::load_all_as("---\nfoo: bar\n...\n%YAML 1.2\n---\nbaz: qux\n").unwrap();
}

// yaml-test-suite MUS6:0 — `%YAML 1.1#...` packs a comment indicator
// directly against the version digits with no whitespace separator.
#[test]
fn directive_comment_without_whitespace_rejected() {
    let r: Result<Value, _> = from_str("%YAML 1.1#...\n---\n");
    assert!(r.is_err());
    // Comment with whitespace is fine.
    let _: Value = from_str("%YAML 1.1 # ok\n---\nfoo: 1\n").unwrap();
}

// yaml-test-suite SR86 / SU74 — aliases are complete references, so
// they cannot be decorated with anchors (or tags). The check fires
// only on direct same-line adjacency; line-broken cases like
// `&node3\n  *alias1: scalar3` (26DV) where the anchor decorates
// an inner mapping are still valid.
#[test]
fn alias_decorated_by_anchor_rejected() {
    // SR86 — `&b *a` adjacency.
    let r: Result<Value, _> = from_str("key1: &a value\nkey2: &b *a\n");
    assert!(r.is_err());
    // Line-broken counter-pattern: an unknown alias might fail
    // resolution, but it must NOT fail the adjacency check — it
    // should reach the loader.
    let r: Result<Value, _> = from_str("top: &n3\n  *alias : scalar3\n");
    if let Err(e) = r {
        assert!(
            !e.to_string().contains("alias cannot be decorated"),
            "line-broken anchor → alias-key must not trigger the adjacency guard"
        );
    }
}

// yaml-test-suite LHL4 — `!invalid{}tag` packs flow indicators into a
// tag URI without separation. Tag URIs are followed by separation
// (whitespace / line break) before the next node.
#[test]
fn tag_followed_by_flow_indicator_rejected() {
    let r: Result<Value, _> = from_str("---\n!invalid{}tag scalar\n");
    assert!(r.is_err());
    // Counter-example: tag separated by whitespace from a flow node.
    let _: Value = from_str("---\n!foo {a: 1}\n").unwrap();
}

// yaml-test-suite BS4K / KS4U — content after the first document's
// root node, without `---` or `...` to mark a new document, is stray
// and rejected. Bare implicit doc 2 after `...` is fine (7Z25).
#[test]
fn stray_content_after_first_implicit_document_rejected() {
    // BS4K — comment terminates plain scalar; second scalar is stray.
    let r: Result<Value, _> = from_str("word1  # comment\nword2\n");
    assert!(r.is_err());
    // KS4U — content after closing `]` of the root flow seq.
    let r: Result<Value, _> = from_str("---\n[\nseq\n]\nstray\n");
    assert!(r.is_err());
    // 7Z25 — implicit doc 2 after explicit `...` is fine.
    let _: Vec<Value> = noyalib::load_all_as("---\nscalar1\n...\nkey: value\n").unwrap();
}

// yaml-test-suite 9C9N — flow content continuation across a line
// break must be indented more than the surrounding block; otherwise
// it would be ambiguous with sibling block content.
#[test]
fn flow_continuation_must_be_indented_more_than_parent() {
    // 9C9N — flow seq continues at col 0 inside an indented block.
    let r: Result<Value, _> = from_str("---\nflow: [a,\nb,\nc]\n");
    assert!(r.is_err());
    // Counter-examples: properly-indented continuation parses.
    let _: Value = from_str("---\nflow: [a,\n  b,\n  c]\n").unwrap();
    let _: Value = from_str("[\n  a,\n  b\n]\n").unwrap();
}

// yaml-test-suite 9KBC / CXX2 — `from_str` previously stopped lazily
// at the first complete value, silently swallowing the spec
// violations that follow. The streaming deserializer now drains
// trailing events, surfacing those errors instead of returning a
// partial value.
#[test]
fn from_str_drains_trailing_events_to_surface_errors() {
    // 9KBC — a mapping inlined onto the `---` line is invalid; the
    // continuation key on the next line triggers
    // "mapping values are not allowed in this context" once events
    // past the first scalar are fetched.
    let r: Result<Value, _> = from_str("--- key1: value1\n    key2: value2\n");
    assert!(
        r.is_err(),
        "expected from_str to surface the lazy-only-accept error"
    );

    // CXX2 — anchor + key on the document-start line.
    let r: Result<Value, _> = from_str("--- &anchor a: b\n");
    assert!(r.is_err());
}