summaryrefslogtreecommitdiffstats
path: root/third_party/rust/ron/tests/extensions.rs
blob: 6161d90b43336ecd557a5f46ec15e2f043d7e332 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct UnitStruct;

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct NewType(f32);

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct TupleStruct(UnitStruct, i8);

#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
struct Key(u32);

#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
enum Enum {
    Unit,
    Bool(bool),
    Chars(char, String),
}

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Struct {
    tuple: ((), NewType, TupleStruct),
    vec: Vec<Option<UnitStruct>>,
    map: HashMap<Key, Enum>,
}

const CONFIG_U_NT: &str = "
#![enable(unwrap_newtypes)]

(
    tuple: ((), 0.5, ((), -5)),
    vec: [
        None,
        Some(()),
    ],
    map: {
        7: Bool(true),
        9: Chars('x', \"\"),
        6: Bool(false),
        5: Unit,
    },
)
";

#[test]
fn unwrap_newtypes() {
    let d: Struct = ron::de::from_str(CONFIG_U_NT).expect("Failed to deserialize");

    println!("unwrap_newtypes: {:#?}", d);

    let s = ron::ser::to_string_pretty(
        &d,
        ron::ser::PrettyConfig::default().extensions(ron::extensions::Extensions::UNWRAP_NEWTYPES),
    )
    .expect("Failed to serialize");

    let d2: Struct = ron::de::from_str(&s).expect("Failed to deserialize");

    assert_eq!(d, d2);
}

const CONFIG_I_S: &str = "
#![enable(implicit_some)]

(
    tuple: ((), (0.5), ((), -5)),
    vec: [
        None,
        (),
        UnitStruct,
        None,
        (),
    ],
    map: {
        (7): Bool(true),
        (9): Chars('x', \"\"),
        (6): Bool(false),
        (5): Unit,
    },
)
";

#[test]
fn implicit_some() {
    let d: Struct = ron::de::from_str(CONFIG_I_S).expect("Failed to deserialize");

    println!("implicit_some: {:#?}", d);
}