Debian Codegraph @

third_party/rust/serde_with/tests/serde_as/enum_map.rs

raw
1 use super::*;
2 use core::{fmt::Write as _, str::FromStr};
3 use serde_test::Configure;
4 use serde_with::EnumMap;
5 use std::net::IpAddr;
6
7 fn bytes_debug_readable(bytes: &[u8]) -> String {
8 let mut result = String::with_capacity(bytes.len() * 2);
9 for &byte in bytes {
10 match byte {
11 non_printable if !(0x20..0x7f).contains(&non_printable) => {
12 result.write_fmt(format_args!("\\x{byte:02x}")).unwrap();
13 }
14 b'\\' => result.push_str("\\\\"),
15 _ => {
16 result.push(char::from(byte));
17 }
18 }
19 }
20 result
21 }
22
23 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24 enum EnumValue {
25 Int(i32),
26 String(String),
27 Unit,
28 Tuple(i32, String, bool),
29 Struct {
30 a: i32,
31 b: String,
32 c: bool,
33 },
34 Ip(IpAddr, IpAddr),
35 #[serde(rename = "$value")]
36 Extra(serde_json::Value),
37 }
38
39 #[serde_as]
40 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
41 struct VecEnumValues {
42 #[serde_as(as = "EnumMap")]
43 vec: Vec<EnumValue>,
44 }
45
46 #[test]
47 fn json_round_trip() {
48 let values = VecEnumValues {
49 vec: vec![
50 EnumValue::Int(123),
51 EnumValue::String("FooBar".to_string()),
52 EnumValue::Int(456),
53 EnumValue::String("XXX".to_string()),
54 EnumValue::Unit,
55 EnumValue::Tuple(1, "Middle".to_string(), false),
56 EnumValue::Struct {
57 a: 666,
58 b: "BBB".to_string(),
59 c: true,
60 },
61 ],
62 };
63
64 let json = serde_json::to_string_pretty(&values).unwrap();
65 expect_test::expect![[r#"
66 {
67 "vec": {
68 "Int": 123,
69 "String": "FooBar",
70 "Int": 456,
71 "String": "XXX",
72 "Unit": null,
73 "Tuple": [
74 1,
75 "Middle",
76 false
77 ],
78 "Struct": {
79 "a": 666,
80 "b": "BBB",
81 "c": true
82 }
83 }
84 }"#]]
85 .assert_eq(&json);
86 let deser_values: VecEnumValues = serde_json::from_str(&json).unwrap();
87 assert_eq!(values, deser_values);
88 }
89
90 #[test]
91 fn ron_serialize() {
92 let values = VecEnumValues {
93 vec: vec![
94 EnumValue::Int(123),
95 EnumValue::String("FooBar".to_string()),
96 EnumValue::Int(456),
97 EnumValue::String("XXX".to_string()),
98 EnumValue::Unit,
99 EnumValue::Tuple(1, "Middle".to_string(), false),
100 EnumValue::Struct {
101 a: 666,
102 b: "BBB".to_string(),
103 c: true,
104 },
105 ],
106 };
107
108 let pretty_config = ron::ser::PrettyConfig::new().new_line("\n".into());
109 let ron = ron::ser::to_string_pretty(&values, pretty_config).unwrap();
110 expect_test::expect![[r#"
111 (
112 vec: {
113 "Int": 123,
114 "String": "FooBar",
115 "Int": 456,
116 "String": "XXX",
117 "Unit": (),
118 "Tuple": (1, "Middle", false),
119 "Struct": (
120 a: 666,
121 b: "BBB",
122 c: true,
123 ),
124 },
125 )"#]]
126 .assert_eq(&ron);
127 // TODO deserializing a Strings as an Identifier seems unsupported
128 let deser_values: ron::Value = ron::de::from_str(&ron).unwrap();
129 expect_test::expect![[r#"
130 Map(
131 Map(
132 {
133 String(
134 "vec",
135 ): Map(
136 Map(
137 {
138 String(
139 "Int",
140 ): Number(
141 Integer(
142 456,
143 ),
144 ),
145 String(
146 "String",
147 ): String(
148 "XXX",
149 ),
150 String(
151 "Struct",
152 ): Map(
153 Map(
154 {
155 String(
156 "a",
157 ): Number(
158 Integer(
159 666,
160 ),
161 ),
162 String(
163 "b",
164 ): String(
165 "BBB",
166 ),
167 String(
168 "c",
169 ): Bool(
170 true,
171 ),
172 },
173 ),
174 ),
175 String(
176 "Tuple",
177 ): Seq(
178 [
179 Number(
180 Integer(
181 1,
182 ),
183 ),
184 String(
185 "Middle",
186 ),
187 Bool(
188 false,
189 ),
190 ],
191 ),
192 String(
193 "Unit",
194 ): Unit,
195 },
196 ),
197 ),
198 },
199 ),
200 )
201 "#]]
202 .assert_debug_eq(&deser_values);
203 }
204
205 #[test]
206 fn xml_round_trip() {
207 let values = VecEnumValues {
208 vec: vec![
209 EnumValue::Int(123),
210 EnumValue::String("FooBar".to_string()),
211 EnumValue::Int(456),
212 EnumValue::String("XXX".to_string()),
213 EnumValue::Unit,
214 // serialize_tuple and variants are not supported by XML
215 // EnumValue::Tuple(1, "Middle".to_string(), false),
216 // Cannot be deserialized. It serializes to:
217 // <Struct><EnumValue><a>666</a><b>BBB</b><c>true</c></EnumValue></Struct>
218 // EnumValue::Struct {
219 // a: 666,
220 // b: "BBB".to_string(),
221 // c: true,
222 // },
223 ],
224 };
225
226 let xml = serde_xml_rs::to_string(&values).unwrap();
227 expect_test::expect![[r#"<?xml version="1.0" encoding="UTF-8"?><VecEnumValues><vec><Int>123</Int><String>FooBar</String><Int>456</Int><String>XXX</String><Unit /></vec></VecEnumValues>"#]]
228 .assert_eq(&xml);
229 let deser_values: VecEnumValues = serde_xml_rs::from_str(&xml).unwrap();
230 assert_eq!(values, deser_values);
231 }
232
233 #[test]
234 fn serde_test_round_trip() {
235 let values = VecEnumValues {
236 vec: vec![
237 EnumValue::Int(123),
238 EnumValue::String("FooBar".to_string()),
239 EnumValue::Int(456),
240 EnumValue::String("XXX".to_string()),
241 EnumValue::Unit,
242 EnumValue::Tuple(1, "Middle".to_string(), false),
243 EnumValue::Struct {
244 a: 666,
245 b: "BBB".to_string(),
246 c: true,
247 },
248 ],
249 };
250
251 use serde_test::Token::*;
252 serde_test::assert_tokens(
253 &values.readable(),
254 &[
255 Struct {
256 name: "VecEnumValues",
257 len: 1,
258 },
259 Str("vec"),
260 Map {
261 len: Option::Some(7),
262 },
263 Str("Int"),
264 I32(123),
265 Str("String"),
266 Str("FooBar"),
267 Str("Int"),
268 I32(456),
269 Str("String"),
270 Str("XXX"),
271 Str("Unit"),
272 Unit,
273 Str("Tuple"),
274 TupleStruct {
275 name: "EnumValue",
276 len: 3,
277 },
278 I32(1),
279 Str("Middle"),
280 Bool(false),
281 TupleStructEnd,
282 Str("Struct"),
283 Struct {
284 name: "EnumValue",
285 len: 3,
286 },
287 Str("a"),
288 I32(666),
289 Str("b"),
290 Str("BBB"),
291 Str("c"),
292 Bool(true),
293 StructEnd,
294 MapEnd,
295 StructEnd,
296 ],
297 );
298 }
299
300 #[test]
301 fn serde_test_round_trip_human_readable() {
302 let values = VecEnumValues {
303 vec: vec![EnumValue::Ip(
304 IpAddr::from_str("127.0.0.1").unwrap(),
305 IpAddr::from_str("::7777:dead:beef").unwrap(),
306 )],
307 };
308
309 use serde_test::Token::*;
310 serde_test::assert_tokens(
311 &values.clone().readable(),
312 &[
313 Struct {
314 name: "VecEnumValues",
315 len: 1,
316 },
317 Str("vec"),
318 Map {
319 len: Option::Some(1),
320 },
321 Str("Ip"),
322 TupleStruct {
323 name: "EnumValue",
324 len: 2,
325 },
326 Str("127.0.0.1"),
327 Str("::7777:dead:beef"),
328 TupleStructEnd,
329 MapEnd,
330 StructEnd,
331 ],
332 );
333
334 serde_test::assert_tokens(
335 &values.compact(),
336 &[
337 Struct {
338 name: "VecEnumValues",
339 len: 1,
340 },
341 Str("vec"),
342 Map {
343 len: Option::Some(1),
344 },
345 Str("Ip"),
346 TupleStruct {
347 name: "EnumValue",
348 len: 2,
349 },
350 NewtypeVariant {
351 name: "IpAddr",
352 variant: "V4",
353 },
354 Tuple { len: 4 },
355 U8(127),
356 U8(0),
357 U8(0),
358 U8(1),
359 TupleEnd,
360 NewtypeVariant {
361 name: "IpAddr",
362 variant: "V6",
363 },
364 Tuple { len: 16 },
365 U8(0),
366 U8(0),
367 U8(0),
368 U8(0),
369 U8(0),
370 U8(0),
371 U8(0),
372 U8(0),
373 U8(0),
374 U8(0),
375 U8(0x77),
376 U8(0x77),
377 U8(0xde),
378 U8(0xad),
379 U8(0xbe),
380 U8(0xef),
381 TupleEnd,
382 TupleStructEnd,
383 MapEnd,
384 StructEnd,
385 ],
386 );
387 }
388
389 // Bincode does not support Deserializer::deserialize_identifier
390 // https://github.com/bincode-org/bincode/blob/e0ac3245162ba668ba04591897dd88ff5b3096b8/src/de/mod.rs#L442
391
392 #[test]
393 fn rmp_round_trip() {
394 let values = VecEnumValues {
395 vec: vec![
396 EnumValue::Int(123),
397 EnumValue::String("FooBar".to_string()),
398 EnumValue::Int(456),
399 EnumValue::String("XXX".to_string()),
400 EnumValue::Unit,
401 EnumValue::Tuple(1, "Middle".to_string(), false),
402 EnumValue::Struct {
403 a: 666,
404 b: "BBB".to_string(),
405 c: true,
406 },
407 EnumValue::Ip(
408 IpAddr::from_str("127.0.0.1").unwrap(),
409 IpAddr::from_str("::7777:dead:beef").unwrap(),
410 ),
411 ],
412 };
413
414 let rmp = rmp_serde::to_vec(&values).unwrap();
415 expect_test::expect![[r"\x91\x88\xa3Int{\xa6String\xa6FooBar\xa3Int\xcd\x01\xc8\xa6String\xa3XXX\xa4Unit\xc0\xa5Tuple\x93\x01\xa6Middle\xc2\xa6Struct\x93\xcd\x02\x9a\xa3BBB\xc3\xa2Ip\x92\x81\xa2V4\x94\x7f\x00\x00\x01\x81\xa2V6\xdc\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00ww\xcc\xde\xcc\xad\xcc\xbe\xcc\xef"]]
416 .assert_eq(&bytes_debug_readable(&rmp));
417 let deser_values: VecEnumValues = rmp_serde::from_read(&*rmp).unwrap();
418 assert_eq!(values, deser_values);
419 }
420
421 #[test]
422 fn yaml_round_trip() {
423 // Duplicate enum variants do not work with YAML
424 let values = VecEnumValues {
425 vec: vec![
426 EnumValue::Int(123),
427 EnumValue::String("FooBar".to_string()),
428 // EnumValue::Int(456),
429 // EnumValue::String("XXX".to_string()),
430 EnumValue::Unit,
431 EnumValue::Tuple(1, "Middle".to_string(), false),
432 EnumValue::Struct {
433 a: 666,
434 b: "BBB".to_string(),
435 c: true,
436 },
437 ],
438 };
439
440 let yaml = serde_yaml::to_string(&values).unwrap();
441 expect_test::expect![[r#"
442 vec:
443 Int: 123
444 String: FooBar
445 Unit: null
446 Tuple:
447 - 1
448 - Middle
449 - false
450 Struct:
451 a: 666
452 b: BBB
453 c: true
454 "#]]
455 .assert_eq(&yaml);
456 let deser_values: VecEnumValues = serde_yaml::from_str(&yaml).unwrap();
457 assert_eq!(values, deser_values);
458 }