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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use ron::extensions::Extensions;
#[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>,
}
#[test]
fn roundtrip() {
let value = Struct {
tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)),
vec: vec![None, Some(UnitStruct)],
map: vec![
(Key(5), Enum::Unit),
(Key(6), Enum::Bool(false)),
(Key(7), Enum::Bool(true)),
(Key(9), Enum::Chars('x', "".to_string())),
]
.into_iter()
.collect(),
};
let serial = ron::ser::to_string(&value).unwrap();
println!("Serialized: {}", serial);
let deserial = ron::de::from_str(&serial);
assert_eq!(Ok(value), deserial);
}
#[test]
fn roundtrip_pretty() {
let value = Struct {
tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)),
vec: vec![None, Some(UnitStruct)],
map: vec![
(Key(5), Enum::Unit),
(Key(6), Enum::Bool(false)),
(Key(7), Enum::Bool(true)),
(Key(9), Enum::Chars('x', "".to_string())),
]
.into_iter()
.collect(),
};
let pretty = ron::ser::PrettyConfig::new()
.enumerate_arrays(true)
.extensions(Extensions::IMPLICIT_SOME);
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();
println!("Serialized: {}", serial);
let deserial = ron::de::from_str(&serial);
assert_eq!(Ok(value), deserial);
}
#[test]
fn roundtrip_sep_tuple_members() {
#[derive(Debug, Deserialize, PartialEq, Eq, Serialize)]
pub enum FileOrMem {
File(String),
Memory,
}
#[derive(Debug, Deserialize, PartialEq, Serialize)]
struct Both {
a: Struct,
b: FileOrMem,
}
let a = Struct {
tuple: ((), NewType(0.5), TupleStruct(UnitStruct, -5)),
vec: vec![None, Some(UnitStruct)],
map: vec![
(Key(5), Enum::Unit),
(Key(6), Enum::Bool(false)),
(Key(7), Enum::Bool(true)),
(Key(9), Enum::Chars('x', "".to_string())),
]
.into_iter()
.collect(),
};
let b = FileOrMem::File("foo".to_owned());
let value = Both { a, b };
let pretty = ron::ser::PrettyConfig::new().separate_tuple_members(true);
let serial = ron::ser::to_string_pretty(&value, pretty).unwrap();
println!("Serialized: {}", serial);
let deserial = ron::de::from_str(&serial);
assert_eq!(Ok(value), deserial);
}
|