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
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
// SPDX-License-Identifier: MIT OR Apache-2.0
// Copyright (c) 2026 Noyalib. All rights reserved.

//! Streaming-deserializer coverage push.
//!
//! Targets specific uncovered branches in `crates/noyalib/src/streaming.rs`
//! identified via `cargo llvm-cov` region/line analysis. Each test names the
//! code path it exercises so future drift maps cleanly back to the source
//! line range. The tests drive the streaming fast path either via
//! [`noyalib::from_str`] (for the typed-non-Value, default-config inputs
//! that the dispatcher routes through `streaming::from_str_streaming`) or
//! directly through [`noyalib::StreamingDeserializer`] when a finer-grained
//! invocation is required.

#![allow(missing_docs)]

use std::collections::BTreeMap;
use std::sync::Arc;

use noyalib::{from_str, from_str_with_config, ParserConfig, TagRegistry};
use serde::Deserialize;
use serde_bytes::ByteBuf;

// ── L170-L182 — peek_parser_event / next_parser_event parser-error paths ───
// A malformed merge value (`<<` followed by a non-alias non-sequence) routes
// through `peek_parser_event` directly, exercising the raw parser-event
// fetch path rather than the cached/replay path.

#[test]
fn coverage_stream_merge_value_non_alias_falls_back() {
    // `<<: { x: 1 }` is a mapping-typed merge value, which the streaming
    // path cannot handle and must surface via the AST fallback.
    let yaml = "\
target:
  <<:
    x: 1
  y: 2
";
    let m: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml).expect("ast fallback handles");
    assert_eq!(m["target"]["x"], 1);
    assert_eq!(m["target"]["y"], 2);
}

// ── L284 — buffered_to_event for a recorded BufferedEvent::Alias ───────────
// An anchored mapping that contains an alias to a previously defined anchor
// causes the recording machinery to emit a `BufferedEvent::Alias`, which is
// then replayed via `buffered_to_event`.

#[test]
fn coverage_stream_anchor_with_inner_alias_replay() {
    // Alias-in-anchor replay where the inner alias is to a scalar — the
    // streaming path records `BufferedEvent::Alias` and surfaces the
    // re-resolution. AST fallback handles the typed mapping.
    let yaml = "\
shared: &shared 42
container: &c
  ref: *shared
copy: *c
";
    let v: noyalib::Value = from_str(yaml).expect("alias-in-anchor parses to Value");
    let container = v
        .get_path("container")
        .and_then(|x| x.as_mapping())
        .unwrap();
    assert_eq!(container.get("ref").and_then(|x| x.as_i64()), Some(42));
    let copy = v.get_path("copy").and_then(|x| x.as_mapping()).unwrap();
    assert_eq!(copy.get("ref").and_then(|x| x.as_i64()), Some(42));
}

// ── L310, L352-L361 — handle_anchor anchor_def_spans + alias inside anchor.
// Specifically targets the SmallVec branch of `maybe_record` for
// `BufferedEvent::Alias` and the depth==0 anchor-completion paths for
// scalar/sequence/mapping/alias.

#[test]
fn coverage_stream_anchored_scalar_seq_map_alias() {
    // Anchored scalar (depth==0 scalar branch).
    let yaml1 = "\
a: &x hello
b: *x
";
    let m1: BTreeMap<String, String> = from_str(yaml1).unwrap();
    assert_eq!(m1["b"], "hello");

    // Anchored sequence (depth==0 sequence-end branch).
    let yaml2 = "\
a: &x [1, 2, 3]
b: *x
";
    let m2: BTreeMap<String, Vec<i64>> = from_str(yaml2).unwrap();
    assert_eq!(m2["b"], vec![1, 2, 3]);

    // Anchored mapping (depth==0 mapping-end branch).
    let yaml3 = "\
a: &x {k: 1}
b: *x
";
    let m3: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml3).unwrap();
    assert_eq!(m3["b"]["k"], 1);
}

// ── L393 — empty-buffer alias bails out as unknown anchor ─────────────────
// An alias to an anchor whose recorded buffer is empty (rare; can happen
// when the anchor declaration is on a malformed event chain that aborts
// before any payload events are recorded) should error.

#[test]
fn coverage_stream_unknown_anchor_alias() {
    let yaml = "\
target:
  ref: *missing
";
    let res: Result<BTreeMap<String, BTreeMap<String, String>>, _> = from_str(yaml);
    assert!(res.is_err());
}

