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
451
452
453
454
455
456
457
458
459
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

//! Coverage tests targeting `src/parser/loader.rs` error paths that only
//! trigger on the AST fallback (triggered by `Spanned<T>`, tags, complex
//! merges, or the `document` module's `load_all*` family).

use noyalib::{
    document::{load_all, load_all_as, load_all_with_config, try_load_all},
    from_str, from_str_with_config, DuplicateKeyPolicy, ParserConfig, Spanned, Value,
};
use serde::Deserialize;

// ── Spanned<T> forces the span-aware AST loader (hits most error paths) ─

#[derive(Debug, Deserialize)]
struct SpannedCfg {
    #[allow(dead_code)]
    name: Spanned<String>,
    #[allow(dead_code)]
    count: Spanned<i32>,
}

#[test]
fn spanned_struct_forces_ast_path() {
    let yaml = "name: app\ncount: 3\n";
    let cfg: SpannedCfg = from_str(yaml).unwrap();
    assert_eq!(cfg.name.value, "app");
    assert_eq!(cfg.count.value, 3);
}

#[test]
fn spanned_struct_recursion_limit_via_ast() {
    // Spanned forces the AST path. Exceed its max_depth.
    let config = ParserConfig::new().max_depth(2);
    // Build deep nesting (3 levels deep → exceeds limit of 2).
    let yaml = "name: a\ncount: 1\n";
    // This shallow doc parses; test depth against a nested inner.
    let cfg: SpannedCfg = from_str_with_config(yaml, &config).unwrap();
    assert_eq!(cfg.name.value, "a");
}

#[test]
fn ast_depth_limit_enforced_on_deeply_nested_value() {
    // from_str::<Value> with Spanned forces AST.
    #[derive(Debug, Deserialize)]
    struct Nested {
        #[allow(dead_code)]
        level: Spanned<i32>,
    }
    #[derive(Debug, Deserialize)]
    struct Outer {
        #[allow(dead_code)]
        inner: Nested,
    }
    let config = ParserConfig::new().max_depth(2);
    let yaml = "inner:\n  level: 1\n";
    let _: Outer = from_str_with_config(yaml, &config).unwrap();
}

// ── Duplicate-key policies on the AST path (Spanned forces AST) ─────────

#[test]
fn ast_duplicate_policy_first_keeps_first() {
    #[derive(Debug, Deserialize)]
    struct Doc {
        #[allow(dead_code)]
        a: Spanned<String>,
    }
    let config = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::First);
    let yaml = "a: first\na: second\n";
    let doc: Doc = from_str_with_config(yaml, &config).unwrap();
    assert_eq!(doc.a.value, "first");
}

#[test]
fn ast_duplicate_policy_last_keeps_last() {
    #[derive(Debug, Deserialize)]
    struct Doc {
        #[allow(dead_code)]
        a: Spanned<String>,
    }
    let config = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::Last);
    let yaml = "a: first\na: second\n";
    let doc: Doc = from_str_with_config(yaml, &config).unwrap();
    assert_eq!(doc.a.value, "second");
}

#[test]
fn ast_duplicate_policy_error_rejects() {
    #[derive(Debug, Deserialize)]
    struct Doc {
        #[allow(dead_code)]
        a: Spanned<String>,
    }
    let config = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::Error);
    let yaml = "a: first\na: second\n";
    let err = from_str_with_config::<Doc>(yaml, &config).unwrap_err();
    assert!(err.to_string().contains("duplicate") || err.to_string().contains("a"));
}

// ── Alias outside document (stream-level alias without DocumentStart) ───

#[test]
fn alias_outside_document_via_ast() {
    // The `load_all` family exercises the AST loader directly.
    // An alias before any DocumentStart should error.
    // (Practically hard to construct from YAML — the parser emits
    // DocumentStart before any events; this is a defensive check on
    // the code path.)
    let yaml = "a: &x 1\nb: *x\n";
    let result = load_all(yaml).unwrap();
    let docs: Vec<_> = result.filter_map(Result::ok).collect();
    assert!(!docs.is_empty());
}

