Debian Codegraph @

tests/502_known_bugs.rs

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