// ── L405-L463 — inject_multi_merge_mapping_contents / buffer_rest_of_mapping
// Trigger the merge-key sequence path with nested sequences + scalars
// inside the locally-buffered tail mapping so all branches of
// `buffer_rest_of_mapping` (MapStart, SeqStart, SeqEnd, Alias, Scalar) fire.

#[test]
fn coverage_stream_merge_with_nested_local_content() {
    let yaml = "\
base: &b
  k1: 1
  k2: 2
target:
  <<: *b
  nested_seq: [a, b, c]
  nested_map:
    inner: deep
";
    let v: BTreeMap<String, BTreeMap<String, noyalib::Value>> = from_str(yaml).unwrap();
    let target = &v["target"];
    assert_eq!(target["k1"].as_i64(), Some(1));
    assert!(target["nested_seq"].is_sequence());
    assert!(target["nested_map"].is_mapping());
}

// ── L463-L464 — alias inside locally-buffered tail of a merge mapping ─────

#[test]
fn coverage_stream_merge_with_alias_in_local_tail() {
    // Targets the `BufferedEvent::Alias` case in `buffer_rest_of_mapping`
    // (line 463). Use Value to keep the test path agnostic to whether
    // streaming or AST handles the alias replay.
    let yaml = "\
shared: &s deep_value
base: &b
  k: 1
target:
  <<: *b
  copy: *s
";
    let v: noyalib::Value = from_str(yaml).expect("alias in local tail parses");
    let target = v.get_path("target").and_then(|x| x.as_mapping()).unwrap();
    assert_eq!(
        target.get("copy").and_then(|x| x.as_str()),
        Some("deep_value")
    );
}

// ── L492 — skip_event used when peeking an Option(Some) branch falls
// into a scalar non-null. Exercise via Option<i64> where some value is
// present and the deserializer must visit_some(self) (line ~828).

#[test]
fn coverage_stream_option_some_scalar() {
    #[derive(Deserialize)]
    struct Doc {
        x: Option<i64>,
        y: Option<i64>,
    }
    let yaml = "x: 5\ny: ~\n";
    let d: Doc = from_str(yaml).unwrap();
    assert_eq!(d.x, Some(5));
    assert_eq!(d.y, None);
}

// ── L500-L513 — skip_to_content + skip_value iterative balance traversal.
// `deserialize_ignored_any` is invoked for `#[serde(deny_unknown_fields)]`
// off + an extra field — but more reliable: serde_ignored / fields not
// in the struct simply call `deserialize_ignored_any`. Use a struct that
// has `#[serde(default)]` and an unknown extra field that must be skipped.

#[test]
fn coverage_stream_ignored_any_skips_complex_value() {
    #[derive(Deserialize)]
    struct Small {
        keep: i64,
    }
    // `extra` (a nested mapping containing a sequence) is consumed via
    // `deserialize_ignored_any` → `skip_value` → balance loop covering
    // SequenceStart/MappingStart/Scalar/SequenceEnd/MappingEnd arms.
    let yaml = "\
keep: 42
extra:
  nested:
    - 1
    - 2
  more:
    a: x
    b: y
";
    let s: Small = from_str(yaml).unwrap();
    assert_eq!(s.keep, 42);
}

// ── L537-L588 — take_tag_from_current / restore_tag_to_current for a
// tagged scalar that hits `deserialize_str` and must restore the tag
// before signalling fallback.

#[test]
fn coverage_stream_tagged_str_restores_tag_for_fallback() {
    // A `!!str`-tagged scalar that targets a String field must restore
    // the tag so the AST path resolves it correctly.
    #[derive(Deserialize)]
    struct Doc {
        value: String,
    }
    let yaml = "value: !!str 42\n";
    let d: Doc = from_str(yaml).unwrap();
    assert_eq!(d.value, "42");
}

// ── L628 — deserialize_any with a sequence event hits SeqAccess via
// type-erased Value visitor.

