serde-saphyr 0.0.28

YAML (de)serializer for Serde, emphasizing panic-free parsing, fast builds and good error reporting. Supports properties, comments, include
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
#![cfg(all(feature = "serialize", feature = "deserialize"))]
mod tests {
    use serde::Deserialize;
    use serde_saphyr::budget::BudgetBreach;
    use serde_saphyr::{DuplicateKeyPolicy, Error, from_reader};
    use serde_saphyr::{
        from_multiple, from_multiple_with_options, from_str, from_str_with_options,
    };
    use std::collections::HashMap;

    fn unwrap_snippet(err: &Error) -> &Error {
        match err {
            Error::WithSnippet { error, .. } => error,
            other => other,
        }
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Details {
        city: String,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Person {
        name: String,
        age: usize,
        details: Details,
    }

    #[test]
    fn simple_nested_struct() {
        let y = "name: John\nage: 80\ndetails: { city: Paris }\n";
        let p: Person = from_str(y).unwrap();
        assert_eq!(p.name, "John");
        assert_eq!(p.age, 80);
        assert_eq!(p.details.city, "Paris");
    }

    #[test]
    fn simple_nested_struct_from_reader() {
        let y = "name: John\nage: 80\ndetails: { city: Paris }\n";
        let reader = std::io::Cursor::new(y.as_bytes().to_owned());
        let p: Person = from_reader(reader).unwrap();
        assert_eq!(p.name, "John");
        assert_eq!(p.age, 80);
        assert_eq!(p.details.city, "Paris");
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Named {
        name: String,
    }

    #[test]
    fn anchors_and_aliases_map_clone() {
        let y = "a: &A { name: John }\nb: *A\n";
        let m: HashMap<String, Named> = from_str(y).unwrap();
        assert_eq!(m.get("a").unwrap().name, "John");
        assert_eq!(m.get("b").unwrap().name, "John");
    }

    #[test]
    fn legacy_misplaced_bracket_in_flow_sequence_is_accepted() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct Cfg {
            key: Vec<usize>,
        }

        let yaml = r#"
            key: [ 1, 2, 2
            ] # this sits wrongly but okay
        "#;

        let cfg: Cfg = from_str(yaml).unwrap();
        assert_eq!(cfg.key, vec![1, 2, 2]);
    }

    #[test]
    fn multiple_documents_deserialize_into_vec() {
        let yaml = "---\nname: John\nage: 80\ndetails:\n  city: Paris\n---\nname: Jane\nage: 42\ndetails:\n  city: London\n";
        let people: Vec<Person> = from_multiple(yaml).unwrap();
        assert_eq!(people.len(), 2);
        assert_eq!(people[0].name, "John");
        assert_eq!(people[1].name, "Jane");
    }

    #[test]
    fn multiple_documents_preserves_tagged_string_nullish_scalar() {
        let yaml = "--- !!str null
---
plain
";
        let values: Vec<String> = from_multiple(yaml).unwrap();
        assert_eq!(values, vec!["null", "plain"]);
    }

    #[test]
    fn multiple_documents_still_skips_explicit_null() {
        let yaml = "--- null
---
plain
";
        let values: Vec<String> = from_multiple(yaml).unwrap();
        assert_eq!(values, vec!["plain"]);
    }

    #[test]
    fn budget_violation_is_reported() {
        use std::collections::HashMap;

        let options = serde_saphyr::options! {
            budget: serde_saphyr::budget! {
                max_nodes: 1,
            },
        };

        let yaml = "a: 1\n";
        let err = from_str_with_options::<HashMap<String, String>>(yaml, options).unwrap_err();
        assert!(matches!(
            unwrap_snippet(&err),
            Error::Budget {
                breach: BudgetBreach::Nodes { .. },
                ..
            }
        ));
    }

    #[test]
    fn multiple_documents_budget_violation() {
        use std::collections::HashMap;

        let options = serde_saphyr::options! {
            budget: serde_saphyr::budget! {
                max_nodes: 1,
            },
        };

        let yaml = "a: 1\n---\nb: 2\n";
        let err = from_multiple_with_options::<HashMap<String, String>>(yaml, options).unwrap_err();
        assert!(matches!(
            unwrap_snippet(&err),
            Error::Budget {
                breach: BudgetBreach::Nodes { .. },
                ..
            }
        ));
    }

    #[test]
    fn anchor_with_nested_nonanchored_container_records_balanced_events() {
        #[derive(Deserialize, Debug, PartialEq)]
        struct Inner {
            m: String,
        }
        #[derive(Deserialize, Debug, PartialEq)]
        struct Outer {
            k: Inner,
        }

        let y = "a: &A { k: { m: v } }\nb: *A\n";
        let m: HashMap<String, Outer> = from_str(y).unwrap();
        assert_eq!(m["a"].k.m, "v");
        assert_eq!(m["b"].k.m, "v");
    }

    // ---------- YAML 1.2 floats ----------
    #[derive(Debug, Deserialize)]
    struct Floats {
        a: f64,
        b: f64,
        c: f32,
    }

    #[test]
    fn yaml12_float_specials() {
        let y = "a: .nan\nb: +.inf\nc: -.inf\n";
        let v: Floats = from_str(y).unwrap();
        assert!(v.a.is_nan());
        assert!(v.b.is_infinite() && v.b.is_sign_positive());
        assert!(v.c.is_infinite() && (v.c.is_sign_negative() as bool));
    }

    // ---------- Duplicate key policy ----------
    #[test]
    fn duplicate_keys_error_policy() {
        let y = "a: 1\na: 2\n";
        let err = from_str::<HashMap<String, i32>>(y).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("duplicate mapping key: a"));
    }

    #[test]
    fn duplicate_keys_error_policy_custom_tagged_string_key() {
        let y = "a: 1\n!foo a: 2\n";
        let err = from_str::<HashMap<String, i32>>(y).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("duplicate mapping key: a"));
    }

