1
use serde::{Deserialize, Serialize};
2

            
3
28
#[derive(Debug, PartialEq, Serialize, Deserialize)]
4
#[serde(tag = "type", content = "value")]
5
enum TheEnum {
6
    Variant([f32; 3]),
7
}
8

            
9
#[test]
10
4
fn roundtrip_through_value() {
11
4
    let value = TheEnum::Variant([0.1, 0.1, 0.1]);
12
4

            
13
4
    let ron = ron::to_string(&value).unwrap();
14
4
    assert_eq!(ron, "(type:Variant,value:(0.1,0.1,0.1))");
15

            
16
4
    let de = ron::from_str::<TheEnum>(&ron).unwrap();
17
4
    assert_eq!(de, value);
18

            
19
4
    let ron_value = ron::from_str::<ron::Value>(&ron).unwrap();
20
4

            
21
4
    // Known bug: ron::Value only stores a unit, cannot find a variant
22
4
    let err = ron_value.into_rust::<TheEnum>().unwrap_err();
23
4
    assert_eq!(
24
4
        err,
25
4
        ron::Error::InvalidValueForType {
26
4
            expected: String::from("variant of enum TheEnum"),
27
4
            found: String::from("a unit value")
28
4
        }
29
4
    );
30

            
31
4
    let old_serde_ron: &str = "(type:\"Variant\",value:(0.1,0.1,0.1))";
32
4

            
33
4
    // Known bug: serde no longer uses strings in > v1.0.180 to deserialize the variant
34
4
    let err = ron::from_str::<TheEnum>(&old_serde_ron).unwrap_err();
35
4
    assert_eq!(
36
4
        err,
37
4
        ron::error::SpannedError {
38
4
            code: ron::Error::ExpectedIdentifier,
39
4
            position: ron::error::Position { line: 1, col: 7 },
40
4
        }
41
4
    );
42

            
43
4
    let ron_value = ron::from_str::<ron::Value>(&old_serde_ron).unwrap();
44
4

            
45
4
    // Known bug: ron::Value is asked for an enum but has no special handling for it (yet)
46
4
    let err = ron_value.into_rust::<TheEnum>().unwrap_err();
47
4
    assert_eq!(
48
4
        err,
49
4
        ron::Error::InvalidValueForType {
50
4
            expected: String::from("variant of enum TheEnum"),
51
4
            found: String::from("the string \"Variant\"")
52
4
        }
53
4
    );
54

            
55
    // This still works, but is a bug as well
56
4
    let ron_value = ron::from_str::<ron::Value>("(\"Variant\",(0.1,0.1,0.1))").unwrap();
57
4
    let de: TheEnum = ron_value.into_rust::<TheEnum>().unwrap();
58
4
    assert_eq!(de, value);
59
4
}