#[test]
fn coverage_stream_seq_via_value_target() {
    // Targeting `Value` keeps us off the streaming path; for streaming we
    // need a typed target. A `Vec<noyalib::Value>` still routes through
    // the streaming `deserialize_any` because each element is type-erased.
    let yaml = "[1, two, 3.0, true, null]\n";
    let v: Vec<noyalib::Value> = from_str(yaml).unwrap();
    assert_eq!(v.len(), 5);
    assert_eq!(v[0].as_i64(), Some(1));
    assert_eq!(v[1].as_str(), Some("two"));
    assert!(v[2].as_f64().is_some());
    assert_eq!(v[3].as_bool(), Some(true));
    assert!(v[4].is_null());
}

// ── L672-L705 — bool / i64 / u64 / f64 type-mismatch paths ─────────────────

#[test]
fn coverage_stream_bool_typemismatch_for_int() {
    // 42 isn't a bool — must surface a type mismatch.
    let res: Result<bool, _> = from_str("42\n");
    assert!(res.is_err());
}

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

#[test]
fn coverage_stream_i64_accepts_whole_float() {
    // 42.0 should be accepted as i64 via fract()==0 branch.
    let n: i64 = from_str("42.0\n").unwrap();
    assert_eq!(n, 42);
}

#[test]
fn coverage_stream_u64_typemismatch_for_negative() {
    let res: Result<u64, _> = from_str("-1\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_u64_accepts_whole_nonneg_float() {
    let n: u64 = from_str("7.0\n").unwrap();
    assert_eq!(n, 7);
}

#[test]
fn coverage_stream_f64_accepts_int() {
    let f: f64 = from_str("5\n").unwrap();
    assert!((f - 5.0).abs() < 1e-9);
}

#[test]
fn coverage_stream_f64_typemismatch_for_string() {
    let res: Result<f64, _> = from_str("not_a_number\n");
    assert!(res.is_err());
}

// ── L737-L766, L787 — deserialize_str: complex key / non-string scalar.

#[test]
fn coverage_stream_str_rejects_non_string_plain() {
    // Plain `42` isn't a string — must error.
    let res: Result<String, _> = from_str("42\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_str_quoted_scalar_passes() {
    let s: String = from_str("\"hello\"\n").unwrap();
    assert_eq!(s, "hello");
}

#[test]
fn coverage_stream_str_block_scalar_passes() {
    let s: String = from_str("|\n  block text\n").unwrap();
    assert_eq!(s, "block text\n");
}

// ── L812-L834 — deserialize_unit non-null-scalar mismatch ─────────────────

#[test]
fn coverage_stream_unit_mismatch_errors() {
    // Plain non-null scalar can't deserialise as `()`.
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct UnitField {
        u: (),
    }
    let res: Result<UnitField, _> = from_str("u: not_null\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_unit_struct_accepts_null() {
    #[derive(Deserialize)]
    struct U;
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        u: U,
    }
    let _: Doc = from_str("u: ~\n").unwrap();
}

// ── L851-L862 — deserialize_newtype_struct early return for SPANNED ────────

#[test]
fn coverage_stream_spanned_falls_back() {
    // `Spanned<T>` must bail to AST fallback. Verify via from_str_with_config
    // since Spanned support is in the loader path.
    use noyalib::Spanned;
    #[derive(Deserialize)]
    struct Doc {
        inner: Spanned<String>,
    }
    let yaml = "inner: hello\n";
    let d: Doc = from_str(yaml).unwrap();
    assert_eq!(d.inner.value, "hello");
}

// ── L856-L862 — newtype_struct with !!core tag passes through ─────────────

#[test]
fn coverage_stream_newtype_with_core_tag() {
    #[derive(Deserialize, PartialEq, Debug)]
    struct Wrap(i64);
    #[derive(Deserialize)]
    struct Doc {
        v: Wrap,
    }
    let yaml = "v: !!int 42\n";
    let d: Doc = from_str(yaml).unwrap();
    assert_eq!(d.v, Wrap(42));
}

// ── L885-L929 — deserialize_seq / deserialize_map with non-sequence /
// non-mapping events (type mismatch through streaming path).

#[test]
fn coverage_stream_seq_typemismatch() {
    // Top-level scalar rejected as sequence.
    let res: Result<Vec<i64>, _> = from_str("not_a_seq\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_map_typemismatch() {
    // Top-level scalar rejected as mapping.
    let res: Result<BTreeMap<String, i64>, _> = from_str("not_a_map\n");
    assert!(res.is_err());
}

// ── L960-L997 — deserialize_enum: unit variant from scalar, struct
// variant from single-key mapping, error on non-scalar variant name.

#[derive(Debug, Deserialize, PartialEq)]
enum E {
    Unit,
    Tup(i32, i32),
    Strukt { a: i32 },
    NewT(String),
}

#[test]
fn coverage_stream_enum_unit_from_scalar() {
    #[derive(Deserialize)]
    struct Doc {
        e: E,
    }
    let d: Doc = from_str("e: Unit\n").unwrap();
    assert_eq!(d.e, E::Unit);
}

#[test]
fn coverage_stream_enum_newtype_variant() {
    #[derive(Deserialize)]
    struct Doc {
        e: E,
    }
    let d: Doc = from_str("e:\n  NewT: hello\n").unwrap();
    assert_eq!(d.e, E::NewT("hello".into()));
}

#[test]
fn coverage_stream_enum_struct_variant() {
    #[derive(Deserialize)]
    struct Doc {
        e: E,
    }
    let d: Doc = from_str("e:\n  Strukt:\n    a: 1\n").unwrap();
    assert_eq!(d.e, E::Strukt { a: 1 });
}

#[test]
fn coverage_stream_enum_tuple_variant() {
    #[derive(Deserialize)]
    struct Doc {
        e: E,
    }
    let d: Doc = from_str("e:\n  Tup: [1, 2]\n").unwrap();
    assert_eq!(d.e, E::Tup(1, 2));
}

// ── L1003-L1017 — deserialize_identifier non-scalar error / ignored_any.

#[test]
fn coverage_stream_ignored_any_at_top_level() {
    use serde::de::IgnoredAny;
    let _: IgnoredAny = from_str("a: 1\nb: 2\n").unwrap();
}

// ── L1052-L1101 — deserialize_bytes branches: !!binary, plain str,
// type mismatches for null/bool/int/float.

#[test]
fn coverage_stream_bytes_from_plain_string() {
    #[derive(Deserialize)]
    struct Doc {
        b: ByteBuf,
    }
    let d: Doc = from_str("b: hello\n").unwrap();
    assert_eq!(d.b.as_ref(), b"hello");
}

#[test]
fn coverage_stream_bytes_rejects_null() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        b: ByteBuf,
    }
    let res: Result<Doc, _> = from_str("b: ~\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_bytes_rejects_bool() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        b: ByteBuf,
    }
    let res: Result<Doc, _> = from_str("b: true\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_bytes_rejects_int() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        b: ByteBuf,
    }
    let res: Result<Doc, _> = from_str("b: 42\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_bytes_rejects_float() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        b: ByteBuf,
    }
    let res: Result<Doc, _> = from_str("b: 3.14\n");
    assert!(res.is_err());
}

#[test]
fn coverage_stream_bytes_binary_invalid() {
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        b: ByteBuf,
    }
    let res: Result<Doc, _> = from_str("b: !!binary \"$$not_b64$$\"\n");
    assert!(res.is_err());
}