    #[test]
    fn duplicate_keys_first_wins_policy() {
        let y = "a: 1\na: 2\nb: 3\n";
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::FirstWins,
        };
        let m = from_str_with_options::<HashMap<String, i32>>(y, opt).unwrap();
        assert_eq!(m.get("a"), Some(&1));
        assert_eq!(m.get("b"), Some(&3));
        assert_eq!(m.len(), 2);
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Background {
        color: String,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct BackgroundRoot {
        target: Background,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct BackgroundNestedRoot {
        background: Background,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct BackgroundRootWithBase {
        base: HashMap<String, String>,
        target: Background,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    enum BackgroundNode {
        Background { color: String },
    }

    #[test]
    fn duplicate_keys_last_wins_direct_struct() {
        let y = "color: red\ncolor: blue\n";
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let bg = from_str_with_options::<Background>(y, opt).unwrap();
        assert_eq!(bg.color, "blue");
    }

    #[test]
    fn duplicate_keys_last_wins_nested_struct() {
        let y = "background:\n  color: red\n  color: blue\n";
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let root = from_str_with_options::<BackgroundNestedRoot>(y, opt).unwrap();
        assert_eq!(root.background.color, "blue");
    }

    #[test]
    fn duplicate_keys_last_wins_struct_variant() {
        let y = "Background:\n  color: red\n  color: blue\n";
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let node = from_str_with_options::<BackgroundNode>(y, opt).unwrap();
        assert_eq!(
            node,
            BackgroundNode::Background {
                color: "blue".to_owned(),
            }
        );
    }

    #[test]
    fn duplicate_keys_first_wins_direct_struct() {
        let y = "color: red\ncolor: blue\n";
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::FirstWins,
        };
        let bg = from_str_with_options::<Background>(y, opt).unwrap();
        assert_eq!(bg.color, "red");
    }

    #[test]
    fn duplicate_keys_error_direct_struct() {
        let y = "color: red\ncolor: blue\n";
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::Error,
        };
        let err = from_str_with_options::<Background>(y, opt).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("duplicate mapping key: color"));
    }

    #[test]
    fn duplicate_keys_last_wins_direct_struct_from_merge_source() {
        let y = r#"
target:
  <<: { color: red, color: blue }
"#;
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let root = from_str_with_options::<BackgroundRoot>(y, opt).unwrap();
        assert_eq!(root.target.color, "blue");
    }

    #[test]
    fn duplicate_keys_last_wins_struct_treats_merge_key_as_ordinary_when_configured() {
        #[derive(Debug, Deserialize, PartialEq)]
        struct HasMergeLikeField {
            #[serde(rename = "<<")]
            merge_like: HashMap<String, String>,
        }

        let y = r#"
<<: { color: blue }
"#;

        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
            merge_keys: serde_saphyr::options::MergeKeyPolicy::AsOrdinary,
        };

        let got = from_str_with_options::<HasMergeLikeField>(y, opt).unwrap();
        assert_eq!(
            got.merge_like.get("color").map(String::as_str),
            Some("blue")
        );
    }

