Debian Codegraph @

tests/508_value_adjacently_tagged_bug.rs

raw symbols SCIP
1 use serde::{Deserialize, Serialize};
2
3 #[derive(Debug, PartialEq, Serialize, Deserialize)]
4 #[serde(tag = "type", content = "value")]
5 enum TheEnum {
6 Variant([f32; 3]),
7 }
8
9 #[test]
10 fn roundtrip_through_value() {
11 let value = TheEnum::Variant([0.1, 0.1, 0.1]);
12
13 let ron = ron::to_string(&value).unwrap();
14 assert_eq!(ron, "(type:Variant,value:(0.1,0.1,0.1))");
15
16 let de = ron::from_str::<TheEnum>(&ron).unwrap();
17 assert_eq!(de, value);
18
19 let ron_value = ron::from_str::<ron::Value>(&ron).unwrap();
20
21 // Known bug: ron::Value only stores a unit, cannot find a variant
22 let err = ron_value.into_rust::<TheEnum>().unwrap_err();
23 assert_eq!(
24 err,
25 ron::Error::InvalidValueForType {
26 expected: String::from("variant of enum TheEnum"),
27 found: String::from("a unit value")
28 }
29 );
30
31 let old_serde_ron: &str = "(type:\"Variant\",value:(0.1,0.1,0.1))";
32
33 // Known bug: serde no longer uses strings in > v1.0.180 to deserialize the variant
34 let err = ron::from_str::<TheEnum>(&old_serde_ron).unwrap_err();
35 assert_eq!(
36 err,
37 ron::error::SpannedError {
38 code: ron::Error::ExpectedIdentifier,
39 span: ron::error::Span {
40 start: ron::error::Position { line: 1, col: 7 },
41 end: ron::error::Position { line: 1, col: 7 },
42 }
43 }
44 );
45
46 let ron_value = ron::from_str::<ron::Value>(&old_serde_ron).unwrap();
47
48 // Known bug: ron::Value is asked for an enum but has no special handling for it (yet)
49 let err = ron_value.into_rust::<TheEnum>().unwrap_err();
50 assert_eq!(
51 err,
52 ron::Error::InvalidValueForType {
53 expected: String::from("variant of enum TheEnum"),
54 found: String::from("the string \"Variant\"")
55 }
56 );
57
58 // This still works, but is a bug as well
59 let ron_value = ron::from_str::<ron::Value>("(\"Variant\",(0.1,0.1,0.1))").unwrap();
60 let de: TheEnum = ron_value.into_rust::<TheEnum>().unwrap();
61 assert_eq!(de, value);
62 }