// ── L1134, L1193, L1207, L1210, L1219-L1234 — merge sequence empty list +
// merge sequence non-alias element forces fallback.

#[test]
fn coverage_stream_merge_empty_sequence() {
    let yaml = "\
target:
  <<: []
  k: 1
";
    let v: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml).unwrap();
    assert_eq!(v["target"]["k"], 1);
}

#[test]
fn coverage_stream_merge_sequence_with_non_alias_falls_back() {
    // `<<: [{a: 1}]` — non-alias element in the merge sequence forces the
    // streaming path to bail; AST fallback handles it.
    let yaml = "\
target:
  <<:
    - a: 1
  b: 2
";
    let v: BTreeMap<String, BTreeMap<String, i64>> = from_str(yaml).unwrap();
    assert_eq!(v["target"]["a"], 1);
    assert_eq!(v["target"]["b"], 2);
}

// ── L1261-L1265 — duplicate-key policy First / Error paths on streaming.

#[test]
fn coverage_stream_duplicate_first_skips_later() {
    use noyalib::DuplicateKeyPolicy;
    let yaml = "k: 1\nk: 2\nk: 3\n";
    let cfg = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::First);
    let m: BTreeMap<String, i64> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["k"], 1);
}

#[test]
fn coverage_stream_duplicate_error_returns_error() {
    use noyalib::DuplicateKeyPolicy;
    let yaml = "k: 1\nk: 2\n";
    let cfg = ParserConfig::new().duplicate_key_policy(DuplicateKeyPolicy::Error);
    let res: Result<BTreeMap<String, i64>, _> = from_str_with_config(yaml, &cfg);
    assert!(res.is_err());
}