    #[test]
    fn duplicate_keys_last_wins_struct_explicit_field_still_overrides_merge_source() {
        let y = r#"
target:
  <<: { color: red, color: blue }
  color: green
"#;

        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };

        let root = from_str_with_options::<BackgroundRoot>(y, opt).unwrap();
        assert_eq!(root.target.color, "green");
    }

    #[test]
    fn duplicate_keys_last_wins_direct_struct_from_aliased_merge_source() {
        let y = r#"
base: &B { color: red, color: blue }
target:
  <<: *B
"#;
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let root = from_str_with_options::<BackgroundRootWithBase>(y, opt).unwrap();
        assert_eq!(root.base.get("color").map(String::as_str), Some("blue"));
        assert_eq!(root.target.color, "blue");
    }

    #[test]
    fn duplicate_keys_first_wins_direct_struct_from_merge_source() {
        let y = r#"
target:
  <<: { color: red, color: blue }
"#;
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::FirstWins,
        };
        let root = from_str_with_options::<BackgroundRoot>(y, opt).unwrap();
        assert_eq!(root.target.color, "red");
    }

    #[test]
    fn duplicate_keys_error_direct_struct_from_merge_source() {
        let y = r#"
target:
  <<: { color: red, color: blue }
"#;
        let opt = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::Error,
        };
        let err = from_str_with_options::<BackgroundRoot>(y, opt).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("duplicate mapping key: color"));
    }

    #[test]
    fn duplicate_sequence_keys_policies() {
        let y = "?\n  - 1\n  - 2\n: first\n?\n  - 1\n  - 2\n: second\n";

        // Error policy should reject duplicate sequence keys.
        let err = from_str::<HashMap<Vec<i32>, String>>(y).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("duplicate mapping key"));

        let opt_first = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::FirstWins,
        };
        let first = from_str_with_options::<HashMap<Vec<i32>, String>>(y, opt_first).unwrap();
        assert_eq!(first.get(&vec![1, 2]).map(String::as_str), Some("first"));
        assert_eq!(first.len(), 1);

        let opt_last = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let last = from_str_with_options::<HashMap<Vec<i32>, String>>(y, opt_last).unwrap();
        assert_eq!(last.get(&vec![1, 2]).map(String::as_str), Some("second"));
        assert_eq!(last.len(), 1);
    }

    #[derive(Debug, Deserialize, PartialEq, Eq, Hash)]
    struct StructKey {
        a: i32,
        b: String,
    }

    #[test]
    fn duplicate_struct_keys_policies() {
        let y = "?\n  a: 1\n  b: foo\n: 7\n?\n  a: 1\n  b: foo\n: 9\n";

        let err = from_str::<HashMap<StructKey, i32>>(y).unwrap_err();
        let msg = format!("{err}");
        assert!(msg.contains("duplicate mapping key"));

        let opt_first = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::FirstWins,
        };
        let first = from_str_with_options::<HashMap<StructKey, i32>>(y, opt_first).unwrap();
        assert_eq!(
            first.get(&StructKey {
                a: 1,
                b: "foo".into()
            }),
            Some(&7)
        );
        assert_eq!(first.len(), 1);

        let opt_last = serde_saphyr::options! {
            duplicate_keys: DuplicateKeyPolicy::LastWins,
        };
        let last = from_str_with_options::<HashMap<StructKey, i32>>(y, opt_last).unwrap();
        assert_eq!(
            last.get(&StructKey {
                a: 1,
                b: "foo".into()
            }),
            Some(&9)
        );
        assert_eq!(last.len(), 1);
    }

    mod hardening_policy_fixed_yaml_tests {
        use super::*;
        use serde::Deserialize;
        use serde_saphyr::from_str_with_options;
        use std::collections::HashMap;

        // ---------- Duplicate key policy: LastWins ----------
        #[test]
        fn duplicate_keys_last_wins_policy() {
            let y = "a: 1\na: 2\nb: 3\n";
            let opt = serde_saphyr::options! {
                duplicate_keys: DuplicateKeyPolicy::LastWins,
            };
            let m = from_str_with_options::<HashMap<String, i32>>(y, opt).unwrap();
            assert_eq!(m.get("a"), Some(&2));
            assert_eq!(m.get("b"), Some(&3));
            assert_eq!(m.len(), 2);
        }

        // ---------- Alias-bomb hardening: per-anchor expansion cap ----------
        //
        // NOTE: The previous version placed the anchored node as a *separate root node*,
        // which is invalid when deserializing into a mapping. Here we anchor the value
        // under a key ("defs") so the whole document is a single mapping.
        #[test]
        fn alias_per_anchor_expansion_limit() {
            // Anchor &A once, then reference it three times; cap expansions at 2.
            let y = "defs: &A { k: v }\nx: *A\ny: *A\nz: *A\n";
            let opt = serde_saphyr::options! {
                alias_limits: serde_saphyr::alias_limits! {
                    max_alias_expansions_per_anchor: 2,
                },
            };
            let err = from_str_with_options::<HashMap<String, HashMap<String, String>>>(y, opt)
                .unwrap_err();
            let msg = format!("{err}");
            assert!(
                msg.contains("alias expansion limit exceeded"),
                "unexpected error: {msg}"
            );
        }

        // ---------- Alias-bomb hardening: total replayed events cap ----------
        //
        // We define the anchor under "defs" and then use it twice in "list".
        // Each expansion of [1,2,3,4] injects 6 events (start, 4 scalars, end) → 12 total.
        // With a cap of 10 this must fail.
        #[derive(Debug, Deserialize)]
        #[allow(dead_code)]
        struct Data {
            defs: Vec<u32>,
            list: Vec<Vec<u32>>,
        }

        #[test]
        fn alias_total_replayed_events_limit() {
            let y = "defs: &A [1, 2, 3, 4]\nlist: [*A, *A]\n";
            let opt = serde_saphyr::options! {
                alias_limits: serde_saphyr::alias_limits! {
                    max_total_replayed_events: 10,
                },
            };
            let err = from_str_with_options::<Data>(y, opt).unwrap_err();
            let msg = format!("{err}");
            assert!(
                msg.contains("alias replay limit exceeded"),
                "unexpected error: {msg}"
            );
        }

        // ---------- Alias-bomb hardening: replay stack depth cap ----------
        //
        // Due to the design that *resolves aliases during recording* of anchors,
        // nested alias chains are flattened before use. To still verify the guard,
        // we set the maximum replay stack depth to 0 and trigger a single alias,
        // which must fail immediately.
        #[test]
        fn alias_replay_stack_depth_limit() {
            let y = "defs: &A [1]\nout: *A\n";
            let opt = serde_saphyr::options! {
                alias_limits: serde_saphyr::alias_limits! {
                    max_replay_stack_depth: 0,
                },
            };
            let err = from_str_with_options::<HashMap<String, Vec<u32>>>(y, opt).unwrap_err();
            let msg = format!("{err}");
            assert!(
                msg.contains("alias replay stack depth exceeded"),
                "unexpected error: {msg}"
            );
        }

        // Place the anchor *inside* the mapping so the document root is a mapping
        // (which matches HashMap<_, _>), then alias it multiple times to exceed the budget.
        #[test]
        fn alias_replay_counts_toward_budget() {
            let options = serde_saphyr::options! {
                budget: serde_saphyr::budget! {
                    max_nodes: 10,
                },
            };

            // Root is a mapping with key "seq". First element defines &A, the rest alias it.
            let y = "\
                seq:
                  - &A [1,2,3]
                  - *A
                  - *A
                  - *A
                  - *A
                ";
            let err =
                from_str_with_options::<HashMap<String, Vec<Vec<u32>>>>(y, options).unwrap_err();
            assert!(format!("{err}").contains("budget"));
        }

        #[test]
        fn merge_key_budget_still_counts_after_alias_value_replay() {
            let y = r#"
base: &base
  x: 1
root:
  keep: *base
  <<: *base
"#;

            let options = serde_saphyr::options! {
                budget: serde_saphyr::budget! {
                    max_merge_keys: 0,
                },
            };

            let err = from_str_with_options::<HashMap<String, serde_json::Value>>(y, options)
                .unwrap_err();
            assert!(matches!(
                unwrap_snippet(&err),
                Error::Budget {
                    breach: BudgetBreach::MergeKeys { .. },
                    ..
                }
            ));
        }
    }
}