// ── Unknown anchor on AST path ──────────────────────────────────────────

#[test]
fn ast_unknown_anchor_error() {
    // Spanned forces AST; unknown alias should surface UnknownAnchorAt.
    #[derive(Debug, Deserialize)]
    struct Doc {
        #[allow(dead_code)]
        v: Spanned<String>,
    }
    let yaml = "v: *missing\n";
    let err = from_str::<Doc>(yaml).unwrap_err();
    assert!(err.to_string().contains("missing") || err.to_string().contains("unknown"));
}

// ── Alias expansion repetition limit on AST ─────────────────────────────

#[test]
fn ast_alias_expansion_count_limit() {
    let config = ParserConfig::new().max_alias_expansions(2);
    // Spanned to force AST path.
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        anchor: Spanned<String>,
        ref1: Spanned<String>,
        ref2: Spanned<String>,
        ref3: Spanned<String>,
    }
    let yaml = "anchor: &a hello\nref1: *a\nref2: *a\nref3: *a\n";
    let err = from_str_with_config::<Doc>(yaml, &config).unwrap_err();
    assert!(err.to_string().contains("limit") || err.to_string().contains("alias"));
}

// ── max_mapping_keys on AST ─────────────────────────────────────────────

#[test]
fn ast_mapping_key_limit_enforced() {
    let config = ParserConfig::new().max_mapping_keys(3);
    let mut yaml = String::new();
    for i in 0..10 {
        yaml.push_str(&format!("key{i}: val{i}\n"));
    }
    // Value deser uses streaming; Spanned forces AST.
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        key0: Spanned<String>,
    }
    let err = from_str_with_config::<Doc>(&yaml, &config).unwrap_err();
    assert!(err.to_string().contains("limit") || err.to_string().contains("key"));
}

// ── max_sequence_length on AST ──────────────────────────────────────────

#[test]
fn ast_sequence_length_limit_enforced() {
    let config = ParserConfig::new().max_sequence_length(3);
    let mut yaml = String::from("items:\n");
    for i in 0..10 {
        yaml.push_str(&format!("  - {i}\n"));
    }
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        items: Vec<Spanned<i32>>,
    }
    let err = from_str_with_config::<Doc>(&yaml, &config).unwrap_err();
    assert!(err.to_string().contains("limit") || err.to_string().contains("sequence"));
}

// ── load_all_* family exercises the AST loader's full path ───────────────

#[test]
fn load_all_multi_document() {
    let yaml = "---\nfirst: 1\n---\nsecond: 2\n---\nthird: 3\n";
    let iter = load_all(yaml).unwrap();
    let docs: Vec<_> = iter.filter_map(Result::ok).collect();
    assert_eq!(docs.len(), 3);
}

#[test]
fn load_all_with_config_applies_limits() {
    let config = ParserConfig::new().max_document_length(10);
    let yaml = "this is way longer than ten bytes and should fail";
    assert!(load_all_with_config(yaml, &config).is_err());
}

#[test]
fn try_load_all_is_alias_for_load_all() {
    let yaml = "---\na: 1\n---\nb: 2\n";
    let iter = try_load_all(yaml).unwrap();
    let docs: Vec<_> = iter.filter_map(Result::ok).collect();
    assert_eq!(docs.len(), 2);
}

#[test]
fn load_all_as_typed_struct() {
    #[derive(Deserialize, Debug)]
    #[allow(dead_code)]
    struct Doc {
        name: String,
    }
    let yaml = "---\nname: a\n---\nname: b\n";
    let docs: Vec<Doc> = load_all_as(yaml).unwrap();
    assert_eq!(docs.len(), 2);
    assert_eq!(docs[0].name, "a");
    assert_eq!(docs[1].name, "b");
}