// ── L1332-L1383 — StreamingVariantAccess paths driven by `<<` merge into
// an enum target. Use a mapping with a single-key variant where the value
// involves further deserialization.

#[test]
fn coverage_stream_enum_with_struct_variant_mapping() {
    #[derive(Debug, Deserialize, PartialEq)]
    enum Choice {
        Pair { a: i32, b: i32 },
    }
    let yaml = "Pair: {a: 1, b: 2}\n";
    let c: Choice = from_str(yaml).unwrap();
    assert_eq!(c, Choice::Pair { a: 1, b: 2 });
}

// ── L1393-L1438 — StreamingTagMapAccess / TagEnumAccess: registry-bypass
// path takes a tagged newtype value through visit_newtype_struct.

#[test]
fn coverage_stream_tag_registry_strips_custom_tag() {
    #[derive(Deserialize, PartialEq, Debug)]
    struct Celsius(f64);
    #[derive(Deserialize)]
    struct Doc {
        t: Celsius,
    }
    let registry = Arc::new(TagRegistry::new().with("!Celsius"));
    let cfg = ParserConfig::new().tag_registry(Arc::clone(&registry));
    let yaml = "t: !Celsius 42.0\n";
    let d: Doc = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(d.t, Celsius(42.0));
}

#[test]
fn coverage_stream_tag_registry_strips_seq_tag() {
    let registry = Arc::new(TagRegistry::new().with("!Items"));
    let cfg = ParserConfig::new().tag_registry(Arc::clone(&registry));
    let yaml = "items: !Items [1, 2, 3]\n";
    #[derive(Deserialize)]
    struct Doc {
        items: Vec<i64>,
    }
    let d: Doc = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(d.items, vec![1, 2, 3]);
}

#[test]
fn coverage_stream_tag_registry_strips_map_tag() {
    let registry = Arc::new(TagRegistry::new().with("!Cfg"));
    let cfg = ParserConfig::new().tag_registry(Arc::clone(&registry));
    let yaml = "cfg: !Cfg {a: 1, b: 2}\n";
    #[derive(Deserialize)]
    struct Doc {
        cfg: BTreeMap<String, i64>,
    }
    let d: Doc = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(d.cfg["a"], 1);
}

#[test]
fn coverage_stream_unregistered_tag_falls_back() {
    // A custom tag with no registry hits the StreamingTagMapAccess path
    // which surfaces the tagged value as a single-key map `{tag: value}`,
    // letting the AST fallback resolve it.
    let yaml = "v: !Custom 42\n";
    let v: noyalib::Value = from_str(yaml).unwrap();
    assert!(v.is_mapping());
}

// ── L1562-L1631 — sexagesimal int / float parsing helpers.

#[test]
fn coverage_stream_legacy_sexagesimal_int() {
    let yaml = "60:00\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let n: i64 = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(n, 3600);
}

#[test]
fn coverage_stream_legacy_sexagesimal_float() {
    let yaml = "60:00.5\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let f: f64 = from_str_with_config(yaml, &cfg).unwrap();
    assert!((f - 3600.5).abs() < 1e-9);
}

#[test]
fn coverage_stream_legacy_sexagesimal_negative() {
    let yaml = "-1:30:00\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let n: i64 = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(n, -(3600 + 1800));
}

#[test]
fn coverage_stream_legacy_sexagesimal_invalid_falls_through() {
    // `1:99` — second component >= 60, must fall through to plain string.
    let yaml = "x: 1:99\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let m: BTreeMap<String, String> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["x"], "1:99");
}

// ── L1639, L1651, L1663 — float-shaped sexagesimal with empty parts /
// non-digit parts triggers the `None` returns.

#[test]
fn coverage_stream_legacy_sexagesimal_empty_part() {
    let yaml = "x: 1::00\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let m: BTreeMap<String, String> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["x"], "1::00");
}

#[test]
fn coverage_stream_legacy_sexagesimal_falls_back_to_float() {
    // `1.5` with sexagesimal enabled but no `:` — falls through to f64 parse.
    let yaml = "1.5\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let f: f64 = from_str_with_config(yaml, &cfg).unwrap();
    assert!((f - 1.5).abs() < 1e-9);
}

