Debian Codegraph @

tests/integration/enums_untagged_variant.rs

raw symbols SCIP
1 use crate::prelude::*;
2 use std::collections::BTreeMap;
3
4 #[derive(JsonSchema, Deserialize, Serialize)]
5 struct UnitStruct;
6
7 #[derive(JsonSchema, Deserialize, Serialize, Default)]
8 struct Struct {
9 foo: i32,
10 bar: bool,
11 }
12
13 #[derive(JsonSchema, Deserialize, Serialize)]
14 #[serde(rename_all = "camelCase")]
15 enum External {
16 TaggedUnitOne,
17 TaggedStruct {
18 baz: i32,
19 foobar: bool,
20 },
21 #[serde(untagged)]
22 UnitOne,
23 #[serde(untagged)]
24 UnitStructNewType(UnitStruct),
25 #[serde(untagged)]
26 StructNewType(Struct),
27 #[serde(untagged)]
28 Struct {
29 baz: i32,
30 foobar: bool,
31 },
32 #[serde(untagged)]
33 Tuple(i32, bool),
34 #[serde(untagged)]
35 StringMap(BTreeMap<String, String>),
36 }
37
38 impl External {
39 fn values() -> impl IntoIterator<Item = Self> {
40 [
41 Self::TaggedUnitOne,
42 Self::TaggedStruct {
43 baz: 123,
44 foobar: true,
45 },
46 Self::UnitOne,
47 Self::StringMap(
48 [("hello".to_owned(), "world".to_owned())]
49 .into_iter()
50 .collect(),
51 ),
52 Self::UnitStructNewType(UnitStruct),
53 Self::StructNewType(Struct {
54 foo: 123,
55 bar: true,
56 }),
57 Self::Struct {
58 baz: 123,
59 foobar: true,
60 },
61 Self::Tuple(456, false),
62 ]
63 }
64 }
65
66 #[derive(JsonSchema, Deserialize, Serialize)]
67 #[serde(tag = "tag")]
68 enum Internal {
69 TaggedUnitOne,
70 TaggedStruct {
71 baz: i32,
72 foobar: bool,
73 },
74 #[serde(untagged)]
75 UnitOne,
76 #[serde(untagged)]
77 UnitStructNewType(UnitStruct),
78 #[serde(untagged)]
79 StructNewType(Struct),
80 #[serde(untagged)]
81 Struct {
82 baz: i32,
83 foobar: bool,
84 },
85 // Internally-tagged enums don't support tuple variants
86 // #[serde(untagged)]
87 // Tuple(i32, bool),
88 #[serde(untagged)]
89 StringMap(BTreeMap<String, String>),
90 }
91
92 impl Internal {
93 fn values() -> impl IntoIterator<Item = Self> {
94 [
95 Self::TaggedUnitOne,
96 Self::TaggedStruct {
97 baz: 123,
98 foobar: true,
99 },
100 Self::UnitOne,
101 Self::StringMap(
102 [("hello".to_owned(), "world".to_owned())]
103 .into_iter()
104 .collect(),
105 ),
106 Self::UnitStructNewType(UnitStruct),
107 Self::StructNewType(Struct {
108 foo: 123,
109 bar: true,
110 }),
111 Self::Struct {
112 baz: 123,
113 foobar: true,
114 },
115 // Self::Tuple(456, false),
116 ]
117 }
118 }
119
120 #[derive(JsonSchema, Deserialize, Serialize)]
121 #[serde(tag = "tag", content = "content")]
122 enum Adjacent {
123 TaggedUnitOne,
124 TaggedStruct {
125 baz: i32,
126 foobar: bool,
127 },
128 #[serde(untagged)]
129 UnitOne,
130 #[serde(untagged)]
131 UnitStructNewType(UnitStruct),
132 #[serde(untagged)]
133 StructNewType(Struct),
134 #[serde(untagged)]
135 Struct {
136 baz: i32,
137 foobar: bool,
138 },
139 #[serde(untagged)]
140 Tuple(i32, bool),
141 #[serde(untagged)]
142 StringMap(BTreeMap<String, String>),
143 }
144
145 impl Adjacent {
146 fn values() -> impl IntoIterator<Item = Self> {
147 [
148 Self::TaggedUnitOne,
149 Self::TaggedStruct {
150 baz: 123,
151 foobar: true,
152 },
153 Self::UnitOne,
154 Self::StringMap(
155 [("hello".to_owned(), "world".to_owned())]
156 .into_iter()
157 .collect(),
158 ),
159 Self::UnitStructNewType(UnitStruct),
160 Self::StructNewType(Struct {
161 foo: 123,
162 bar: true,
163 }),
164 Self::Struct {
165 baz: 123,
166 foobar: true,
167 },
168 Self::Tuple(456, false),
169 ]
170 }
171 }
172
173 #[test]
174 fn externally_tagged_enum() {
175 test!(External)
176 .assert_snapshot()
177 .assert_allows_ser_roundtrip(External::values())
178 .assert_matches_de_roundtrip(arbitrary_values());
179 }
180
181 #[test]
182 fn internally_tagged_enum() {
183 test!(Internal)
184 .assert_snapshot()
185 .assert_allows_ser_roundtrip(Internal::values())
186 .assert_matches_de_roundtrip(arbitrary_values_except(
187 Value::is_array,
188 "internally tagged enums can technically be deserialized from sequences, but that's not intended to be used via JSON, so schemars ignores it",
189 ));
190 }
191
192 #[test]
193 fn adjacently_tagged_enum() {
194 test!(Adjacent)
195 .assert_snapshot()
196 .assert_allows_ser_roundtrip(Adjacent::values())
197 .assert_matches_de_roundtrip(arbitrary_values());
198 }