1
use std::collections::HashMap;
2

            
3
use ron::extensions::Extensions;
4
use serde::{Deserialize, Serialize};
5

            
6
24
#[derive(Debug, PartialEq, Deserialize, Serialize)]
7
struct UnitStruct;
8

            
9
12
#[derive(Debug, PartialEq, Deserialize, Serialize)]
10
struct NewType(f32);
11

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

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

            
18
108
#[derive(Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
19
enum Enum {
20
    Unit,
21
    Bool(bool),
22
    Chars(char, String),
23
}
24

            
25
48
#[derive(Debug, PartialEq, Deserialize, Serialize)]
26
struct Struct {
27
    tuple: ((), NewType, TupleStruct),
28
    vec: Vec<Option<UnitStruct>>,
29
    map: HashMap<Key, Enum>,
30
}
31

            
32
#[test]
33
4
fn roundtrip() {
34
4
    let value = Struct {
35
4
        tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)),
36
4
        vec: vec![None, Some(UnitStruct)],
37
4
        map: vec![
38
4
            (Key(5), Enum::Unit),
39
4
            (Key(6), Enum::Bool(false)),
40
4
            (Key(7), Enum::Bool(true)),
41
4
            (Key(9), Enum::Chars('x', "".to_string())),
42
4
        ]
43
4
        .into_iter()
44
4
        .collect(),
45
4
    };
46
4

            
47
4
    let serial = ron::ser::to_string(&value).unwrap();
48
4

            
49
4
    println!("Serialized: {}", serial);
50
4

            
51
4
    let deserial = ron::de::from_str(&serial);
52
4

            
53
4
    assert_eq!(Ok(value), deserial);
54
4
}
55

            
56
#[test]
57
4
fn roundtrip_pretty() {
58
4
    let value = Struct {
59
4
        tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)),
60
4
        vec: vec![None, Some(UnitStruct)],
61
4
        map: vec![
62
4
            (Key(5), Enum::Unit),
63
4
            (Key(6), Enum::Bool(false)),
64
4
            (Key(7), Enum::Bool(true)),
65
4
            (Key(9), Enum::Chars('x', "".to_string())),
66
4
        ]
67
4
        .into_iter()
68
4
        .collect(),
69
4
    };
70
4

            
71
4
    let pretty = ron::ser::PrettyConfig::new()
72
4
        .enumerate_arrays(true)
73
4
        .extensions(Extensions::IMPLICIT_SOME);
74
4
    let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();
75
4

            
76
4
    println!("Serialized: {}", serial);
77
4

            
78
4
    let deserial = ron::de::from_str(&serial);
79
4

            
80
4
    assert_eq!(Ok(value), deserial);
81
4
}
82

            
83
#[test]
84
4
fn roundtrip_sep_tuple_members() {
85
8
    #[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
86
    pub enum FileOrMem {
87
        File(String),
88
        Memory,
89
    }
90

            
91
12
    #[derive(Debug, Deserialize, PartialEq, Serialize)]
92
    struct Both {
93
        a: Struct,
94
        b: FileOrMem,
95
    }
96

            
97
4
    let a = Struct {
98
4
        tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)),
99
4
        vec: vec![None, Some(UnitStruct)],
100
4
        map: vec![
101
4
            (Key(5), Enum::Unit),
102
4
            (Key(6), Enum::Bool(false)),
103
4
            (Key(7), Enum::Bool(true)),
104
4
            (Key(9), Enum::Chars('x', "".to_string())),
105
4
        ]
106
4
        .into_iter()
107
4
        .collect(),
108
4
    };
109
4
    let b = FileOrMem::File("foo".to_owned());
110
4

            
111
4
    let value = Both { a, b };
112
4

            
113
4
    let pretty = ron::ser::PrettyConfig::new().separate_tuple_members(true);
114
4
    let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();
115
4

            
116
4
    println!("Serialized: {}", serial);
117
4

            
118
4
    let deserial = ron::de::from_str(&serial);
119
4

            
120
4
    assert_eq!(Ok(value), deserial);
121
4
}