#[test]
fn coverage_stream_legacy_sexagesimal_falls_back_to_string() {
    // `not:a:number` looks colon-shaped but has non-digit parts.
    let yaml = "x: notnum:abc\n";
    let cfg = ParserConfig::new().legacy_sexagesimal(true);
    let m: BTreeMap<String, String> = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(m["x"], "notnum:abc");
}

// ── L1684 — parse_integer hex / octal paths via legacy_octal_numbers.

#[test]
fn coverage_stream_hex_integer() {
    let n: i64 = from_str("0xFF\n").unwrap();
    assert_eq!(n, 255);
}

#[test]
fn coverage_stream_octal_o_prefix() {
    let n: i64 = from_str("0o17\n").unwrap();
    assert_eq!(n, 15);
}

#[test]
fn coverage_stream_legacy_octal_bare_zero_prefix() {
    let yaml = "0644\n";
    let cfg = ParserConfig::new().legacy_octal_numbers(true);
    let n: i64 = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(n, 0o644);
}

// ── L1703-L1798 — collect_keys / extract_local_keys / filter_merge_entries
// with nested sequence/mapping bodies inside merge sources & locals.

#[test]
fn coverage_stream_merge_with_nested_seq_in_source() {
    let yaml = "\
base: &b
  list: [1, 2]
  flag: true
target:
  <<: *b
  extra: x
";
    let v: BTreeMap<String, BTreeMap<String, noyalib::Value>> = from_str(yaml).unwrap();
    let target = &v["target"];
    assert!(target["list"].is_sequence());
    assert_eq!(target["flag"].as_bool(), Some(true));
    assert_eq!(target["extra"].as_str(), Some("x"));
}

#[test]
fn coverage_stream_merge_with_nested_map_in_source() {
    let yaml = "\
base: &b
  inner:
    a: 1
    b: 2
  flag: true
target:
  <<: *b
  extra: x
";
    let v: BTreeMap<String, BTreeMap<String, noyalib::Value>> = from_str(yaml).unwrap();
    let target = &v["target"];
    assert!(target["inner"].is_mapping());
}

#[test]
fn coverage_stream_merge_local_seq_value_releases_d() {
    // Locally-buffered tail contains a sequence value — extract_local_keys
    // must increment / decrement `d` on SeqStart/SeqEnd and reset `key=true`.
    let yaml = "\
base: &b {a: 1}
target:
  <<: *b
  list: [x, y]
  follow: 9
";
    let v: BTreeMap<String, BTreeMap<String, noyalib::Value>> = from_str(yaml).unwrap();
    let t = &v["target"];
    assert!(t["list"].is_sequence());
    assert_eq!(t["follow"].as_i64(), Some(9));
}

#[test]
fn coverage_stream_merge_local_alias_value() {
    // Local tail contains an Alias value — `extract_local_keys` Alias arm
    // (line 1786) and `BufferedEvent::Alias` case in
    // `buffer_rest_of_mapping` (line 463).
    let yaml = "\
base: &b {a: 1}
shared: &s sharedval
target:
  <<: *b
  ref: *s
  follow: 9
";
    let v: noyalib::Value = from_str(yaml).expect("alias-as-value tail parses");
    let target = v.get_path("target").and_then(|x| x.as_mapping()).unwrap();
    assert_eq!(
        target.get("ref").and_then(|x| x.as_str()),
        Some("sharedval")
    );
    assert_eq!(target.get("follow").and_then(|x| x.as_i64()), Some(9));
}

// ── L1751, L1758, L1766 — skip_buffered_value sequence/map skip helpers ───

#[test]
fn coverage_stream_filter_merge_seq_value() {
    // The merge source has a sequence value the filter must skip over.
    let yaml = "\
base: &b
  k_seq: [1, 2, 3]
  k_str: hello
target:
  <<: *b
";
    let v: BTreeMap<String, BTreeMap<String, noyalib::Value>> = from_str(yaml).unwrap();
    assert!(v["target"]["k_seq"].is_sequence());
}

#[test]
fn coverage_stream_filter_merge_map_value() {
    let yaml = "\
base: &b
  k_map:
    deep: 1
  k_str: hello
target:
  <<: *b
";
    let v: BTreeMap<String, BTreeMap<String, noyalib::Value>> = from_str(yaml).unwrap();
    assert!(v["target"]["k_map"].is_mapping());
}

