ron2 0.3.0

RON parser with full AST access, perfect round-trip fidelity, and formatting tools
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
//! Compatibility tests between the original `ron` crate and `ron2`.
//!
//! This module tests that ron2 can parse ron output and vice versa,
//! validating roundtrips and semantic equivalence.

#![cfg(feature = "derive")]

use ron2::{FromRon, ToRon, Value, fmt::FormatConfig};
use serde::{Deserialize, Serialize};

// ============================================================================
// Test Types
// ============================================================================

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct UnitStruct;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Newtype(i32);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct TupleStruct(i32, String, bool);

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Point {
    x: i32,
    y: i32,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Person {
    name: String,
    age: u32,
    active: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
enum SimpleEnum {
    Unit,
    Tuple(i32),
    Struct { value: String },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
enum Color {
    Red,
    Green,
    Blue,
    Rgb(u8, u8, u8),
    Named { name: String, hex: String },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Collections {
    numbers: Vec<i32>,
    strings: Vec<String>,
    nested: Vec<Vec<i32>>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Optionals {
    maybe_int: Option<i32>,
    maybe_string: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Inner {
    value: i32,
    name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Nested {
    inner: Inner,
    list: Vec<Inner>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Numbers {
    i8_val: i8,
    i16_val: i16,
    i32_val: i32,
    i64_val: i64,
    u8_val: u8,
    u16_val: u16,
    u32_val: u32,
    u64_val: u64,
    f32_val: f32,
    f64_val: f64,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, ToRon, FromRon)]
struct Tuples {
    pair: (i32, String),
    triple: (bool, i32, f64),
}

// ============================================================================
// Cross-Parsing Tests: ron output → ron2 parsing
// ============================================================================

/// Test that ron2 can parse ron crate output
mod ron_to_ron2 {
    use super::*;

    fn ron_output_parseable_by_ron2<T>(value: &T)
    where
        T: Serialize + FromRon + PartialEq + std::fmt::Debug,
    {
        let ron_output = ron::to_string(value).expect("ron should serialize");
        let parsed = T::from_ron(&ron_output).expect("ron2 should parse ron output");
        assert_eq!(value, &parsed, "Values should match after ron → ron2");
    }

    #[test]
    fn unit_struct() {
        ron_output_parseable_by_ron2(&UnitStruct);
    }

    #[test]
    fn newtype() {
        ron_output_parseable_by_ron2(&Newtype(42));
        ron_output_parseable_by_ron2(&Newtype(-100));
        ron_output_parseable_by_ron2(&Newtype(0));
    }

    #[test]
    fn tuple_struct() {
        ron_output_parseable_by_ron2(&TupleStruct(42, "hello".into(), true));
        ron_output_parseable_by_ron2(&TupleStruct(-1, String::new(), false));
    }

    #[test]
    fn point() {
        ron_output_parseable_by_ron2(&Point { x: 10, y: 20 });
        ron_output_parseable_by_ron2(&Point { x: -5, y: 0 });
    }

    #[test]
    fn person() {
        ron_output_parseable_by_ron2(&Person {
            name: "Alice".into(),
            age: 30,
            active: true,
        });
    }

    #[test]
    fn simple_enum() {
        ron_output_parseable_by_ron2(&SimpleEnum::Unit);
        ron_output_parseable_by_ron2(&SimpleEnum::Tuple(42));
        ron_output_parseable_by_ron2(&SimpleEnum::Struct {
            value: "test".into(),
        });
    }

    #[test]
    fn color() {
        ron_output_parseable_by_ron2(&Color::Red);
        ron_output_parseable_by_ron2(&Color::Green);
        ron_output_parseable_by_ron2(&Color::Blue);
        ron_output_parseable_by_ron2(&Color::Rgb(255, 128, 0));
        ron_output_parseable_by_ron2(&Color::Named {
            name: "coral".into(),
            hex: "#FF7F50".into(),
        });
    }

    #[test]
    fn collections() {
        ron_output_parseable_by_ron2(&Collections {
            numbers: vec![1, 2, 3],
            strings: vec!["a".into(), "b".into()],
            nested: vec![vec![1, 2], vec![3, 4]],
        });
        ron_output_parseable_by_ron2(&Collections {
            numbers: vec![],
            strings: vec![],
            nested: vec![],
        });
    }

    #[test]
    fn optionals() {
        ron_output_parseable_by_ron2(&Optionals {
            maybe_int: Some(42),
            maybe_string: Some("hello".into()),
        });
        ron_output_parseable_by_ron2(&Optionals {
            maybe_int: None,
            maybe_string: None,
        });
    }

    #[test]
    fn nested() {
        ron_output_parseable_by_ron2(&Nested {
            inner: Inner {
                value: 1,
                name: "first".into(),
            },
            list: vec![
                Inner {
                    value: 2,
                    name: "second".into(),
                },
                Inner {
                    value: 3,
                    name: "third".into(),
                },
            ],
        });
    }

    #[test]
    fn numbers() {
        ron_output_parseable_by_ron2(&Numbers {
            i8_val: -128,
            i16_val: -32768,
            i32_val: -2147483648,
            i64_val: -9223372036854775808,
            u8_val: 255,
            u16_val: 65535,
            u32_val: 4294967295,
            u64_val: 18446744073709551615,
            f32_val: 1.5,
            f64_val: 2.5,
        });
    }

    #[test]
    fn tuples() {
        ron_output_parseable_by_ron2(&Tuples {
            pair: (42, "answer".into()),
            triple: (true, -1, 1.5),
        });
    }
}

// ============================================================================
// Cross-Parsing Tests: ron2 output → ron parsing
// ============================================================================

/// Test that ron crate can parse ron2 output
mod ron2_to_ron {
    use super::*;

    fn ron2_output_parseable_by_ron<T>(value: &T)
    where
        T: ToRon + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug,
    {
        let ron2_output = value.to_ron().expect("ron2 should serialize");
        let parsed: T = ron::from_str(&ron2_output).expect("ron should parse ron2 output");
        assert_eq!(value, &parsed, "Values should match after ron2 → ron");
    }

    #[test]
    fn unit_struct() {
        ron2_output_parseable_by_ron(&UnitStruct);
    }

    #[test]
    fn newtype() {
        ron2_output_parseable_by_ron(&Newtype(42));
        ron2_output_parseable_by_ron(&Newtype(-100));
    }

    #[test]
    fn tuple_struct() {
        ron2_output_parseable_by_ron(&TupleStruct(42, "hello".into(), true));
    }

    #[test]
    fn point() {
        ron2_output_parseable_by_ron(&Point { x: 10, y: 20 });
    }

    #[test]
    fn person() {
        ron2_output_parseable_by_ron(&Person {
            name: "Bob".into(),
            age: 25,
            active: false,
        });
    }

    #[test]
    fn simple_enum() {
        ron2_output_parseable_by_ron(&SimpleEnum::Unit);
        ron2_output_parseable_by_ron(&SimpleEnum::Tuple(99));
        ron2_output_parseable_by_ron(&SimpleEnum::Struct {
            value: "data".into(),
        });
    }

    #[test]
    fn color() {
        ron2_output_parseable_by_ron(&Color::Red);
        ron2_output_parseable_by_ron(&Color::Rgb(0, 0, 0));
        ron2_output_parseable_by_ron(&Color::Named {
            name: "white".into(),
            hex: "#FFFFFF".into(),
        });
    }

    #[test]
    fn collections() {
        ron2_output_parseable_by_ron(&Collections {
            numbers: vec![10, 20, 30],
            strings: vec!["x".into(), "y".into(), "z".into()],
            nested: vec![vec![100]],
        });
    }

    #[test]
    fn optionals() {
        ron2_output_parseable_by_ron(&Optionals {
            maybe_int: Some(0),
            maybe_string: None,
        });
    }

    #[test]
    fn nested() {
        ron2_output_parseable_by_ron(&Nested {
            inner: Inner {
                value: 42,
                name: "test".into(),
            },
            list: vec![],
        });
    }

    #[test]
    fn numbers() {
        ron2_output_parseable_by_ron(&Numbers {
            i8_val: 0,
            i16_val: 0,
            i32_val: 0,
            i64_val: 0,
            u8_val: 0,
            u16_val: 0,
            u32_val: 0,
            u64_val: 0,
            f32_val: 0.0,
            f64_val: 0.0,
        });
    }

    #[test]
    fn tuples() {
        ron2_output_parseable_by_ron(&Tuples {
            pair: (0, String::new()),
            triple: (false, 0, 0.0),
        });
    }
}

// ============================================================================
// Roundtrip Tests
// ============================================================================

/// Full roundtrip: ron → ron2 → ron
mod roundtrip_ron_to_ron2_to_ron {
    use super::*;

    fn roundtrip<T>(original: &T)
    where
        T: Serialize + FromRon + ToRon + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug,
    {
        // ron serialize
        let ron_text = ron::to_string(original).expect("ron serialize failed");

        // ron2 parse
        let ron2_parsed = T::from_ron(&ron_text).expect("ron2 parse failed");

        // ron2 serialize
        let ron2_text = ron2_parsed.to_ron().expect("ron2 serialize failed");

        // ron parse
        let final_value: T = ron::from_str(&ron2_text).expect("ron parse of ron2 output failed");

        assert_eq!(
            original, &final_value,
            "Roundtrip mismatch: ron → ron2 → ron"
        );
    }

    #[test]
    fn basic_structs() {
        roundtrip(&Point { x: 1, y: 2 });
        roundtrip(&Person {
            name: "Charlie".into(),
            age: 35,
            active: true,
        });
    }

    #[test]
    fn enums() {
        roundtrip(&SimpleEnum::Unit);
        roundtrip(&SimpleEnum::Tuple(42));
        roundtrip(&SimpleEnum::Struct {
            value: "roundtrip".into(),
        });
        roundtrip(&Color::Rgb(100, 150, 200));
    }

    #[test]
    fn collections() {
        roundtrip(&Collections {
            numbers: vec![1, 2, 3, 4, 5],
            strings: vec!["hello".into(), "world".into()],
            nested: vec![vec![1, 2], vec![3, 4, 5]],
        });
    }

    #[test]
    fn nested_types() {
        roundtrip(&Nested {
            inner: Inner {
                value: 100,
                name: "nested".into(),
            },
            list: vec![
                Inner {
                    value: 1,
                    name: "a".into(),
                },
                Inner {
                    value: 2,
                    name: "b".into(),
                },
            ],
        });
    }
}

/// Full roundtrip: ron2 → ron → ron2
mod roundtrip_ron2_to_ron_to_ron2 {
    use super::*;

    fn roundtrip<T>(original: &T)
    where
        T: ToRon + FromRon + Serialize + for<'de> Deserialize<'de> + PartialEq + std::fmt::Debug,
    {
        // ron2 serialize
        let ron2_text = original.to_ron().expect("ron2 serialize failed");

        // ron parse
        let ron_parsed: T = ron::from_str(&ron2_text).expect("ron parse failed");

        // ron serialize
        let ron_text = ron::to_string(&ron_parsed).expect("ron serialize failed");

        // ron2 parse
        let final_value = T::from_ron(&ron_text).expect("ron2 parse of ron output failed");

        assert_eq!(
            original, &final_value,
            "Roundtrip mismatch: ron2 → ron → ron2"
        );
    }

    #[test]
    fn basic_structs() {
        roundtrip(&Point { x: -10, y: 20 });
        roundtrip(&Person {
            name: "Diana".into(),
            age: 40,
            active: false,
        });
    }

    #[test]
    fn enums() {
        roundtrip(&SimpleEnum::Unit);
        roundtrip(&SimpleEnum::Tuple(-42));
        roundtrip(&Color::Named {
            name: "test".into(),
            hex: "#123456".into(),
        });
    }

    #[test]
    fn optionals() {
        roundtrip(&Optionals {
            maybe_int: Some(999),
            maybe_string: Some("optional".into()),
        });
        roundtrip(&Optionals {
            maybe_int: None,
            maybe_string: None,
        });
    }
}

// ============================================================================
// Value-Level Comparison Tests
// ============================================================================

/// Compare at the Value level
mod value_comparison {
    use super::*;

    /// Parse both ron and ron2 output to Value and compare semantically
    fn values_match<T>(value: &T)
    where
        T: Serialize + ToRon,
    {
        let ron_output = ron::to_string(value).expect("ron should serialize");
        let ron2_output = value.to_ron().expect("ron2 should serialize");

        let ron_value: Value = ron_output
            .parse::<ron2::Value>()
            .expect("ron2 should parse ron output");
        let ron2_value: Value = ron2_output
            .parse::<ron2::Value>()
            .expect("ron2 should parse ron2 output");

        // Values may differ in representation but should be semantically equivalent
        // For now, just verify both parse successfully
        assert!(
            !format!("{:?}", ron_value).is_empty(),
            "ron output should produce valid Value"
        );
        assert!(
            !format!("{:?}", ron2_value).is_empty(),
            "ron2 output should produce valid Value"
        );
    }

    #[test]
    fn structs() {
        values_match(&Point { x: 1, y: 2 });
        values_match(&Person {
            name: "Eve".into(),
            age: 28,
            active: true,
        });
    }

    #[test]
    fn enums() {
        values_match(&SimpleEnum::Unit);
        values_match(&SimpleEnum::Tuple(42));
        values_match(&Color::Rgb(1, 2, 3));
    }

    #[test]
    fn collections() {
        values_match(&Collections {
            numbers: vec![1, 2],
            strings: vec!["a".into()],
            nested: vec![],
        });
    }
}

// ============================================================================
// Edge Cases
// ============================================================================

mod edge_cases {
    use super::*;

    /// Test string escapes are handled consistently
    #[test]
    fn string_escapes() {
        let person = Person {
            name: "Line\nBreak".into(),
            age: 0,
            active: false,
        };

        let ron_out = ron::to_string(&person).unwrap();
        let ron2_parsed = Person::from_ron(&ron_out).unwrap();
        assert_eq!(person, ron2_parsed);

        let ron2_out = person.to_ron().unwrap();
        let ron_parsed: Person = ron::from_str(&ron2_out).unwrap();
        assert_eq!(person, ron_parsed);
    }

    /// Test special characters in strings
    #[test]
    fn special_chars() {
        let person = Person {
            name: "Tab\there".into(),
            age: 0,
            active: false,
        };

        let ron_out = ron::to_string(&person).unwrap();
        let ron2_parsed = Person::from_ron(&ron_out).unwrap();
        assert_eq!(person, ron2_parsed);
    }

    /// Test unicode strings
    #[test]
    fn unicode() {
        let person = Person {
            name: "日本語".into(),
            age: 0,
            active: false,
        };

        let ron_out = ron::to_string(&person).unwrap();
        let ron2_parsed = Person::from_ron(&ron_out).unwrap();
        assert_eq!(person, ron2_parsed);

        let ron2_out = person.to_ron().unwrap();
        let ron_parsed: Person = ron::from_str(&ron2_out).unwrap();
        assert_eq!(person, ron_parsed);
    }

    /// Test empty collections
    #[test]
    fn empty_collections() {
        let c = Collections {
            numbers: vec![],
            strings: vec![],
            nested: vec![],
        };

        let ron_out = ron::to_string(&c).unwrap();
        let ron2_parsed = Collections::from_ron(&ron_out).unwrap();
        assert_eq!(c, ron2_parsed);

        let ron2_out = c.to_ron().unwrap();
        let ron_parsed: Collections = ron::from_str(&ron2_out).unwrap();
        assert_eq!(c, ron_parsed);
    }

    /// Test numeric boundaries
    #[test]
    fn numeric_boundaries() {
        let nums = Numbers {
            i8_val: i8::MAX,
            i16_val: i16::MAX,
            i32_val: i32::MAX,
            i64_val: i64::MAX,
            u8_val: u8::MAX,
            u16_val: u16::MAX,
            u32_val: u32::MAX,
            u64_val: u64::MAX,
            f32_val: f32::MAX,
            f64_val: f64::MAX,
        };

        let ron_out = ron::to_string(&nums).unwrap();
        let ron2_parsed = Numbers::from_ron(&ron_out).unwrap();
        assert_eq!(nums, ron2_parsed);
    }

    /// Test negative number boundaries
    #[test]
    fn negative_boundaries() {
        let nums = Numbers {
            i8_val: i8::MIN,
            i16_val: i16::MIN,
            i32_val: i32::MIN,
            i64_val: i64::MIN,
            u8_val: 0,
            u16_val: 0,
            u32_val: 0,
            u64_val: 0,
            f32_val: f32::MIN,
            f64_val: f64::MIN,
        };

        let ron_out = ron::to_string(&nums).unwrap();
        let ron2_parsed = Numbers::from_ron(&ron_out).unwrap();
        assert_eq!(nums, ron2_parsed);
    }

    /// Test deeply nested structures
    #[test]
    fn deep_nesting() {
        let nested = Nested {
            inner: Inner {
                value: 1,
                name: "level1".into(),
            },
            list: vec![
                Inner {
                    value: 2,
                    name: "level2a".into(),
                },
                Inner {
                    value: 3,
                    name: "level2b".into(),
                },
            ],
        };

        let ron_out = ron::to_string(&nested).unwrap();
        let ron2_parsed = Nested::from_ron(&ron_out).unwrap();
        assert_eq!(nested, ron2_parsed);

        let ron2_out = nested.to_ron().unwrap();
        let ron_parsed: Nested = ron::from_str(&ron2_out).unwrap();
        assert_eq!(nested, ron_parsed);
    }
}

// ============================================================================
// Struct Syntax Verification
// ============================================================================

mod struct_syntax {
    use super::*;

    /// Verify both crates use the standard parenthesis syntax for structs.
    ///
    /// Standard RON: `Point(x: 1, y: 2)` - parentheses for named fields
    /// NOT: `Point { x: 1, y: 2 }` - braces are for maps
    #[test]
    fn both_crates_use_parenthesis_syntax() {
        let point = Point { x: 1, y: 2 };

        // Check ron crate output uses parentheses
        let ron_out = ron::to_string(&point).unwrap();
        assert!(
            ron_out.contains('(') && ron_out.contains(')'),
            "ron crate should use parentheses: {ron_out}"
        );
        assert!(
            !ron_out.starts_with("Point{") && !ron_out.contains("Point {"),
            "ron crate should not use braces for struct fields: {ron_out}"
        );

        // Check ron2 crate output uses parentheses
        let ron2_out = point.to_ron().unwrap();
        assert!(
            ron2_out.contains('(') && ron2_out.contains(')'),
            "ron2 crate should use parentheses: {ron2_out}"
        );
        assert!(
            !ron2_out.starts_with("Point{") && !ron2_out.contains("Point {"),
            "ron2 crate should not use braces for struct fields: {ron2_out}"
        );
    }

    /// Verify the exact output format matches expected RON syntax.
    #[test]
    fn ron2_produces_standard_syntax() {
        let point = Point { x: 1, y: 2 };
        let ron2_out = point.to_ron_with(&FormatConfig::minimal()).unwrap();

        // ron2 with Minimal format produces compact output: Point(x:1,y:2)
        assert_eq!(ron2_out, "Point(x:1,y:2)");
    }
}