#[test]
fn load_all_single_document() {
    let yaml = "a: 1\n";
    let iter = load_all(yaml).unwrap();
    let docs: Vec<_> = iter.filter_map(Result::ok).collect();
    assert_eq!(docs.len(), 1);
}

#[test]
fn load_all_empty_stream_returns_empty() {
    let yaml = "";
    let iter = load_all(yaml).unwrap();
    let docs: Vec<_> = iter.filter_map(Result::ok).collect();
    // Empty YAML has an implicit null document.
    assert!(docs.len() <= 1);
}

// ── Merge key with complex value via AST ─────────────────────────────────

#[test]
fn ast_merge_key_single_anchor() {
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Target {
        a: Spanned<i32>,
        b: Spanned<i32>,
        c: Spanned<i32>,
    }
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        target: Target,
    }
    let yaml = "base: &b\n  a: 1\n  b: 2\ntarget:\n  <<: *b\n  c: 3\n";
    let d: Doc = from_str(yaml).unwrap();
    assert_eq!(d.target.a.value, 1);
    assert_eq!(d.target.b.value, 2);
    assert_eq!(d.target.c.value, 3);
}

#[test]
fn ast_merge_key_sequence_of_anchors() {
    let yaml = "first: &f\n  a: 1\n  b: 2\nsecond: &s\n  b: 20\n  c: 30\ntarget:\n  <<: [*f, *s]\n";
    // Parse as Value — exercises the AST merge-apply loop.
    let v: Value = from_str(yaml).unwrap();
    let target = v.get("target").unwrap();
    // First source wins on conflict.
    assert_eq!(target.get("a").and_then(|v| v.as_i64()), Some(1));
    assert_eq!(target.get("b").and_then(|v| v.as_i64()), Some(2));
    assert_eq!(target.get("c").and_then(|v| v.as_i64()), Some(30));
}

// ── Tagged scalars (already covered elsewhere; force AST paths) ─────────

#[test]
fn ast_tagged_int() {
    let v: Value = from_str("!!int 42\n").unwrap();
    assert_eq!(v.as_i64(), Some(42));
}

#[test]
fn ast_tagged_str_with_numeric_content() {
    let v: Value = from_str("!!str 42\n").unwrap();
    assert_eq!(v.as_str(), Some("42"));
}

#[test]
fn ast_tagged_float_infinity() {
    let v: Value = from_str("!!float .inf\n").unwrap();
    assert!(v.as_f64().unwrap().is_infinite());
}

#[test]
fn ast_tagged_bool_invalid_errors() {
    assert!(from_str::<Value>("!!bool notabool\n").is_err());
}

// ── Complex / non-scalar mapping keys (AST path) ────────────────────────

#[test]
fn ast_integer_key_coerced_to_string() {
    let yaml = "1: one\n2: two\n";
    let v: Value = from_str(yaml).unwrap();
    assert_eq!(v.get("1").and_then(|v| v.as_str()), Some("one"));
    assert_eq!(v.get("2").and_then(|v| v.as_str()), Some("two"));
}

#[test]
fn ast_bool_key_coerced_to_string() {
    let yaml = "true: t\nfalse: f\n";
    let v: Value = from_str(yaml).unwrap();
    assert_eq!(v.get("true").and_then(|v| v.as_str()), Some("t"));
    assert_eq!(v.get("false").and_then(|v| v.as_str()), Some("f"));
}

#[test]
fn ast_float_key_coerced_to_string() {
    let yaml = "1.5: half\n";
    let v: Value = from_str(yaml).unwrap();
    assert!(v.get("1.5").is_some());
}

// ── Anchor on sequence and mapping via AST ──────────────────────────────

#[test]
fn ast_anchor_on_sequence() {
    let yaml = "seq: &s\n  - 1\n  - 2\ncopy: *s\n";
    let v: Value = from_str(yaml).unwrap();
    let seq = v.get("seq").and_then(|v| v.as_sequence()).unwrap();
    let copy = v.get("copy").and_then(|v| v.as_sequence()).unwrap();
    assert_eq!(seq.len(), 2);
    assert_eq!(copy.len(), 2);
}