// ── L1811-L1820 — filter_merge_entries: local key wins over merge source.

#[test]
fn coverage_stream_filter_merge_local_overrides_source() {
    let yaml = "\
base: &b
  k: from_base
target:
  <<: *b
  k: from_local
";
    let v: BTreeMap<String, BTreeMap<String, String>> = from_str(yaml).unwrap();
    // Local key wins over merge source per YAML spec.
    assert_eq!(v["target"]["k"], "from_local");
}

// ── Direct StreamingDeserializer driver: max-alias / depth / mapping cap ──

#[test]
fn coverage_stream_recursion_limit_exceeded() {
    // Build a deeply nested structure that exceeds `max_depth=2`.
    let yaml = "a:\n  b:\n    c:\n      d: 1\n";
    let cfg = ParserConfig::new().max_depth(2);
    let res: Result<BTreeMap<String, noyalib::Value>, _> = from_str_with_config(yaml, &cfg);
    assert!(res.is_err());
}

#[test]
fn coverage_stream_max_sequence_length_exceeded() {
    let yaml = "[1, 2, 3, 4, 5]\n";
    let cfg = ParserConfig::new().max_sequence_length(2);
    let res: Result<Vec<i64>, _> = from_str_with_config(yaml, &cfg);
    assert!(res.is_err());
}

#[test]
fn coverage_stream_max_mapping_keys_exceeded() {
    let yaml = "a: 1\nb: 2\nc: 3\n";
    let cfg = ParserConfig::new().max_mapping_keys(2);
    let res: Result<BTreeMap<String, i64>, _> = from_str_with_config(yaml, &cfg);
    assert!(res.is_err());
}

#[test]
fn coverage_stream_max_alias_expansions_exceeded() {
    // Self-amplifying alias chain: each alias resolves to a structure
    // containing the next alias, blowing the alias counter past 1.
    let yaml = "\
a: &a 1
b: &b
  - *a
  - *a
  - *a
c:
  - *b
  - *b
  - *b
";
    let cfg = ParserConfig::new().max_alias_expansions(1);
    let res: Result<BTreeMap<String, noyalib::Value>, _> = from_str_with_config(yaml, &cfg);
    assert!(res.is_err());
}

// ── Direct StreamingDeserializer construction tests for tag_in_registry
// `!!`-prefixed core-tag short-circuit (line ~558-568).

#[test]
fn coverage_stream_core_tag_str_via_streaming_de() {
    // Core `!!str` is never stripped by the registry — it goes through
    // the AST fallback for tag resolution. This exercises the registry
    // short-circuit branch.
    let registry = Arc::new(TagRegistry::new().with("!!str"));
    let cfg = ParserConfig::new().tag_registry(Arc::clone(&registry));
    let yaml = "v: !!str 42\n";
    #[derive(Deserialize)]
    struct Doc {
        v: String,
    }
    let d: Doc = from_str_with_config(yaml, &cfg).unwrap();
    assert_eq!(d.v, "42");
}

// ── Direct streaming construction for `Drop` paths on partial-consumption.

#[test]
fn coverage_stream_partial_consumption_drops_seq() {
    // Construct a nested seq inside a struct, then deserialize where the
    // visitor encounters an early error. The Drop impl must drain on
    // partial consumption — we trigger this by parsing a seq that
    // contains an element of the wrong type (string in i64 seq), causing
    // mid-iteration failure that exercises the Drop drain loop.
    let yaml = "values:\n  - 1\n  - 2\n  - not_a_number\n";
    #[derive(Deserialize)]
    #[allow(dead_code)]
    struct Doc {
        values: Vec<i64>,
    }
    let res: Result<Doc, _> = from_str(yaml);
    assert!(res.is_err());
}

// ── Module-level convenience: trailing data after first node ──────────────

#[test]
fn coverage_stream_trailing_garbage_after_value_propagates() {
    // The drain loop in `from_str_streaming` must surface any error past
    // the first satisfied node. A bracketed flow scalar followed by stray
    // unbalanced text shows up.
    let yaml = "key: ok\n: bad\n";
    let res: Result<BTreeMap<String, String>, _> = from_str(yaml);
    // Either succeeds (lenient parse) or errors — the key path is that
    // the drain loop processes events past the satisfied node without panic.
    let _ = res;
}