1
use serde::{Deserialize, Serialize};
2

            
3
#[test]
4
4
fn nested_untagged_enum() {
5
    // Contributed by @caelunshun in https://github.com/ron-rs/ron/issues/217
6

            
7
8
    #[derive(Debug, PartialEq, Deserialize)]
8
    struct Root {
9
        value: Either,
10
    }
11

            
12
4
    #[derive(Debug, PartialEq, Deserialize)]
13
    #[serde(transparent)]
14
    struct Newtype(Either);
15

            
16
    #[derive(Debug, PartialEq, Deserialize)]
17
    #[serde(untagged)]
18
    enum Either {
19
        One(One),
20
        Two(Two),
21
    }
22

            
23
32
    #[derive(Debug, PartialEq, Deserialize)]
24
    enum One {
25
        OneA,
26
        OneB,
27
        OneC,
28
    }
29

            
30
    #[derive(Debug, PartialEq, Deserialize)]
31
    enum Two {
32
        TwoA,
33
        TwoB,
34
        TwoC,
35
    }
36

            
37
4
    assert_eq!(ron::de::from_str("OneA"), Ok(One::OneA));
38
4
    assert_eq!(ron::de::from_str("OneA"), Ok(Either::One(One::OneA)));
39
4
    assert_eq!(
40
4
        ron::de::from_str("(value: OneA)"),
41
4
        Ok(Root {
42
4
            value: Either::One(One::OneA)
43
4
        })
44
4
    );
45
4
    assert_eq!(
46
4
        ron::de::from_str("OneA"),
47
4
        Ok(Newtype(Either::One(One::OneA)))
48
4
    );
49
4
}
50

            
51
#[test]
52
4
fn untagged_enum_of_enum_list() {
53
    // Contributed by @joonazan in https://github.com/ron-rs/ron/issues/217
54

            
55
8
    #[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
56
    pub enum UnitType {
57
        Explorer,
58
    }
59

            
60
    #[derive(Serialize, Deserialize, Debug, PartialEq)]
61
    #[serde(untagged)]
62
    enum CardTextNumberFlat {
63
        JustNum(u8),
64
        RangeW(Range_),
65
        CountUnitW(CountUnit_),
66
        PowerCardsPlayedW(PowerCardsPlayed),
67
    }
68

            
69
    #[allow(non_snake_case)]
70
16
    #[derive(Serialize, Deserialize, Debug, PartialEq)]
71
    struct Range_ {
72
        Range: u8,
73
    }
74
8
    #[derive(Serialize, Deserialize, Debug, PartialEq)]
75
    #[allow(non_snake_case)]
76
    struct CountUnit_ {
77
        CountUnit: Vec<UnitType>,
78
    }
79
    #[derive(Serialize, Deserialize, Debug, PartialEq)]
80
    struct PowerCardsPlayed;
81

            
82
4
    let units = CardTextNumberFlat::CountUnitW(CountUnit_ {
83
4
        CountUnit: vec![UnitType::Explorer],
84
4
    });
85
4
    let range = CardTextNumberFlat::RangeW(Range_ { Range: 1 });
86
4

            
87
4
    let units_ron: String = ron::to_string(&units).unwrap();
88
4
    let range_ron = ron::to_string(&range).unwrap();
89
4

            
90
4
    assert_eq!(units_ron, "(CountUnit:[Explorer])");
91
4
    assert_eq!(range_ron, "(Range:1)");
92

            
93
4
    let units_de: CardTextNumberFlat = ron::from_str(&units_ron).unwrap();
94
4
    let range_de: CardTextNumberFlat = ron::from_str(&range_ron).unwrap();
95
4

            
96
4
    assert_eq!(units_de, units);
97
4
    assert_eq!(range_de, range);
98
4
}