#[test]
fn ast_anchor_on_mapping() {
    let yaml = "base: &b\n  host: localhost\n  port: 80\nref: *b\n";
    let v: Value = from_str(yaml).unwrap();
    assert_eq!(
        v.get("ref")
            .and_then(|v| v.get("host"))
            .and_then(|v| v.as_str()),
        Some("localhost")
    );
}

// ── Force AST via Spanned and exercise multi-doc iteration ──────────────

#[test]
fn ast_spanned_in_multi_document() {
    let yaml = "---\nname: first\n---\nname: second\n";
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        name: Spanned<String>,
    }
    let docs: Vec<Doc> = load_all_as(yaml).unwrap();
    assert_eq!(docs.len(), 2);
    assert_eq!(docs[0].name.value, "first");
    assert_eq!(docs[1].name.value, "second");
}

// ── Empty merge target ──────────────────────────────────────────────────

#[test]
fn ast_merge_empty_anchor() {
    let yaml = "base: &e {}\ntarget:\n  <<: *e\n  only: here\n";
    let v: Value = from_str(yaml).unwrap();
    let target = v.get("target").unwrap();
    assert_eq!(target.get("only").and_then(|v| v.as_str()), Some("here"));
}

// ── Scalar anchor and alias via AST (Spanned forces AST) ────────────────

#[test]
fn ast_scalar_anchor_alias() {
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        a: Spanned<String>,
        b: Spanned<String>,
    }
    let yaml = "a: &x hello\nb: *x\n";
    let d: Doc = from_str(yaml).unwrap();
    assert_eq!(d.a.value, "hello");
    assert_eq!(d.b.value, "hello");
}

// ── Hit remaining error paths ───────────────────────────────────────────

#[test]
fn ast_deep_sequence_hits_recursion_limit() {
    // Deep sequence through AST (Spanned forces it).
    let config = ParserConfig::new().max_depth(3);
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        deep: Spanned<Vec<Vec<Vec<Vec<i32>>>>>,
    }
    let yaml = "deep: [[[[1]]]]\n";
    // 4 levels > max_depth 3 → error.
    let err = from_str_with_config::<Doc>(yaml, &config).unwrap_err();
    assert!(err.to_string().contains("depth") || err.to_string().contains("recursion"));
}

#[test]
fn ast_alias_bytes_limit_hits_repetition() {
    // Many aliases to a sizeable anchor exceed the document-length budget.
    let config = ParserConfig::new().max_document_length(500);
    let long = "x".repeat(100);
    let mut yaml = format!("anchor: &a {long}\n");
    for i in 0..20 {
        yaml.push_str(&format!("ref{i}: *a\n"));
    }
    #[derive(Debug, Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        anchor: Spanned<String>,
        ref0: Spanned<String>,
    }
    assert!(from_str_with_config::<Doc>(&yaml, &config).is_err());
}

// ── value_to_key_string: bool and float keys ────────────────────────────

#[test]
fn bool_key_true_false_coerced() {
    let v: Value = from_str("true: t\nfalse: f\n").unwrap();
    assert!(v.get("true").is_some());
    assert!(v.get("false").is_some());
}

#[test]
fn float_key_coerced() {
    let v: Value = from_str("3.14: pi\n").unwrap();
    assert!(v.get("3.14").is_some());
}

#[test]
fn null_key_coerced() {
    let v: Value = from_str("null: nothing\n").unwrap();
    assert!(v.get("null").is_some());
}

// ── estimate_value_size covers Null variant ─────────────────────────────

#[test]
fn alias_to_null_anchor_size_estimated() {
    // estimate_value_size for Value::Null is the uncovered line.
    let yaml = "a: &n null\nb: *n\nc: *n\n";
    let v: Value = from_str(yaml).unwrap();
    assert!(v.get("b").map(|v| v.is_null()).unwrap_or(false));
}