Debian Codegraph @

third_party/rust/ron/tests/502_known_bugs.rs

raw
1 use std::collections::{BTreeMap, HashMap};
2
3 use ron::{error::Position, error::SpannedError, extensions::Extensions, ser::PrettyConfig, Error};
4 use serde::{Deserialize, Serialize};
5
6 #[test]
7 fn struct_names_inside_internally_tagged() {
8 #[derive(PartialEq, Debug, Serialize, Deserialize)]
9 struct A {
10 hi: i32,
11 }
12
13 #[derive(PartialEq, Debug, Serialize, Deserialize)]
14 #[serde(tag = "tag")]
15 enum InternallyTagged {
16 B { ho: i32, a: A },
17 }
18
19 assert_eq!(
20 check_roundtrip(
21 &InternallyTagged::B {
22 ho: 24,
23 a: A { hi: 42 }
24 },
25 PrettyConfig::default()
26 ),
27 Ok(())
28 );
29 assert_eq!(
30 check_roundtrip(
31 &InternallyTagged::B {
32 ho: 24,
33 a: A { hi: 42 }
34 },
35 PrettyConfig::default().struct_names(true)
36 ),
37 Err(Err(SpannedError {
38 code: Error::MissingStructField {
39 field: "hi",
40 outer: None
41 },
42 position: Position { line: 7, col: 2 }
43 })),
44 );
45 }
46
47 #[test]
48 fn struct_names_inside_adjacently_tagged() {
49 #[derive(PartialEq, Debug, Serialize, Deserialize)]
50 struct A {
51 hi: i32,
52 }
53
54 #[derive(PartialEq, Debug, Serialize, Deserialize)]
55 #[serde(tag = "tag", content = "content")]
56 enum AdjacentlyTagged {
57 B { ho: i32, a: A },
58 }
59
60 assert_eq!(
61 check_roundtrip(
62 &AdjacentlyTagged::B {
63 ho: 24,
64 a: A { hi: 42 }
65 },
66 PrettyConfig::default()
67 ),
68 Ok(())
69 );
70 assert_eq!(
71 check_roundtrip(
72 &AdjacentlyTagged::B {
73 ho: 24,
74 a: A { hi: 42 }
75 },
76 PrettyConfig::default().struct_names(true)
77 ),
78 Ok(()),
79 );
80 assert_eq!(
81 ron::from_str::<AdjacentlyTagged>(
82 "AdjacentlyTagged(tag: B, content: B(ho: 24, a: A(hi: 42)))"
83 ),
84 Ok(AdjacentlyTagged::B {
85 ho: 24,
86 a: A { hi: 42 }
87 }),
88 );
89 assert_eq!(
90 ron::from_str::<AdjacentlyTagged>(
91 "AdjacentlyTagged(content: B(ho: 24, a: A(hi: 42)), tag: B)"
92 ),
93 Err(SpannedError {
94 code: Error::MissingStructField {
95 field: "ho",
96 outer: Some(String::from("AdjacentlyTagged"))
97 },
98 position: Position { line: 1, col: 58 }
99 }),
100 );
101 }
102
103 #[test]
104 fn struct_names_inside_untagged() {
105 #[derive(PartialEq, Debug, Serialize, Deserialize)]
106 struct A {
107 hi: i32,
108 }
109
110 #[derive(PartialEq, Debug, Serialize, Deserialize)]
111 #[serde(untagged)]
112 enum Untagged {
113 B { ho: i32, a: A },
114 }
115
116 assert_eq!(
117 check_roundtrip(
118 &Untagged::B {
119 ho: 24,
120 a: A { hi: 42 }
121 },
122 PrettyConfig::default()
123 ),
124 Ok(())
125 );
126 assert_eq!(
127 check_roundtrip(
128 &Untagged::B {
129 ho: 24,
130 a: A { hi: 42 }
131 },
132 PrettyConfig::default().struct_names(true)
133 ),
134 Err(Err(SpannedError {
135 code: Error::Message(String::from(
136 "data did not match any variant of untagged enum Untagged"
137 )),
138 position: Position { line: 6, col: 2 }
139 })),
140 );
141 }
142
143 #[test]
144 fn struct_names_inside_flatten_struct() {
145 #[derive(PartialEq, Debug, Serialize, Deserialize)]
146 struct A {
147 hi: i32,
148 }
149
150 #[derive(PartialEq, Debug, Serialize, Deserialize)]
151 struct B {
152 a: A,
153 }
154
155 #[derive(PartialEq, Debug, Serialize, Deserialize)]
156 struct FlattenedStruct {
157 ho: i32,
158 #[serde(flatten)]
159 a: B,
160 }
161
162 assert_eq!(
163 check_roundtrip(
164 &FlattenedStruct {
165 ho: 24,
166 a: B { a: A { hi: 42 } }
167 },
168 PrettyConfig::default()
169 ),
170 Ok(())
171 );
172 assert_eq!(
173 check_roundtrip(
174 &FlattenedStruct {
175 ho: 24,
176 a: B { a: A { hi: 42 } }
177 },
178 PrettyConfig::default().struct_names(true)
179 ),
180 Err(Err(SpannedError {
181 code: Error::MissingStructField {
182 field: "hi",
183 outer: None
184 },
185 position: Position { line: 6, col: 1 }
186 })),
187 );
188 }
189
190 #[test]
191 fn struct_names_inside_flatten_struct_variant() {
192 #[derive(PartialEq, Debug, Serialize, Deserialize)]
193 struct A {
194 hi: i32,
195 }
196
197 #[derive(PartialEq, Debug, Serialize, Deserialize)]
198 struct B {
199 a: A,
200 }
201
202 #[derive(PartialEq, Debug, Serialize, Deserialize)]
203 enum FlattenedStructVariant {
204 C {
205 ho: i32,
206 #[serde(flatten)]
207 a: B,
208 },
209 }
210
211 assert_eq!(
212 check_roundtrip(
213 &FlattenedStructVariant::C {
214 ho: 24,
215 a: B { a: A { hi: 42 } }
216 },
217 PrettyConfig::default()
218 ),
219 Ok(())
220 );
221 assert_eq!(
222 check_roundtrip(
223 &FlattenedStructVariant::C {
224 ho: 24,
225 a: B { a: A { hi: 42 } }
226 },
227 PrettyConfig::default().struct_names(true)
228 ),
229 Err(Err(SpannedError {
230 code: Error::MissingStructField {
231 field: "hi",
232 outer: Some(String::from("C"))
233 },
234 position: Position { line: 6, col: 1 }
235 })),
236 );
237 }
238
239 #[test]
240 fn implicit_some_inside_internally_tagged() {
241 #[derive(PartialEq, Debug, Serialize, Deserialize)]
242 struct A {
243 hi: Option<Option<()>>,
244 }
245
246 #[derive(PartialEq, Debug, Serialize, Deserialize)]
247 #[serde(tag = "tag")]
248 enum InternallyTagged {
249 B { ho: i32, a: A },
250 }
251
252 assert_eq!(
253 check_roundtrip(
254 &InternallyTagged::B {
255 ho: 24,
256 a: A { hi: Some(Some(())) }
257 },
258 PrettyConfig::default()
259 ),
260 Ok(())
261 );
262 assert_eq!(
263 check_roundtrip(
264 &InternallyTagged::B {
265 ho: 24,
266 a: A { hi: Some(Some(())) }
267 },
268 PrettyConfig::default().extensions(Extensions::IMPLICIT_SOME)
269 ),
270 Err(Ok(Error::Message(String::from("ROUNDTRIP error: B { ho: 24, a: A { hi: Some(Some(())) } } != B { ho: 24, a: A { hi: None } }"))))
271 );
272 }
273
274 #[test]
275 fn implicit_some_inside_adjacently_tagged() {
276 #[derive(PartialEq, Debug, Serialize, Deserialize)]
277 struct Unit;
278
279 #[derive(PartialEq, Debug, Serialize, Deserialize)]
280 struct A {
281 hi: Option<Option<Unit>>,
282 }
283
284 #[derive(PartialEq, Debug, Serialize, Deserialize)]
285 #[serde(tag = "tag", content = "content")]
286 enum AdjacentlyTagged {
287 B { ho: i32, a: A },
288 }
289
290 assert_eq!(
291 check_roundtrip(
292 &AdjacentlyTagged::B {
293 ho: 24,
294 a: A {
295 hi: Some(Some(Unit))
296 }
297 },
298 PrettyConfig::default()
299 ),
300 Ok(())
301 );
302 assert_eq!(
303 check_roundtrip(
304 &AdjacentlyTagged::B {
305 ho: 24,
306 a: A {
307 hi: Some(Some(Unit))
308 }
309 },
310 PrettyConfig::default().extensions(Extensions::IMPLICIT_SOME)
311 ),
312 Ok(()),
313 );
314 assert_eq!(
315 ron::from_str::<AdjacentlyTagged>(
316 "#![enable(implicit_some)] (tag: B, content: (ho: 24, a: A(hi: ())))"
317 ),
318 Ok(AdjacentlyTagged::B {
319 ho: 24,
320 a: A {
321 hi: Some(Some(Unit))
322 }
323 }),
324 );
325 assert_eq!(
326 ron::from_str::<AdjacentlyTagged>(
327 "#![enable(implicit_some)] (content: (ho: 24, a: A(hi: ())), tag: B)"
328 ),
329 Ok(AdjacentlyTagged::B {
330 ho: 24,
331 a: A { hi: None } // THIS IS WRONG
332 }),
333 );
334 }
335
336 #[test]
337 fn implicit_some_inside_untagged() {
338 #[derive(PartialEq, Debug, Serialize, Deserialize)]
339 struct A {
340 hi: Option<Option<()>>,
341 }
342
343 #[derive(PartialEq, Debug, Serialize, Deserialize)]
344 #[serde(untagged)]
345 enum Untagged {
346 B { ho: i32, a: A },
347 }
348
349 assert_eq!(
350 check_roundtrip(
351 &Untagged::B {
352 ho: 24,
353 a: A { hi: Some(Some(())) }
354 },
355 PrettyConfig::default()
356 ),
357 Ok(())
358 );
359 assert_eq!(
360 check_roundtrip(
361 &Untagged::B {
362 ho: 24,
363 a: A { hi: Some(Some(())) }
364 },
365 PrettyConfig::default().extensions(Extensions::IMPLICIT_SOME)
366 ),
367 Err(Ok(Error::Message(String::from(
368 "ROUNDTRIP error: B { ho: 24, a: A { hi: Some(Some(())) } } != B { ho: 24, a: A { hi: None } }"
369 )))),
370 );
371 }
372
373 #[test]
374 fn implicit_some_inside_flatten_struct() {
375 #[derive(PartialEq, Debug, Serialize, Deserialize)]
376 #[serde(untagged)]
377 enum Untagged {
378 Unit,
379 }
380
381 #[derive(PartialEq, Debug, Serialize, Deserialize)]
382 struct A {
383 hi: Option<Option<Untagged>>,
384 }
385
386 #[derive(PartialEq, Debug, Serialize, Deserialize)]
387 struct B {
388 a: A,
389 }
390
391 #[derive(PartialEq, Debug, Serialize, Deserialize)]
392 struct FlattenedStruct {
393 ho: i32,
394 #[serde(flatten)]
395 a: B,
396 }
397
398 assert_eq!(
399 check_roundtrip(
400 &FlattenedStruct {
401 ho: 24,
402 a: B {
403 a: A {
404 hi: Some(Some(Untagged::Unit))
405 }
406 }
407 },
408 PrettyConfig::default()
409 ),
410 Ok(())
411 );
412 assert_eq!(
413 check_roundtrip(
414 &FlattenedStruct {
415 ho: 24,
416 a: B {
417 a: A { hi: Some(Some(Untagged::Unit)) }
418 }
419 },
420 PrettyConfig::default().extensions(Extensions::IMPLICIT_SOME)
421 ),
422 Err(Ok(Error::Message(String::from("ROUNDTRIP error: FlattenedStruct { ho: 24, a: B { a: A { hi: Some(Some(Unit)) } } } != FlattenedStruct { ho: 24, a: B { a: A { hi: None } } }"))))
423 );
424 }
425
426 #[test]
427 fn implicit_some_inside_flatten_struct_variant() {
428 #[derive(PartialEq, Debug, Serialize, Deserialize)]
429 struct A {
430 hi: Option<Option<[u8; 0]>>,
431 }
432
433 #[derive(PartialEq, Debug, Serialize, Deserialize)]
434 struct B {
435 a: A,
436 }
437
438 #[derive(PartialEq, Debug, Serialize, Deserialize)]
439 enum FlattenedStructVariant {
440 C {
441 ho: i32,
442 #[serde(flatten)]
443 a: B,
444 },
445 }
446
447 assert_eq!(
448 check_roundtrip(
449 &FlattenedStructVariant::C {
450 ho: 24,
451 a: B {
452 a: A { hi: Some(Some([])) }
453 }
454 },
455 PrettyConfig::default()
456 ),
457 Err(Err(SpannedError {
458 code: Error::InvalidValueForType {
459 expected: String::from("an empty array"),
460 found: String::from("a unit value")
461 },
462 position: Position { line: 6, col: 1 }
463 }))
464 );
465 assert_eq!(
466 check_roundtrip(
467 &FlattenedStructVariant::C {
468 ho: 24,
469 a: B {
470 a: A { hi: Some(Some([])) }
471 }
472 },
473 PrettyConfig::default().extensions(Extensions::IMPLICIT_SOME)
474 ),
475 Err(Ok(Error::Message(String::from("ROUNDTRIP error: C { ho: 24, a: B { a: A { hi: Some(Some([])) } } } != C { ho: 24, a: B { a: A { hi: None } } }"))))
476 );
477 }
478
479 #[test]
480 fn newtype_inside_internally_tagged() {
481 #[derive(PartialEq, Debug, Serialize, Deserialize)]
482 struct A(i32);
483
484 #[derive(PartialEq, Debug, Serialize, Deserialize)]
485 #[serde(tag = "tag")]
486 enum InternallyTagged {
487 B { ho: i32, a: A },
488 }
489
490 // NOTE:
491 // 1. ron is correctly collected into Content, newtype is a one-seq here
492 // 2. newtype asks ContentDeserializer for newtype
493 // 3. ContentDeserializer forwards any value to visit_newtype_struct
494 // https://github.com/serde-rs/serde/blob/8c4aad3a59515f7b779f764d5e16d6bae297ab7f/serde/src/private/de.rs#L1347-L1359
495
496 assert_eq!(
497 check_roundtrip(
498 &InternallyTagged::B { ho: 24, a: A(42) },
499 PrettyConfig::default()
500 ),
501 Err(Err(SpannedError {
502 code: Error::InvalidValueForType {
503 expected: String::from("i32"),
504 found: String::from("a sequence")
505 },
506 position: Position { line: 5, col: 2 }
507 }))
508 );
509 }
510
511 #[test]
512 fn newtype_inside_adjacently_tagged() {
513 #[derive(PartialEq, Debug, Serialize, Deserialize)]
514 struct A(i32);
515
516 #[derive(PartialEq, Debug, Serialize, Deserialize)]
517 #[serde(tag = "tag", content = "content")]
518 enum AdjacentlyTagged {
519 B { ho: i32, a: A },
520 }
521
522 assert_eq!(
523 check_roundtrip(
524 &AdjacentlyTagged::B { ho: 24, a: A(42) },
525 PrettyConfig::default()
526 ),
527 Ok(())
528 );
529 assert_eq!(
530 ron::from_str::<AdjacentlyTagged>("(tag: B, content: (ho: 24, a: (42)))"),
531 Ok(AdjacentlyTagged::B { ho: 24, a: A(42) }),
532 );
533 assert_eq!(
534 ron::from_str::<AdjacentlyTagged>("(content: (ho: 24, a: (42)), tag: B)"),
535 Err(SpannedError {
536 code: Error::InvalidValueForType {
537 expected: String::from("i32"),
538 found: String::from("a sequence")
539 },
540 position: Position { line: 1, col: 36 }
541 })
542 );
543 }
544
545 #[test]
546 fn newtype_inside_untagged() {
547 #[derive(PartialEq, Debug, Serialize, Deserialize)]
548 struct A(i32);
549
550 #[derive(PartialEq, Debug, Serialize, Deserialize)]
551 #[serde(untagged)]
552 enum Untagged {
553 B { ho: i32, a: A },
554 }
555
556 assert_eq!(
557 check_roundtrip(&Untagged::B { ho: 24, a: A(42) }, PrettyConfig::default()),
558 Err(Err(SpannedError {
559 code: Error::Message(String::from(
560 "data did not match any variant of untagged enum Untagged"
561 )),
562 position: Position { line: 4, col: 2 }
563 }))
564 );
565 }
566
567 #[test]
568 fn newtype_inside_flatten_struct() {
569 #[derive(PartialEq, Debug, Serialize, Deserialize)]
570 struct A(i32);
571
572 #[derive(PartialEq, Debug, Serialize, Deserialize)]
573 struct B {
574 a: A,
575 }
576
577 #[derive(PartialEq, Debug, Serialize, Deserialize)]
578 struct FlattenedStruct {
579 ho: i32,
580 #[serde(flatten)]
581 a: B,
582 }
583
584 assert_eq!(
585 check_roundtrip(
586 &FlattenedStruct {
587 ho: 24,
588 a: B { a: A(42) }
589 },
590 PrettyConfig::default()
591 ),
592 Err(Err(SpannedError {
593 code: Error::InvalidValueForType {
594 expected: String::from("i32"),
595 found: String::from("a sequence")
596 },
597 position: Position { line: 4, col: 1 }
598 }))
599 );
600 }
601
602 #[test]
603 fn newtype_inside_flatten_struct_variant() {
604 #[derive(PartialEq, Debug, Serialize, Deserialize)]
605 struct A(i32);
606
607 #[derive(PartialEq, Debug, Serialize, Deserialize)]
608 struct B {
609 a: A,
610 }
611
612 #[derive(PartialEq, Debug, Serialize, Deserialize)]
613 enum FlattenedStructVariant {
614 C {
615 ho: i32,
616 #[serde(flatten)]
617 a: B,
618 },
619 }
620
621 assert_eq!(
622 check_roundtrip(
623 &FlattenedStructVariant::C {
624 ho: 24,
625 a: B { a: A(42) }
626 },
627 PrettyConfig::default()
628 ),
629 Err(Err(SpannedError {
630 code: Error::InvalidValueForType {
631 expected: String::from("i32"),
632 found: String::from("a sequence")
633 },
634 position: Position { line: 4, col: 1 }
635 }))
636 );
637 }
638
639 #[test]
640 fn one_tuple_inside_unwrapped_newtype_variant_inside_internally_tagged() {
641 #[derive(PartialEq, Debug, Serialize, Deserialize)]
642 enum A {
643 Newtype((i32,)),
644 }
645
646 #[derive(PartialEq, Debug, Serialize, Deserialize)]
647 #[serde(tag = "tag")]
648 enum InternallyTagged {
649 B { ho: i32, a: A },
650 }
651
652 assert_eq!(
653 check_roundtrip(
654 &InternallyTagged::B {
655 ho: 24,
656 a: A::Newtype((42,))
657 },
658 PrettyConfig::default()
659 ),
660 Ok(())
661 );
662 assert_eq!(
663 check_roundtrip(
664 &InternallyTagged::B {
665 ho: 24,
666 a: A::Newtype((42,))
667 },
668 PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
669 ),
670 Err(Err(SpannedError {
671 code: Error::InvalidValueForType {
672 expected: String::from("a tuple of size 1"),
673 found: String::from("the unsigned integer `42`")
674 },
675 position: Position { line: 6, col: 2 }
676 }))
677 );
678 }
679
680 #[test]
681 fn one_tuple_inside_unwrapped_newtype_variant_inside_adjacently_tagged() {
682 #[derive(PartialEq, Debug, Serialize, Deserialize)]
683 enum A {
684 Newtype([i32; 1]),
685 }
686
687 #[derive(PartialEq, Debug, Serialize, Deserialize)]
688 #[serde(tag = "tag", content = "content")]
689 enum AdjacentlyTagged {
690 B { ho: i32, a: A },
691 }
692
693 assert_eq!(
694 check_roundtrip(
695 &AdjacentlyTagged::B {
696 ho: 24,
697 a: A::Newtype([42])
698 },
699 PrettyConfig::default()
700 ),
701 Ok(())
702 );
703 assert_eq!(
704 check_roundtrip(
705 &AdjacentlyTagged::B {
706 ho: 24,
707 a: A::Newtype([42])
708 },
709 PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
710 ),
711 Ok(())
712 );
713 assert_eq!(
714 ron::from_str::<AdjacentlyTagged>(
715 "#![enable(unwrap_variant_newtypes)] (tag: B, content: (ho: 24, a: Newtype(42)))"
716 ),
717 Ok(AdjacentlyTagged::B {
718 ho: 24,
719 a: A::Newtype([42])
720 }),
721 );
722 assert_eq!(
723 ron::from_str::<AdjacentlyTagged>(
724 "#![enable(unwrap_variant_newtypes)] (content: (ho: 24, a: Newtype(42)), tag: B)"
725 ),
726 Err(SpannedError {
727 code: Error::InvalidValueForType {
728 expected: String::from("an array of length 1"),
729 found: String::from("the unsigned integer `42`")
730 },
731 position: Position { line: 1, col: 79 }
732 })
733 );
734 }
735
736 #[test]
737 fn one_tuple_inside_unwrapped_newtype_variant_inside_untagged() {
738 #[derive(PartialEq, Debug, Serialize, Deserialize)]
739 struct OneTuple(i32, #[serde(skip)] ());
740
741 #[derive(PartialEq, Debug, Serialize, Deserialize)]
742 #[serde(untagged)]
743 enum Untagged {
744 B { ho: i32, a: Option<OneTuple> },
745 }
746
747 assert_eq!(
748 check_roundtrip(
749 &Untagged::B {
750 ho: 24,
751 a: Some(OneTuple(42, ()))
752 },
753 PrettyConfig::default()
754 ),
755 Ok(())
756 );
757 assert_eq!(
758 check_roundtrip(
759 &Untagged::B {
760 ho: 24,
761 a: Some(OneTuple(42, ()))
762 },
763 PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
764 ),
765 Err(Err(SpannedError {
766 code: Error::Message(String::from(
767 "data did not match any variant of untagged enum Untagged"
768 )),
769 position: Position { line: 5, col: 2 }
770 }))
771 );
772 }
773
774 #[test]
775 fn one_tuple_inside_unwrapped_newtype_variant_inside_flatten_struct() {
776 #[derive(PartialEq, Debug, Serialize, Deserialize)]
777 enum A {
778 Newtype([i32; 1]),
779 }
780
781 #[derive(PartialEq, Debug, Serialize, Deserialize)]
782 struct B {
783 a: A,
784 }
785
786 #[derive(PartialEq, Debug, Serialize, Deserialize)]
787 struct FlattenedStruct {
788 ho: i32,
789 #[serde(flatten)]
790 a: B,
791 }
792
793 assert_eq!(
794 check_roundtrip(
795 &FlattenedStruct {
796 ho: 24,
797 a: B {
798 a: A::Newtype([42])
799 }
800 },
801 PrettyConfig::default()
802 ),
803 Ok(())
804 );
805 assert_eq!(
806 check_roundtrip(
807 &FlattenedStruct {
808 ho: 24,
809 a: B {
810 a: A::Newtype([42])
811 }
812 },
813 PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
814 ),
815 Err(Err(SpannedError {
816 code: Error::InvalidValueForType {
817 expected: String::from("an array of length 1"),
818 found: String::from("the unsigned integer `42`")
819 },
820 position: Position { line: 5, col: 1 }
821 }))
822 );
823 }
824
825 #[test]
826 fn one_tuple_inside_unwrapped_newtype_variant_inside_flatten_struct_variant() {
827 #[derive(PartialEq, Debug, Serialize, Deserialize)]
828 enum A {
829 Newtype((i32,)),
830 }
831
832 #[derive(PartialEq, Debug, Serialize, Deserialize)]
833 struct B {
834 a: A,
835 }
836
837 #[derive(PartialEq, Debug, Serialize, Deserialize)]
838 enum FlattenedStructVariant {
839 C {
840 ho: i32,
841 #[serde(flatten)]
842 a: B,
843 },
844 }
845
846 assert_eq!(
847 check_roundtrip(
848 &FlattenedStructVariant::C {
849 ho: 24,
850 a: B {
851 a: A::Newtype((42,))
852 }
853 },
854 PrettyConfig::default()
855 ),
856 Ok(()),
857 );
858 assert_eq!(
859 check_roundtrip(
860 &FlattenedStructVariant::C {
861 ho: 24,
862 a: B {
863 a: A::Newtype((42,))
864 }
865 },
866 PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
867 ),
868 Err(Err(SpannedError {
869 code: Error::InvalidValueForType {
870 expected: String::from("a tuple of size 1"),
871 found: String::from("the unsigned integer `42`")
872 },
873 position: Position { line: 5, col: 1 }
874 }))
875 );
876 }
877
878 #[test]
879 fn one_tuple_variant_inside_internally_tagged() {
880 // A tuple variant with just one element that is not a newtype variant
881 #[derive(PartialEq, Debug, Serialize, Deserialize)]
882 enum OneEnum {
883 OneTuple(i32, #[serde(skip)] ()),
884 }
885
886 #[derive(PartialEq, Debug, Serialize, Deserialize)]
887 struct A {
888 hi: OneEnum,
889 }
890
891 #[derive(PartialEq, Debug, Serialize, Deserialize)]
892 #[serde(tag = "tag")]
893 enum InternallyTagged {
894 B { ho: i32, a: A },
895 }
896
897 assert_eq!(
898 check_roundtrip(
899 &InternallyTagged::B {
900 ho: 24,
901 a: A {
902 hi: OneEnum::OneTuple(42, ())
903 }
904 },
905 PrettyConfig::default()
906 ),
907 Err(Err(SpannedError {
908 code: Error::InvalidValueForType {
909 expected: String::from("tuple variant"),
910 found: String::from("the unsigned integer `42`")
911 },
912 position: Position { line: 7, col: 2 }
913 }))
914 );
915 }
916
917 #[test]
918 fn one_tuple_variant_inside_adjacently_tagged() {
919 // A tuple variant with just one element that is not a newtype variant
920 #[derive(PartialEq, Debug, Serialize, Deserialize)]
921 enum OneEnum {
922 OneTuple(i32, #[serde(skip)] ()),
923 }
924
925 #[derive(PartialEq, Debug, Serialize, Deserialize)]
926 struct A {
927 hi: OneEnum,
928 }
929
930 #[derive(PartialEq, Debug, Serialize, Deserialize)]
931 #[serde(tag = "tag", content = "content")]
932 enum AdjacentlyTagged {
933 B { ho: i32, a: A },
934 }
935
936 assert_eq!(
937 check_roundtrip(
938 &AdjacentlyTagged::B {
939 ho: 24,
940 a: A {
941 hi: OneEnum::OneTuple(42, ())
942 }
943 },
944 PrettyConfig::default()
945 ),
946 Ok(())
947 );
948 assert_eq!(
949 ron::from_str::<AdjacentlyTagged>("(tag: B, content: (ho: 24, a: (hi: OneTuple(42))))"),
950 Ok(AdjacentlyTagged::B {
951 ho: 24,
952 a: A {
953 hi: OneEnum::OneTuple(42, ())
954 }
955 }),
956 );
957 assert_eq!(
958 ron::from_str::<AdjacentlyTagged>("(content: (ho: 24, a: (hi: OneTuple(42))), tag: B)"),
959 Err(SpannedError {
960 code: Error::InvalidValueForType {
961 expected: String::from("tuple variant"),
962 found: String::from("the unsigned integer `42`")
963 },
964 position: Position { line: 1, col: 50 }
965 })
966 );
967 }
968
969 #[test]
970 fn one_tuple_variant_inside_untagged() {
971 // A tuple variant with just one element that is not a newtype variant
972 #[derive(PartialEq, Debug, Serialize, Deserialize)]
973 enum OneEnum {
974 OneTuple(i32, #[serde(skip)] ()),
975 }
976
977 #[derive(PartialEq, Debug, Serialize, Deserialize)]
978 struct A {
979 hi: OneEnum,
980 }
981
982 #[derive(PartialEq, Debug, Serialize, Deserialize)]
983 #[serde(untagged)]
984 enum Untagged {
985 B { ho: i32, a: A },
986 }
987
988 assert_eq!(
989 check_roundtrip(
990 &Untagged::B {
991 ho: 24,
992 a: A {
993 hi: OneEnum::OneTuple(42, ())
994 }
995 },
996 PrettyConfig::default()
997 ),
998 Err(Err(SpannedError {
999 code: Error::Message(String::from(
1000 "data did not match any variant of untagged enum Untagged"
1001 )),
1002 position: Position { line: 6, col: 2 }
1003 }))
1004 );
1005 }
1006
1007 #[test]
1008 fn one_tuple_variant_inside_flatten_struct() {
1009 // A tuple variant with just one element that is not a newtype variant
1010 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1011 enum OneEnum {
1012 OneTuple(i32, #[serde(skip)] ()),
1013 }
1014
1015 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1016 struct A {
1017 hi: OneEnum,
1018 }
1019
1020 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1021 struct B {
1022 a: A,
1023 }
1024
1025 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1026 struct FlattenedStruct {
1027 ho: i32,
1028 #[serde(flatten)]
1029 a: B,
1030 }
1031
1032 assert_eq!(
1033 check_roundtrip(
1034 &FlattenedStruct {
1035 ho: 24,
1036 a: B {
1037 a: A {
1038 hi: OneEnum::OneTuple(42, ())
1039 }
1040 }
1041 },
1042 PrettyConfig::default()
1043 ),
1044 Err(Err(SpannedError {
1045 code: Error::InvalidValueForType {
1046 expected: String::from("tuple variant"),
1047 found: String::from("the unsigned integer `42`")
1048 },
1049 position: Position { line: 6, col: 1 }
1050 }))
1051 );
1052 }
1053
1054 #[test]
1055 fn one_tuple_variant_inside_flatten_struct_variant() {
1056 // A tuple variant with just one element that is not a newtype variant
1057 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1058 enum OneEnum {
1059 OneTuple(i32, #[serde(skip)] ()),
1060 }
1061
1062 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1063 struct A {
1064 hi: OneEnum,
1065 }
1066
1067 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1068 struct B {
1069 a: A,
1070 }
1071
1072 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1073 enum FlattenedStructVariant {
1074 C {
1075 ho: i32,
1076 #[serde(flatten)]
1077 a: B,
1078 },
1079 }
1080
1081 assert_eq!(
1082 check_roundtrip(
1083 &FlattenedStructVariant::C {
1084 ho: 24,
1085 a: B {
1086 a: A {
1087 hi: OneEnum::OneTuple(42, ())
1088 }
1089 }
1090 },
1091 PrettyConfig::default()
1092 ),
1093 Err(Err(SpannedError {
1094 code: Error::InvalidValueForType {
1095 expected: String::from("tuple variant"),
1096 found: String::from("the unsigned integer `42`")
1097 },
1098 position: Position { line: 6, col: 1 }
1099 }))
1100 );
1101 }
1102
1103 #[test]
1104 fn raw_value_inside_internally_tagged() {
1105 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1106 struct A {
1107 hi: Box<ron::value::RawValue>,
1108 }
1109
1110 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1111 #[serde(tag = "tag")]
1112 enum InternallyTagged {
1113 B { ho: i32, a: A },
1114 }
1115
1116 assert_eq!(
1117 check_roundtrip(
1118 &InternallyTagged::B {
1119 ho: 24,
1120 a: A {
1121 hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
1122 .unwrap(),
1123 }
1124 },
1125 PrettyConfig::default()
1126 ),
1127 Err(Err(SpannedError {
1128 code: Error::InvalidValueForType {
1129 expected: String::from("any valid RON-value-string"),
1130 found: String::from("the unsigned integer `42`")
1131 },
1132 position: Position { line: 7, col: 2 }
1133 }))
1134 );
1135 }
1136
1137 #[test]
1138 fn raw_value_inside_adjacently_tagged() {
1139 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1140 struct A {
1141 hi: Box<ron::value::RawValue>,
1142 }
1143
1144 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1145 #[serde(tag = "tag", content = "content")]
1146 enum AdjacentlyTagged {
1147 B { ho: i32, a: A },
1148 }
1149
1150 assert_eq!(
1151 check_roundtrip(
1152 &AdjacentlyTagged::B {
1153 ho: 24,
1154 a: A {
1155 hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
1156 .unwrap(),
1157 }
1158 },
1159 PrettyConfig::default()
1160 ),
1161 // adds an extra space
1162 Err(Ok(Error::Message(String::from("ROUNDTRIP error: B { ho: 24, a: A { hi: RawValue(42) } } != B { ho: 24, a: A { hi: RawValue( 42) } }"))))
1163 );
1164 assert_eq!(
1165 ron::from_str::<AdjacentlyTagged>("(tag: B, content: (ho: 24, a: (hi:42)))"),
1166 Ok(AdjacentlyTagged::B {
1167 ho: 24,
1168 a: A {
1169 hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
1170 .unwrap(),
1171 }
1172 }),
1173 );
1174 assert_eq!(
1175 ron::from_str::<AdjacentlyTagged>("(content: (ho: 24, a: (hi:42)), tag: B)"),
1176 Err(SpannedError {
1177 code: Error::InvalidValueForType {
1178 expected: String::from("any valid RON-value-string"),
1179 found: String::from("the unsigned integer `42`")
1180 },
1181 position: Position { line: 1, col: 39 }
1182 })
1183 );
1184 }
1185
1186 #[test]
1187 fn raw_value_inside_untagged() {
1188 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1189 struct A {
1190 hi: Box<ron::value::RawValue>,
1191 }
1192
1193 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1194 #[serde(untagged)]
1195 enum Untagged {
1196 B { ho: i32, a: A },
1197 }
1198
1199 assert_eq!(
1200 check_roundtrip(
1201 &Untagged::B {
1202 ho: 24,
1203 a: A {
1204 hi: ron::value::RawValue::from_boxed_ron(String::from("42").into_boxed_str())
1205 .unwrap(),
1206 }
1207 },
1208 PrettyConfig::default()
1209 ),
1210 Err(Err(SpannedError {
1211 code: Error::Message(String::from(
1212 "data did not match any variant of untagged enum Untagged"
1213 )),
1214 position: Position { line: 6, col: 2 }
1215 }))
1216 );
1217 }
1218
1219 #[test]
1220 fn raw_value_inside_flatten_struct() {
1221 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1222 struct A {
1223 hi: Box<ron::value::RawValue>,
1224 }
1225
1226 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1227 struct B {
1228 a: A,
1229 }
1230
1231 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1232 struct FlattenedStruct {
1233 ho: i32,
1234 #[serde(flatten)]
1235 a: B,
1236 }
1237
1238 assert_eq!(
1239 check_roundtrip(
1240 &FlattenedStruct {
1241 ho: 24,
1242 a: B {
1243 a: A {
1244 hi: ron::value::RawValue::from_boxed_ron(
1245 String::from("42").into_boxed_str()
1246 )
1247 .unwrap(),
1248 }
1249 }
1250 },
1251 PrettyConfig::default()
1252 ),
1253 Err(Err(SpannedError {
1254 code: Error::InvalidValueForType {
1255 expected: String::from("any valid RON-value-string"),
1256 found: String::from("the unsigned integer `42`")
1257 },
1258 position: Position { line: 6, col: 1 }
1259 }))
1260 );
1261 }
1262
1263 #[test]
1264 fn raw_value_inside_flatten_struct_variant() {
1265 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1266 struct A {
1267 hi: Box<ron::value::RawValue>,
1268 }
1269
1270 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1271 struct B {
1272 a: A,
1273 }
1274
1275 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1276 enum FlattenedStructVariant {
1277 C {
1278 ho: i32,
1279 #[serde(flatten)]
1280 a: B,
1281 },
1282 }
1283
1284 assert_eq!(
1285 check_roundtrip(
1286 &FlattenedStructVariant::C {
1287 ho: 24,
1288 a: B {
1289 a: A {
1290 hi: ron::value::RawValue::from_boxed_ron(
1291 String::from("42").into_boxed_str()
1292 )
1293 .unwrap(),
1294 }
1295 }
1296 },
1297 PrettyConfig::default()
1298 ),
1299 Err(Err(SpannedError {
1300 code: Error::InvalidValueForType {
1301 expected: String::from("any valid RON-value-string"),
1302 found: String::from("the unsigned integer `42`")
1303 },
1304 position: Position { line: 6, col: 1 }
1305 }))
1306 );
1307 }
1308
1309 #[test]
1310 fn unit_like_zero_length_inside_internally_tagged() {
1311 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1312 struct A {
1313 hi: [i32; 0],
1314 }
1315
1316 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1317 #[serde(tag = "tag")]
1318 enum InternallyTagged {
1319 B { ho: i32, a: A },
1320 }
1321
1322 assert_eq!(
1323 check_roundtrip(
1324 &InternallyTagged::B {
1325 ho: 24,
1326 a: A { hi: [] }
1327 },
1328 PrettyConfig::default()
1329 ),
1330 Err(Err(SpannedError {
1331 code: Error::InvalidValueForType {
1332 expected: String::from("an empty array"),
1333 found: String::from("a unit value")
1334 },
1335 position: Position { line: 7, col: 2 }
1336 }))
1337 );
1338 }
1339
1340 #[test]
1341 fn unit_like_zero_length_inside_adjacently_tagged() {
1342 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1343 struct TupleStruct();
1344
1345 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1346 struct A {
1347 hi: TupleStruct,
1348 }
1349
1350 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1351 #[serde(tag = "tag", content = "content")]
1352 enum AdjacentlyTagged {
1353 B { ho: i32, a: A },
1354 }
1355
1356 assert_eq!(
1357 check_roundtrip(
1358 &AdjacentlyTagged::B {
1359 ho: 24,
1360 a: A { hi: TupleStruct() }
1361 },
1362 PrettyConfig::default()
1363 ),
1364 Ok(()),
1365 );
1366 assert_eq!(
1367 ron::from_str::<AdjacentlyTagged>("(tag: B, content: (ho: 24, a: (hi: ())))"),
1368 Ok(AdjacentlyTagged::B {
1369 ho: 24,
1370 a: A { hi: TupleStruct() }
1371 }),
1372 );
1373 assert_eq!(
1374 ron::from_str::<AdjacentlyTagged>("(content: (ho: 24, a: (hi: ())), tag: B)"),
1375 Err(SpannedError {
1376 code: Error::InvalidValueForType {
1377 expected: String::from("tuple struct TupleStruct"),
1378 found: String::from("a unit value")
1379 },
1380 position: Position { line: 1, col: 40 }
1381 })
1382 );
1383 }
1384
1385 #[test]
1386 fn unit_like_zero_length_inside_untagged() {
1387 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1388 struct Struct {}
1389
1390 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1391 struct A {
1392 hi: Struct,
1393 }
1394
1395 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1396 #[serde(untagged)]
1397 enum Untagged {
1398 B { ho: i32, a: A },
1399 }
1400
1401 assert_eq!(
1402 check_roundtrip(
1403 &Untagged::B {
1404 ho: 24,
1405 a: A { hi: Struct {} }
1406 },
1407 PrettyConfig::default()
1408 ),
1409 Err(Err(SpannedError {
1410 code: Error::Message(String::from(
1411 "data did not match any variant of untagged enum Untagged"
1412 )),
1413 position: Position { line: 6, col: 2 }
1414 }))
1415 );
1416 }
1417
1418 #[test]
1419 fn unit_like_zero_length_inside_flatten_struct() {
1420 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1421 enum Enum {
1422 Tuple(),
1423 }
1424
1425 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1426 struct A {
1427 hi: Enum,
1428 }
1429
1430 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1431 struct B {
1432 a: A,
1433 }
1434
1435 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1436 struct FlattenedStruct {
1437 ho: i32,
1438 #[serde(flatten)]
1439 a: B,
1440 }
1441
1442 assert_eq!(
1443 check_roundtrip(
1444 &FlattenedStruct {
1445 ho: 24,
1446 a: B {
1447 a: A { hi: Enum::Tuple() }
1448 }
1449 },
1450 PrettyConfig::default()
1451 ),
1452 Err(Err(SpannedError {
1453 code: Error::InvalidValueForType {
1454 expected: String::from("tuple variant"),
1455 found: String::from("a unit value")
1456 },
1457 position: Position { line: 6, col: 1 }
1458 }))
1459 );
1460 }
1461
1462 #[test]
1463 fn unit_like_zero_length_inside_flatten_struct_variant() {
1464 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1465 enum Enum {
1466 Struct {},
1467 }
1468
1469 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1470 struct A {
1471 hi: Enum,
1472 }
1473
1474 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1475 struct B {
1476 a: A,
1477 }
1478
1479 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1480 enum FlattenedStructVariant {
1481 C {
1482 ho: i32,
1483 #[serde(flatten)]
1484 a: B,
1485 },
1486 }
1487
1488 assert_eq!(
1489 check_roundtrip(
1490 &FlattenedStructVariant::C {
1491 ho: 24,
1492 a: B {
1493 a: A {
1494 hi: Enum::Struct {}
1495 }
1496 }
1497 },
1498 PrettyConfig::default()
1499 ),
1500 Err(Err(SpannedError {
1501 code: Error::InvalidValueForType {
1502 expected: String::from("struct variant"),
1503 found: String::from("a unit value")
1504 },
1505 position: Position { line: 6, col: 1 }
1506 }))
1507 );
1508 }
1509
1510 #[test]
1511 fn i128_inside_internally_tagged() {
1512 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1513 struct A {
1514 hi: i128,
1515 }
1516
1517 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1518 #[serde(tag = "tag")]
1519 enum InternallyTagged {
1520 B { ho: i32, a: A },
1521 }
1522
1523 #[cfg(not(feature = "integer128"))]
1524 assert_eq!(
1525 check_roundtrip(
1526 &InternallyTagged::B {
1527 ho: 24,
1528 a: A { hi: i128::MAX }
1529 },
1530 PrettyConfig::default()
1531 ),
1532 Err(Ok(Error::Message(String::from("i128 is not supported"))))
1533 );
1534 #[cfg(feature = "integer128")]
1535 assert_eq!(
1536 check_roundtrip(
1537 &InternallyTagged::B {
1538 ho: 24,
1539 a: A { hi: i128::MAX }
1540 },
1541 PrettyConfig::default()
1542 ),
1543 Err(Err(SpannedError {
1544 code: Error::InvalidValueForType {
1545 expected: String::from("any value"),
1546 found: format!("integer `{}` as u128", i128::MAX)
1547 },
1548 position: Position { line: 5, col: 52 }
1549 }))
1550 );
1551 }
1552
1553 #[test]
1554 fn u128_inside_adjacently_tagged() {
1555 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1556 struct A {
1557 hi: u128,
1558 }
1559
1560 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1561 #[serde(tag = "tag", content = "content")]
1562 enum AdjacentlyTagged {
1563 B { ho: i32, a: A },
1564 }
1565
1566 #[cfg(not(feature = "integer128"))]
1567 assert_eq!(
1568 check_roundtrip(
1569 &AdjacentlyTagged::B {
1570 ho: 24,
1571 a: A { hi: u128::MAX }
1572 },
1573 PrettyConfig::default()
1574 ),
1575 Err(Ok(Error::Message(String::from("u128 is not supported"))))
1576 );
1577 #[cfg(feature = "integer128")]
1578 assert_eq!(
1579 check_roundtrip(
1580 &AdjacentlyTagged::B {
1581 ho: 24,
1582 a: A { hi: u128::MAX }
1583 },
1584 PrettyConfig::default()
1585 ),
1586 Ok(()),
1587 );
1588 #[cfg(feature = "integer128")]
1589 assert_eq!(
1590 ron::from_str::<AdjacentlyTagged>(&format!(
1591 "(tag: B, content: (ho: 24, a: (hi: {})))",
1592 u128::MAX
1593 ),),
1594 Ok(AdjacentlyTagged::B {
1595 ho: 24,
1596 a: A { hi: u128::MAX }
1597 }),
1598 );
1599 #[cfg(feature = "integer128")]
1600 assert_eq!(
1601 ron::from_str::<AdjacentlyTagged>(&format!(
1602 "(content: (ho: 24, a: (hi: {})), tag: B)",
1603 u128::MAX
1604 ),),
1605 Err(SpannedError {
1606 code: Error::InvalidValueForType {
1607 expected: String::from("any value"),
1608 found: format!("integer `{}` as u128", u128::MAX)
1609 },
1610 position: Position { line: 1, col: 67 }
1611 }),
1612 );
1613 }
1614
1615 #[test]
1616 fn i128_inside_untagged() {
1617 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1618 struct A {
1619 hi: i128,
1620 }
1621
1622 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1623 #[serde(untagged)]
1624 enum Untagged {
1625 B { ho: i32, a: A },
1626 }
1627
1628 #[cfg(not(feature = "integer128"))]
1629 assert_eq!(
1630 check_roundtrip(
1631 &Untagged::B {
1632 ho: 24,
1633 a: A { hi: i128::MIN }
1634 },
1635 PrettyConfig::default()
1636 ),
1637 Err(Ok(Error::Message(String::from("i128 is not supported"))))
1638 );
1639 #[cfg(feature = "integer128")]
1640 assert_eq!(
1641 check_roundtrip(
1642 &Untagged::B {
1643 ho: 24,
1644 a: A { hi: i128::MIN }
1645 },
1646 PrettyConfig::default()
1647 ),
1648 Err(Err(SpannedError {
1649 code: Error::InvalidValueForType {
1650 expected: String::from("any value"),
1651 found: format!("integer `{}` as i128", i128::MIN)
1652 },
1653 position: Position { line: 4, col: 53 }
1654 }))
1655 );
1656 }
1657
1658 #[test]
1659 fn u128_inside_flatten_struct() {
1660 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1661 struct A {
1662 hi: u128,
1663 }
1664
1665 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1666 struct B {
1667 a: A,
1668 }
1669
1670 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1671 struct FlattenedStruct {
1672 ho: i32,
1673 #[serde(flatten)]
1674 a: B,
1675 }
1676
1677 #[cfg(not(feature = "integer128"))]
1678 assert_eq!(
1679 check_roundtrip(
1680 &FlattenedStruct {
1681 ho: 24,
1682 a: B {
1683 a: A { hi: u128::MAX }
1684 }
1685 },
1686 PrettyConfig::default()
1687 ),
1688 Err(Ok(Error::Message(String::from("u128 is not supported"))))
1689 );
1690 #[cfg(feature = "integer128")]
1691 assert_eq!(
1692 check_roundtrip(
1693 &FlattenedStruct {
1694 ho: 24,
1695 a: B {
1696 a: A { hi: u128::MAX }
1697 }
1698 },
1699 PrettyConfig::default()
1700 ),
1701 Err(Err(SpannedError {
1702 code: Error::InvalidValueForType {
1703 expected: String::from("any value"),
1704 found: format!("integer `{}` as u128", u128::MAX)
1705 },
1706 position: Position { line: 4, col: 52 }
1707 }))
1708 );
1709 }
1710
1711 #[test]
1712 fn i128_inside_flatten_struct_variant() {
1713 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1714 struct A {
1715 hi: i128,
1716 }
1717
1718 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1719 struct B {
1720 a: A,
1721 }
1722
1723 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1724 enum FlattenedStructVariant {
1725 C {
1726 ho: i32,
1727 #[serde(flatten)]
1728 a: B,
1729 },
1730 }
1731
1732 #[cfg(not(feature = "integer128"))]
1733 assert_eq!(
1734 check_roundtrip(
1735 &FlattenedStructVariant::C {
1736 ho: 24,
1737 a: B {
1738 a: A { hi: i128::MIN }
1739 }
1740 },
1741 PrettyConfig::default()
1742 ),
1743 Err(Ok(Error::Message(String::from("i128 is not supported"))))
1744 );
1745 #[cfg(feature = "integer128")]
1746 assert_eq!(
1747 check_roundtrip(
1748 &FlattenedStructVariant::C {
1749 ho: 24,
1750 a: B {
1751 a: A { hi: i128::MIN }
1752 }
1753 },
1754 PrettyConfig::default()
1755 ),
1756 Err(Err(SpannedError {
1757 code: Error::InvalidValueForType {
1758 expected: String::from("any value"),
1759 found: format!("integer `{}` as i128", i128::MIN)
1760 },
1761 position: Position { line: 4, col: 53 }
1762 }))
1763 );
1764 }
1765
1766 #[test]
1767 fn duplicate_key_inside_flatten_struct() {
1768 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1769 struct FlattenedStruct {
1770 ho: i32,
1771 #[serde(flatten)]
1772 other: HashMap<String, i32>,
1773 }
1774
1775 assert_eq!(
1776 check_roundtrip(
1777 &FlattenedStruct {
1778 ho: 24,
1779 other: [(String::from("ho"), 42)].into_iter().collect(),
1780 },
1781 PrettyConfig::default()
1782 ),
1783 Err(Err(SpannedError {
1784 code: Error::DuplicateStructField {
1785 field: "ho",
1786 outer: None
1787 },
1788 position: Position { line: 3, col: 9 }
1789 }))
1790 );
1791 }
1792
1793 #[test]
1794 fn duplicate_key_inside_flatten_struct_variant() {
1795 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1796 struct C {
1797 ho: i32,
1798 hi: bool,
1799 }
1800
1801 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1802 struct B {
1803 #[serde(flatten)]
1804 inner: C,
1805 }
1806
1807 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1808 enum FlattenedStructVariant {
1809 A {
1810 ho: i32,
1811 #[serde(flatten)]
1812 inner: B,
1813 },
1814 }
1815
1816 assert_eq!(
1817 check_roundtrip(
1818 &FlattenedStructVariant::A {
1819 ho: 24,
1820 inner: B {
1821 inner: C { ho: 42, hi: false }
1822 }
1823 },
1824 PrettyConfig::default()
1825 ),
1826 Err(Err(SpannedError {
1827 code: Error::DuplicateStructField {
1828 field: "ho",
1829 outer: Some(String::from("A"))
1830 },
1831 position: Position { line: 3, col: 9 }
1832 }))
1833 );
1834 }
1835
1836 #[test]
1837 fn non_string_key_inside_flatten_struct() {
1838 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1839 struct FlattenedStruct {
1840 ho: i32,
1841 #[serde(flatten)]
1842 other: HashMap<i32, bool>,
1843 }
1844
1845 assert_eq!(
1846 check_roundtrip(
1847 &FlattenedStruct {
1848 ho: 24,
1849 other: [(1, true), (0, false)].into_iter().collect(),
1850 },
1851 PrettyConfig::default()
1852 ),
1853 Err(Err(SpannedError {
1854 code: Error::ExpectedString,
1855 position: Position { line: 3, col: 5 }
1856 }))
1857 );
1858 }
1859
1860 #[test]
1861 fn non_string_key_inside_flatten_struct_variant() {
1862 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1863 enum FlattenedStructVariant {
1864 A {
1865 ho: i32,
1866 #[serde(flatten)]
1867 other: HashMap<char, u8>,
1868 },
1869 }
1870
1871 assert_eq!(
1872 check_roundtrip(
1873 &FlattenedStructVariant::A {
1874 ho: 24,
1875 other: [('h', 0), ('i', 1)].into_iter().collect(),
1876 },
1877 PrettyConfig::default()
1878 ),
1879 Err(Err(SpannedError {
1880 code: Error::ExpectedString,
1881 position: Position { line: 3, col: 5 }
1882 }))
1883 );
1884 }
1885
1886 #[test]
1887 fn more_than_one_flatten_map_inside_flatten_struct() {
1888 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1889 struct Deep {
1890 #[serde(flatten)]
1891 other: HashMap<String, bool>,
1892 }
1893
1894 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1895 struct Inner {
1896 #[serde(flatten)]
1897 deep: Deep,
1898 }
1899
1900 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1901 struct FlattenedStruct {
1902 ho: i32,
1903 #[serde(flatten)]
1904 hi: Inner,
1905 #[serde(flatten)]
1906 other: HashMap<String, bool>,
1907 }
1908
1909 assert_eq!(
1910 check_roundtrip(
1911 &FlattenedStruct {
1912 ho: 24,
1913 hi: Inner {
1914 deep: Deep {
1915 other: HashMap::new(),
1916 },
1917 },
1918 other: [(String::from("42"), true)].into_iter().collect(),
1919 },
1920 PrettyConfig::default()
1921 ),
1922 // both maps collect all the unknown keys
1923 Err(Ok(Error::Message(String::from("ROUNDTRIP error: FlattenedStruct { ho: 24, hi: Inner { deep: Deep { other: {} } }, other: {\"42\": true} } != FlattenedStruct { ho: 24, hi: Inner { deep: Deep { other: {\"42\": true} } }, other: {\"42\": true} }"))))
1924 );
1925 }
1926
1927 #[test]
1928 fn more_than_one_flatten_map_inside_flatten_struct_variant() {
1929 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1930 struct Deep {
1931 #[serde(flatten)]
1932 other: HashMap<String, bool>,
1933 }
1934
1935 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1936 struct Inner {
1937 #[serde(flatten)]
1938 deep: Deep,
1939 }
1940
1941 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1942 enum FlattenedStructVariant {
1943 A {
1944 ho: i32,
1945 #[serde(flatten)]
1946 hi: Inner,
1947 #[serde(flatten)]
1948 other: HashMap<String, bool>,
1949 },
1950 }
1951
1952 assert_eq!(
1953 check_roundtrip(
1954 &FlattenedStructVariant::A {
1955 ho: 24,
1956 hi: Inner {
1957 deep: Deep {
1958 other: [(String::from("24"), false)].into_iter().collect(),
1959 },
1960 },
1961 other: HashMap::new(),
1962 },
1963 PrettyConfig::default()
1964 ),
1965 // both maps collect all the unknown keys
1966 Err(Ok(Error::Message(String::from("ROUNDTRIP error: A { ho: 24, hi: Inner { deep: Deep { other: {\"24\": false} } }, other: {} } != A { ho: 24, hi: Inner { deep: Deep { other: {\"24\": false} } }, other: {\"24\": false} }"))))
1967 );
1968 }
1969
1970 #[test]
1971 fn flatten_struct_beside_map_inside_flatten_struct() {
1972 // The non-flattened struct must contain some flattened fields
1973 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1974 struct Flattened {
1975 hi: i32,
1976 #[serde(flatten)]
1977 flat: (),
1978 }
1979
1980 // The non-flattened struct must be behind a level of flatten
1981 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1982 struct Inner {
1983 flat: Flattened,
1984 }
1985
1986 #[derive(PartialEq, Debug, Serialize, Deserialize)]
1987 struct FlattenedStruct {
1988 ho: i32,
1989 #[serde(flatten)]
1990 other: HashMap<String, i32>,
1991 #[serde(flatten)]
1992 inner: Inner,
1993 }
1994
1995 assert_eq!(
1996 check_roundtrip(
1997 &FlattenedStruct {
1998 ho: 24,
1999 other: HashMap::new(),
2000 inner: Inner {
2001 flat: Flattened { hi: 42, flat: () },
2002 },
2003 },
2004 PrettyConfig::default()
2005 ),
2006 Err(Err(SpannedError {
2007 code: Error::InvalidValueForType {
2008 expected: String::from("i32"),
2009 found: String::from("a map")
2010 },
2011 position: Position { line: 6, col: 1 }
2012 }))
2013 );
2014 }
2015
2016 #[test]
2017 fn flatten_struct_beside_map_inside_flatten_struct_variant() {
2018 // The non-flattened struct must contain some flattened fields
2019 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2020 struct Flattened {
2021 hi: i32,
2022 #[serde(flatten)]
2023 flat: (),
2024 }
2025
2026 // The non-flattened struct must be behind a level of flatten
2027 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2028 struct Inner {
2029 flat: Flattened,
2030 }
2031
2032 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2033 enum FlattenedStructVariant {
2034 A {
2035 ho: i32,
2036 #[serde(flatten)]
2037 other: HashMap<String, i32>,
2038 #[serde(flatten)]
2039 inner: Inner,
2040 },
2041 }
2042
2043 assert_eq!(
2044 check_roundtrip(
2045 &FlattenedStructVariant::A {
2046 ho: 24,
2047 other: HashMap::new(),
2048 inner: Inner {
2049 flat: Flattened { hi: 42, flat: () },
2050 },
2051 },
2052 PrettyConfig::default()
2053 ),
2054 Err(Err(SpannedError {
2055 code: Error::InvalidValueForType {
2056 expected: String::from("i32"),
2057 found: String::from("a map")
2058 },
2059 position: Position { line: 6, col: 1 }
2060 }))
2061 );
2062 }
2063
2064 #[test]
2065 fn untagged_flatten_struct_variant_beside_map_inside_flatten_struct() {
2066 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2067 #[serde(untagged)]
2068 enum Flattened {
2069 Struct {
2070 hi: i32,
2071 #[serde(flatten)]
2072 flat: (),
2073 },
2074 }
2075
2076 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2077 struct FlattenedStruct {
2078 ho: i32,
2079 #[serde(flatten)]
2080 other: HashMap<String, i32>,
2081 #[serde(flatten)]
2082 flat: Flattened,
2083 }
2084
2085 assert_eq!(
2086 check_roundtrip(
2087 &FlattenedStruct {
2088 ho: 24,
2089 other: HashMap::new(),
2090 flat: Flattened::Struct { hi: 42, flat: () },
2091 },
2092 PrettyConfig::default()
2093 ),
2094 Err(Ok(Error::Message(String::from("ROUNDTRIP error: FlattenedStruct { ho: 24, other: {}, flat: Struct { hi: 42, flat: () } } != FlattenedStruct { ho: 24, other: {\"hi\": 42}, flat: Struct { hi: 42, flat: () } }"))))
2095 );
2096 }
2097
2098 #[test]
2099 fn untagged_flatten_struct_variant_beside_map_inside_flatten_struct_variant() {
2100 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2101 #[serde(untagged)]
2102 enum Flattened {
2103 Struct {
2104 hi: i32,
2105 #[serde(flatten)]
2106 flat: (),
2107 },
2108 }
2109
2110 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2111 enum FlattenedStructVariant {
2112 A {
2113 ho: i32,
2114 #[serde(flatten)]
2115 other: HashMap<String, i32>,
2116 #[serde(flatten)]
2117 flat: Flattened,
2118 },
2119 }
2120
2121 assert_eq!(
2122 check_roundtrip(
2123 &FlattenedStructVariant::A {
2124 ho: 24,
2125 other: HashMap::new(),
2126 flat: Flattened::Struct { hi: 42, flat: () },
2127 },
2128 PrettyConfig::default()
2129 ),
2130 Err(Ok(Error::Message(String::from("ROUNDTRIP error: A { ho: 24, other: {}, flat: Struct { hi: 42, flat: () } } != A { ho: 24, other: {\"hi\": 42}, flat: Struct { hi: 42, flat: () } }"))))
2131 );
2132 }
2133
2134 #[test]
2135 fn externally_tagged_newtype_variant_beside_map_inside_flatten_struct_variant() {
2136 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2137 enum Flattened {
2138 Newtype(()),
2139 }
2140
2141 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2142 enum FlattenedStructVariant {
2143 A {
2144 ho: i32,
2145 #[serde(flatten)]
2146 other: HashMap<String, i32>,
2147 #[serde(flatten)]
2148 flat: Flattened,
2149 },
2150 }
2151
2152 assert_eq!(
2153 check_roundtrip(
2154 &FlattenedStructVariant::A {
2155 ho: 24,
2156 other: HashMap::new(),
2157 flat: Flattened::Newtype(()),
2158 },
2159 PrettyConfig::default()
2160 ),
2161 Err(Err(SpannedError {
2162 code: Error::InvalidValueForType {
2163 expected: String::from("i32"),
2164 found: String::from("a unit value")
2165 },
2166 position: Position { line: 4, col: 1 }
2167 }))
2168 );
2169 }
2170
2171 #[test]
2172 fn externally_tagged_struct_variant_beside_map_inside_flatten_struct() {
2173 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2174 enum Flattened {
2175 Struct { hi: i32 },
2176 }
2177
2178 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2179 struct FlattenedStruct {
2180 ho: i32,
2181 #[serde(flatten)]
2182 other: HashMap<String, i32>,
2183 #[serde(flatten)]
2184 flat: Flattened,
2185 }
2186
2187 assert_eq!(
2188 check_roundtrip(
2189 &FlattenedStruct {
2190 ho: 24,
2191 other: HashMap::new(),
2192 flat: Flattened::Struct { hi: 42 },
2193 },
2194 PrettyConfig::default()
2195 ),
2196 Err(Err(SpannedError {
2197 code: Error::InvalidValueForType {
2198 expected: String::from("i32"),
2199 found: String::from("a map")
2200 },
2201 position: Position { line: 6, col: 1 }
2202 }))
2203 );
2204 }
2205
2206 #[test]
2207 fn externally_tagged_tuple_variant_beside_map_inside_flatten_struct_variant() {
2208 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2209 enum Flattened {
2210 Tuple(i32, bool),
2211 }
2212
2213 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2214 enum FlattenedStructVariant {
2215 A {
2216 ho: i32,
2217 #[serde(flatten)]
2218 other: HashMap<String, i32>,
2219 #[serde(flatten)]
2220 flat: Flattened,
2221 },
2222 }
2223
2224 assert_eq!(
2225 check_roundtrip(
2226 &FlattenedStructVariant::A {
2227 ho: 24,
2228 other: HashMap::new(),
2229 flat: Flattened::Tuple(42, true),
2230 },
2231 PrettyConfig::default()
2232 ),
2233 Err(Err(SpannedError {
2234 code: Error::InvalidValueForType {
2235 expected: String::from("i32"),
2236 found: String::from("a sequence")
2237 },
2238 position: Position { line: 7, col: 1 }
2239 }))
2240 );
2241 }
2242
2243 #[test]
2244 fn internally_tagged_unit_variant_beside_map_inside_flatten_struct() {
2245 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2246 #[serde(tag = "tag")]
2247 enum Flattened {
2248 Unit,
2249 }
2250
2251 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2252 struct FlattenedStruct {
2253 ho: i32,
2254 #[serde(flatten)]
2255 other: HashMap<String, i32>,
2256 #[serde(flatten)]
2257 flat: Flattened,
2258 }
2259
2260 assert_eq!(
2261 check_roundtrip(
2262 &FlattenedStruct {
2263 ho: 24,
2264 other: HashMap::new(),
2265 flat: Flattened::Unit,
2266 },
2267 PrettyConfig::default()
2268 ),
2269 Err(Err(SpannedError {
2270 code: Error::InvalidValueForType {
2271 expected: String::from("i32"),
2272 found: String::from("the string \"Unit\"")
2273 },
2274 position: Position { line: 4, col: 1 }
2275 }))
2276 );
2277 }
2278
2279 #[test]
2280 fn internally_tagged_newtype_variant_beside_map_inside_flatten_struct_variant() {
2281 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2282 #[serde(tag = "tag")]
2283 enum Flattened {
2284 Newtype(()),
2285 }
2286
2287 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2288 enum FlattenedStructVariant {
2289 A {
2290 ho: i32,
2291 #[serde(flatten)]
2292 other: HashMap<String, i32>,
2293 #[serde(flatten)]
2294 flat: Flattened,
2295 },
2296 }
2297
2298 assert_eq!(
2299 check_roundtrip(
2300 &FlattenedStructVariant::A {
2301 ho: 24,
2302 other: HashMap::new(),
2303 flat: Flattened::Newtype(()),
2304 },
2305 PrettyConfig::default()
2306 ),
2307 Err(Err(SpannedError {
2308 code: Error::InvalidValueForType {
2309 expected: String::from("i32"),
2310 found: String::from("the string \"Newtype\"")
2311 },
2312 position: Position { line: 4, col: 1 }
2313 }))
2314 );
2315 }
2316
2317 #[test]
2318 fn internally_tagged_struct_variant_beside_map_inside_flatten_struct() {
2319 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2320 #[serde(tag = "tag")]
2321 enum Flattened {
2322 Struct { hi: i32 },
2323 }
2324
2325 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2326 struct FlattenedStruct {
2327 ho: i32,
2328 #[serde(flatten)]
2329 other: HashMap<String, i32>,
2330 #[serde(flatten)]
2331 flat: Flattened,
2332 }
2333
2334 assert_eq!(
2335 check_roundtrip(
2336 &FlattenedStruct {
2337 ho: 24,
2338 other: HashMap::new(),
2339 flat: Flattened::Struct { hi: 42 },
2340 },
2341 PrettyConfig::default()
2342 ),
2343 Err(Err(SpannedError {
2344 code: Error::InvalidValueForType {
2345 expected: String::from("i32"),
2346 found: String::from("the string \"Struct\"")
2347 },
2348 position: Position { line: 5, col: 1 }
2349 }))
2350 );
2351 }
2352
2353 #[test]
2354 fn adjacently_tagged_unit_variant_beside_map_inside_flatten_struct() {
2355 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2356 #[serde(tag = "tag", content = "content")]
2357 enum Flattened {
2358 Unit,
2359 }
2360
2361 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2362 struct FlattenedStruct {
2363 ho: i32,
2364 #[serde(flatten)]
2365 other: HashMap<String, i32>,
2366 #[serde(flatten)]
2367 flat: Flattened,
2368 }
2369
2370 assert_eq!(
2371 check_roundtrip(
2372 &FlattenedStruct {
2373 ho: 24,
2374 other: HashMap::new(),
2375 flat: Flattened::Unit,
2376 },
2377 PrettyConfig::default()
2378 ),
2379 Err(Err(SpannedError {
2380 code: Error::InvalidValueForType {
2381 expected: String::from("i32"),
2382 found: String::from("the string \"Unit\"")
2383 },
2384 position: Position { line: 4, col: 1 }
2385 }))
2386 );
2387 }
2388
2389 #[test]
2390 fn adjacently_tagged_newtype_variant_beside_map_inside_flatten_struct_variant() {
2391 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2392 #[serde(tag = "tag", content = "content")]
2393 enum Flattened {
2394 Newtype(()),
2395 }
2396
2397 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2398 enum FlattenedStructVariant {
2399 A {
2400 ho: i32,
2401 #[serde(flatten)]
2402 other: HashMap<String, i32>,
2403 #[serde(flatten)]
2404 flat: Flattened,
2405 },
2406 }
2407
2408 assert_eq!(
2409 check_roundtrip(
2410 &FlattenedStructVariant::A {
2411 ho: 24,
2412 other: HashMap::new(),
2413 flat: Flattened::Newtype(()),
2414 },
2415 PrettyConfig::default()
2416 ),
2417 Err(Err(SpannedError {
2418 code: Error::InvalidValueForType {
2419 expected: String::from("i32"),
2420 found: String::from("the string \"Newtype\"")
2421 },
2422 position: Position { line: 5, col: 1 }
2423 }))
2424 );
2425 }
2426
2427 #[test]
2428 fn adjacently_tagged_struct_variant_beside_map_inside_flatten_struct() {
2429 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2430 #[serde(tag = "tag", content = "content")]
2431 enum Flattened {
2432 Struct { hi: i32 },
2433 }
2434
2435 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2436 struct FlattenedStruct {
2437 ho: i32,
2438 #[serde(flatten)]
2439 other: HashMap<String, i32>,
2440 #[serde(flatten)]
2441 flat: Flattened,
2442 }
2443
2444 assert_eq!(
2445 check_roundtrip(
2446 &FlattenedStruct {
2447 ho: 24,
2448 other: HashMap::new(),
2449 flat: Flattened::Struct { hi: 42 },
2450 },
2451 PrettyConfig::default()
2452 ),
2453 Err(Err(SpannedError {
2454 code: Error::InvalidValueForType {
2455 expected: String::from("i32"),
2456 found: String::from("the string \"Struct\"")
2457 },
2458 position: Position { line: 7, col: 1 }
2459 }))
2460 );
2461 }
2462
2463 #[test]
2464 fn adjacently_tagged_tuple_variant_beside_map_inside_flatten_struct_variant() {
2465 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2466 #[serde(tag = "tag", content = "content")]
2467 enum Flattened {
2468 Tuple(i32, bool),
2469 }
2470
2471 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2472 enum FlattenedStructVariant {
2473 A {
2474 ho: i32,
2475 #[serde(flatten)]
2476 other: HashMap<String, i32>,
2477 #[serde(flatten)]
2478 flat: Flattened,
2479 },
2480 }
2481
2482 assert_eq!(
2483 check_roundtrip(
2484 &FlattenedStructVariant::A {
2485 ho: 24,
2486 other: HashMap::new(),
2487 flat: Flattened::Tuple(42, true),
2488 },
2489 PrettyConfig::default()
2490 ),
2491 Err(Err(SpannedError {
2492 code: Error::InvalidValueForType {
2493 expected: String::from("i32"),
2494 found: String::from("the string \"Tuple\"")
2495 },
2496 position: Position { line: 5, col: 1 }
2497 }))
2498 );
2499 }
2500
2501 #[test]
2502 fn tagged_struct_beside_map_inside_flatten_struct() {
2503 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2504 #[serde(tag = "tag")]
2505 struct Flattened {
2506 hi: i32,
2507 }
2508
2509 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2510 struct FlattenedStruct {
2511 ho: i32,
2512 #[serde(flatten)]
2513 other: HashMap<String, i32>,
2514 #[serde(flatten)]
2515 flat: Flattened,
2516 }
2517
2518 assert_eq!(
2519 check_roundtrip(
2520 &FlattenedStruct {
2521 ho: 24,
2522 other: HashMap::new(),
2523 flat: Flattened { hi: 42 },
2524 },
2525 PrettyConfig::default()
2526 ),
2527 Err(Err(SpannedError {
2528 code: Error::InvalidValueForType {
2529 expected: String::from("i32"),
2530 found: String::from("the string \"Flattened\"")
2531 },
2532 position: Position { line: 5, col: 1 }
2533 }))
2534 );
2535 }
2536
2537 #[test]
2538 fn tagged_struct_beside_map_inside_flatten_struct_variant() {
2539 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2540 #[serde(tag = "tag")]
2541 struct Flattened {
2542 hi: i32,
2543 }
2544
2545 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2546 enum FlattenedStructVariant {
2547 A {
2548 ho: i32,
2549 #[serde(flatten)]
2550 other: HashMap<String, i32>,
2551 #[serde(flatten)]
2552 flat: Flattened,
2553 },
2554 }
2555
2556 assert_eq!(
2557 check_roundtrip(
2558 &FlattenedStructVariant::A {
2559 ho: 24,
2560 other: HashMap::new(),
2561 flat: Flattened { hi: 42 },
2562 },
2563 PrettyConfig::default()
2564 ),
2565 Err(Err(SpannedError {
2566 code: Error::InvalidValueForType {
2567 expected: String::from("i32"),
2568 found: String::from("the string \"Flattened\"")
2569 },
2570 position: Position { line: 5, col: 1 }
2571 }))
2572 );
2573 }
2574
2575 #[test]
2576 fn zero_length_untagged_tuple_variant() {
2577 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2578 #[serde(untagged)]
2579 enum Untagged {
2580 A(),
2581 }
2582
2583 assert_eq!(
2584 check_roundtrip(&Untagged::A(), PrettyConfig::default()),
2585 Err(Err(SpannedError {
2586 code: Error::Message(String::from(
2587 "data did not match any variant of untagged enum Untagged"
2588 )),
2589 position: Position { line: 1, col: 3 }
2590 }))
2591 );
2592 }
2593
2594 #[test]
2595 fn zero_length_untagged_struct_variant() {
2596 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2597 #[serde(untagged)]
2598 enum Untagged {
2599 A {},
2600 }
2601
2602 assert_eq!(
2603 check_roundtrip(&Untagged::A {}, PrettyConfig::default()),
2604 Err(Err(SpannedError {
2605 code: Error::Message(String::from(
2606 "data did not match any variant of untagged enum Untagged"
2607 )),
2608 position: Position { line: 1, col: 3 }
2609 }))
2610 );
2611 }
2612
2613 #[test]
2614 fn unwrapped_one_element_untagged_tuple_variant() {
2615 // A tuple variant with just one element that is not a newtype variant
2616 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2617 #[serde(untagged)]
2618 enum Untagged {
2619 OneTuple(i32, #[serde(skip)] ()),
2620 }
2621
2622 assert_eq!(
2623 check_roundtrip(&Untagged::OneTuple(42, ()), PrettyConfig::default()),
2624 Ok(())
2625 );
2626 assert_eq!(
2627 check_roundtrip(
2628 &Untagged::OneTuple(42, ()),
2629 PrettyConfig::default().extensions(Extensions::UNWRAP_VARIANT_NEWTYPES)
2630 ),
2631 Ok(())
2632 );
2633 }
2634
2635 #[test]
2636 fn unit_inside_untagged_newtype_variant_inside_internally_tagged_newtype_variant() {
2637 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2638 #[serde(untagged)]
2639 enum Untagged {
2640 Newtype(()),
2641 }
2642
2643 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2644 #[serde(tag = "tag")]
2645 enum InternallyTagged {
2646 Newtype(Untagged),
2647 }
2648
2649 assert_eq!(
2650 check_roundtrip(
2651 &InternallyTagged::Newtype(Untagged::Newtype(())),
2652 PrettyConfig::default()
2653 ),
2654 Err(Err(SpannedError {
2655 code: Error::Message(String::from(
2656 "data did not match any variant of untagged enum Untagged"
2657 )),
2658 position: Position { line: 3, col: 2 }
2659 }))
2660 );
2661 }
2662
2663 #[test]
2664 fn unit_inside_untagged_newtype_variant_inside_flatten_struct() {
2665 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2666 #[serde(untagged)]
2667 enum Untagged {
2668 Newtype(()),
2669 }
2670
2671 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2672 struct FlattenedStruct {
2673 ho: i32,
2674 #[serde(flatten)]
2675 a: Untagged,
2676 }
2677
2678 assert_eq!(
2679 check_roundtrip(
2680 &FlattenedStruct {
2681 ho: 24,
2682 a: Untagged::Newtype(()),
2683 },
2684 PrettyConfig::default()
2685 ),
2686 Err(Err(SpannedError {
2687 code: Error::Message(String::from(
2688 "data did not match any variant of untagged enum Untagged"
2689 )),
2690 position: Position { line: 3, col: 1 }
2691 }))
2692 );
2693 }
2694
2695 #[test]
2696 fn unit_struct_inside_untagged_newtype_variant_inside_internally_tagged_newtype_variant() {
2697 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2698 struct Unit;
2699
2700 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2701 #[serde(untagged)]
2702 enum Untagged {
2703 Newtype(Unit),
2704 }
2705
2706 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2707 #[serde(tag = "tag")]
2708 enum InternallyTagged {
2709 Newtype(Untagged),
2710 }
2711
2712 assert_eq!(
2713 check_roundtrip(
2714 &InternallyTagged::Newtype(Untagged::Newtype(Unit)),
2715 PrettyConfig::default()
2716 ),
2717 Err(Err(SpannedError {
2718 code: Error::Message(String::from(
2719 "data did not match any variant of untagged enum Untagged"
2720 )),
2721 position: Position { line: 3, col: 2 }
2722 }))
2723 );
2724 }
2725
2726 #[test]
2727 fn untagged_unit_variant_inside_internally_tagged_newtype_variant() {
2728 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2729 #[serde(untagged)]
2730 enum Untagged {
2731 Unit,
2732 }
2733
2734 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2735 enum FlattenedStructVariant {
2736 C {
2737 ho: i32,
2738 #[serde(flatten)]
2739 a: Untagged,
2740 },
2741 }
2742
2743 assert_eq!(
2744 check_roundtrip(
2745 &FlattenedStructVariant::C {
2746 ho: 24,
2747 a: Untagged::Unit,
2748 },
2749 PrettyConfig::default()
2750 ),
2751 Err(Err(SpannedError {
2752 code: Error::Message(String::from(
2753 "data did not match any variant of untagged enum Untagged"
2754 )),
2755 position: Position { line: 3, col: 1 }
2756 }))
2757 );
2758 }
2759
2760 #[test]
2761 fn untagged_unit_variant_inside_flatten_struct_variant() {
2762 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2763 enum Enum {
2764 Unit,
2765 }
2766
2767 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2768 #[serde(untagged)]
2769 enum Untagged {
2770 Unit,
2771 Newtype(Enum),
2772 }
2773
2774 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2775 #[serde(tag = "tag")]
2776 enum InternallyTagged {
2777 Newtype(Untagged),
2778 }
2779
2780 assert_eq!(
2781 check_roundtrip(
2782 &InternallyTagged::Newtype(Untagged::Newtype(Enum::Unit)),
2783 PrettyConfig::default()
2784 ),
2785 Ok(())
2786 );
2787 assert_eq!(
2788 check_roundtrip(
2789 &InternallyTagged::Newtype(Untagged::Unit),
2790 PrettyConfig::default()
2791 ),
2792 Err(Err(SpannedError {
2793 code: Error::Message(String::from(
2794 "data did not match any variant of untagged enum Untagged"
2795 )),
2796 position: Position { line: 3, col: 2 }
2797 }))
2798 );
2799 }
2800
2801 #[test]
2802 fn unit_inside_internally_tagged_newtype_variant_inside_multi_flatten_struct() {
2803 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2804 struct AnotherFlattenedStruct {
2805 hi: i32,
2806 }
2807
2808 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2809 #[serde(tag = "tag")]
2810 enum InternallyTagged {
2811 Newtype(()),
2812 }
2813
2814 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2815 struct FlattenedStruct {
2816 ho: i32,
2817 #[serde(flatten)]
2818 a: InternallyTagged,
2819 #[serde(flatten)]
2820 b: AnotherFlattenedStruct,
2821 }
2822
2823 assert_eq!(
2824 check_roundtrip(
2825 &FlattenedStruct {
2826 ho: 24,
2827 a: InternallyTagged::Newtype(()),
2828 b: AnotherFlattenedStruct { hi: 42 },
2829 },
2830 PrettyConfig::default()
2831 ),
2832 Err(Err(SpannedError {
2833 code: Error::InvalidValueForType {
2834 expected: String::from("unit"),
2835 found: String::from("a map")
2836 },
2837 position: Position { line: 5, col: 1 }
2838 }))
2839 );
2840 }
2841
2842 #[test]
2843 fn untagged_unit_variant_inside_internally_tagged_newtype_variant_inside_multi_flatten_struct() {
2844 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2845 #[serde(untagged)]
2846 enum Untagged {
2847 Unit,
2848 }
2849
2850 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2851 struct AnotherFlattenedStruct {
2852 hi: i32,
2853 }
2854
2855 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2856 #[serde(tag = "tag")]
2857 enum InternallyTagged {
2858 Newtype(Untagged),
2859 }
2860
2861 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2862 struct FlattenedStruct {
2863 ho: i32,
2864 #[serde(flatten)]
2865 a: InternallyTagged,
2866 #[serde(flatten)]
2867 b: AnotherFlattenedStruct,
2868 }
2869
2870 assert_eq!(
2871 check_roundtrip(
2872 &FlattenedStruct {
2873 ho: 24,
2874 a: InternallyTagged::Newtype(Untagged::Unit),
2875 b: AnotherFlattenedStruct { hi: 42 },
2876 },
2877 PrettyConfig::default()
2878 ),
2879 Err(Err(SpannedError {
2880 code: Error::Message(String::from(
2881 "data did not match any variant of untagged enum Untagged"
2882 )),
2883 position: Position { line: 5, col: 1 }
2884 }))
2885 );
2886 }
2887
2888 #[test]
2889 fn flattened_externally_tagged_newtype_variant_beside_flattened_intenally_tagged_enum() {
2890 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2891 enum ExternallyTagged {
2892 Newtype(()),
2893 Other(()),
2894 }
2895
2896 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2897 #[serde(tag = "tag")]
2898 enum InternallyTagged {
2899 Newtype(ExternallyTagged),
2900 }
2901
2902 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2903 struct Flattened {
2904 #[serde(flatten)]
2905 a: InternallyTagged,
2906 #[serde(flatten)]
2907 b: ExternallyTagged,
2908 }
2909
2910 assert_eq!(
2911 check_roundtrip(
2912 &Flattened {
2913 a: InternallyTagged::Newtype(ExternallyTagged::Other(())),
2914 b: ExternallyTagged::Newtype(()),
2915 },
2916 PrettyConfig::default()
2917 ),
2918 Err(Err(SpannedError {
2919 code: Error::InvalidValueForType {
2920 expected: String::from("map with a single key"),
2921 found: String::from("a map"),
2922 },
2923 position: Position { line: 5, col: 1 }
2924 }))
2925 );
2926 }
2927
2928 #[test]
2929 fn flattened_externally_tagged_struct_variant_beside_flattened_intenally_tagged_enum() {
2930 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2931 enum ExternallyTagged {
2932 Struct { a: i32 },
2933 Other(()),
2934 }
2935
2936 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2937 #[serde(tag = "tag")]
2938 enum InternallyTagged {
2939 Newtype(ExternallyTagged),
2940 }
2941
2942 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2943 struct Flattened {
2944 #[serde(flatten)]
2945 a: InternallyTagged,
2946 #[serde(flatten)]
2947 b: ExternallyTagged,
2948 }
2949
2950 assert_eq!(
2951 check_roundtrip(
2952 &Flattened {
2953 a: InternallyTagged::Newtype(ExternallyTagged::Other(())),
2954 b: ExternallyTagged::Struct { a: 42 },
2955 },
2956 PrettyConfig::default()
2957 ),
2958 Err(Err(SpannedError {
2959 code: Error::InvalidValueForType {
2960 expected: String::from("map with a single key"),
2961 found: String::from("a map"),
2962 },
2963 position: Position { line: 7, col: 1 }
2964 }))
2965 );
2966 }
2967
2968 #[test]
2969 fn flattened_map_inside_option_beside_flattened_struct_variant() {
2970 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2971 #[serde(untagged)]
2972 enum Untagged {
2973 Struct { a: i32 },
2974 }
2975
2976 #[derive(PartialEq, Debug, Serialize, Deserialize)]
2977 struct Flattened {
2978 #[serde(flatten)]
2979 a: Untagged,
2980 #[serde(flatten)]
2981 b: Option<BTreeMap<String, i32>>,
2982 }
2983
2984 assert_eq!(
2985 check_roundtrip(
2986 &Flattened {
2987 a: Untagged::Struct {
2988 a: 42,
2989 },
2990 b: Some([(String::from("b"), 24)].into_iter().collect()),
2991 },
2992 PrettyConfig::default()
2993 ),
2994 Err(Ok(Error::Message(String::from("ROUNDTRIP error: Flattened { a: Struct { a: 42 }, b: Some({\"b\": 24}) } != Flattened { a: Struct { a: 42 }, b: Some({\"a\": 42, \"b\": 24}) }"))))
2995 );
2996 }
2997
2998 #[test]
2999 fn flattened_untagged_struct_beside_flattened_untagged_struct() {
3000 #[derive(PartialEq, Debug, Serialize, Deserialize)]
3001 #[serde(untagged)]
3002 enum Untagged {
3003 Struct { a: i32 },
3004 Other { b: i32 },
3005 }
3006
3007 #[derive(PartialEq, Debug, Serialize, Deserialize)]
3008 struct Flattened {
3009 #[serde(flatten)]
3010 a: Untagged,
3011 #[serde(flatten)]
3012 b: Untagged,
3013 }
3014
3015 assert_eq!(
3016 check_roundtrip(
3017 &Flattened {
3018 a: Untagged::Struct {
3019 a: 42,
3020 },
3021 b: Untagged::Other {
3022 b: 24,
3023 },
3024 },
3025 PrettyConfig::default()
3026 ),
3027 Err(Ok(Error::Message(String::from("ROUNDTRIP error: Flattened { a: Struct { a: 42 }, b: Other { b: 24 } } != Flattened { a: Struct { a: 42 }, b: Struct { a: 42 } }"))))
3028 );
3029 }
3030
3031 #[test]
3032 fn flattened_field_inside_flattened_struct_alongside_map() {
3033 #[derive(PartialEq, Debug, Serialize, Deserialize)]
3034 struct Units {
3035 a: i32,
3036 #[serde(flatten)]
3037 b: (),
3038 }
3039
3040 #[derive(PartialEq, Debug, Serialize, Deserialize)]
3041 struct Flattened {
3042 #[serde(flatten)]
3043 a: Units,
3044 #[serde(flatten)]
3045 b: BTreeMap<String, i32>,
3046 }
3047
3048 assert_eq!(
3049 check_roundtrip(
3050 &Flattened {
3051 a: Units {
3052 a: 42,
3053 b: (),
3054 },
3055 b: [(String::from("c"), 24)].into_iter().collect(),
3056 },
3057 PrettyConfig::default()
3058 ),
3059 Err(Ok(Error::Message(String::from("ROUNDTRIP error: Flattened { a: Units { a: 42, b: () }, b: {\"c\": 24} } != Flattened { a: Units { a: 42, b: () }, b: {\"a\": 42, \"c\": 24} }"))))
3060 );
3061 }
3062
3063 fn check_roundtrip<T: PartialEq + std::fmt::Debug + Serialize + serde::de::DeserializeOwned>(
3064 val: &T,
3065 config: PrettyConfig,
3066 ) -> Result<(), Result<Error, SpannedError>> {
3067 let ron = ron::ser::to_string_pretty(val, config).map_err(|err| Ok(err))?;
3068 println!("{ron}");
3069 let de = ron::from_str(&ron).map_err(|err| Err(err))?;
3070 if val == &de {
3071 Ok(())
3072 } else {
3073 Err(Ok(Error::Message(format!(
3074 "ROUNDTRIP error: {:?} != {:?}",
3075 val, de
3076 ))))
3077 }
3078 }