File: enum.rs

package info (click to toggle)
rust-deser-hjson 2.2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 252 kB
  • sloc: makefile: 4
file content (138 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download
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
use {
    deser_hjson::from_str,
    serde:: Deserialize,
};

#[macro_use] mod common;

#[test]
fn test_enum() {
    #[derive(Deserialize, PartialEq, Debug)]
    enum E {
        Unit,
        Newtype(u32),
        Tuple(u32, u32),
        Struct { a: u32 },
    }

    let j = r#""Unit""#;
    let expected = E::Unit;
    assert_eq!(expected, from_str(j).unwrap());

    let j = r#"{Newtype:1}"#;
    let expected = E::Newtype(1);
    assert_eq!(expected, from_str(j).unwrap());

    let j = r#"
    {
        Tuple : [ # Tuple variant
            1
            2
        ]
    }
    "#;
    let expected = E::Tuple(1, 2);
    assert_eq!(expected, from_str(j).unwrap());

    let j = r#"
    {
        # this variant is explitely defined
        Struct: {a:1}
    }"#;
    let expected = E::Struct { a: 1 };
    assert_eq!(expected, from_str(j).unwrap());
}

#[test]
fn test_quoteless_tag_variant() {
    #[derive(Deserialize, PartialEq, Debug)]
    enum E {
        A,
        B,
    }
    let hjson = "B\n";
    assert_eq!(E::B, from_str(hjson).unwrap());
    let hjson = "B";
    assert_eq!(E::B, from_str(hjson).unwrap());
    #[derive(Deserialize, PartialEq, Debug)]
    struct S {
        e: E,
    }
    let hjson = r#"{
        e: B
    }"#;
    assert_eq!(S{e:E::B}, from_str(hjson).unwrap());
    let hjson = r#"{"e": "B"}"#;
    assert_eq!(S{e:E::B}, from_str(hjson).unwrap());
    let hjson = "{e:B}";
    assert_eq!(S{e:E::B}, from_str(hjson).unwrap());
}

#[test]
fn test_arr_struct_untagged() {
    // this enum is untagged: the variant is automatically recognized
    #[derive(Deserialize, PartialEq, Debug)]
    #[serde(untagged)]
    enum Untagged {
        Int(u16),
        Float(f32),
        String(String),
        Array(Vec<String>),
    }
    #[derive(Deserialize, PartialEq, Debug)]
    struct InnerThing {
        name: String,
        untagged: Untagged,
    }
    #[derive(Deserialize, PartialEq, Debug)]
    struct OuterThing {
        outer_name: String,
        items: Vec<InnerThing>,
    }
    let hjson = r#"
        {
            outer_name: the thing
            items: [
                {
                    name: first item
                    untagged: "xterm -e \"nvim {file}\""
                }
                {
                    name: "also an \"item\""
                    untagged: ["bla", "et", "bla"]
                }
                {
                    name: third
                    untagged: 4
                }
                {
                    name: fourth
                    untagged: 4.3
                }
            ]
        }
    "#;
    let outer_thing = OuterThing {
        outer_name: "the thing".to_owned(),
        items: vec![
            InnerThing {
                name: "first item".to_owned(),
                untagged: Untagged::String("xterm -e \"nvim {file}\"".to_string()),
            },
            InnerThing {
                name: r#"also an "item""#.to_owned(),
                untagged: Untagged::Array(vo!["bla", "et", "bla"]),
            },
            InnerThing {
                name: "third".to_owned(),
                untagged: Untagged::Int(4),
            },
            InnerThing {
                name: "fourth".to_owned(),
                untagged: Untagged::Float(4.3),
            },
        ],
    };
    assert_eq!(outer_thing, from_str::<OuterThing>(hjson).unwrap());
}