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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
#![warn(missing_docs)]
#[macro_use]
extern crate failure;
extern crate serde;
#[cfg(feature = "toml")]
extern crate toml;
#[cfg(feature = "json")]
extern crate serde_json;
#[cfg(feature = "yaml")]
extern crate serde_yaml;
#[cfg(feature = "ron")]
extern crate ron;
#[cfg(test)]
#[macro_use]
extern crate serde_derive;
#[cfg(test)]
#[macro_use]
mod test_util;
mod backend;
pub mod error;
pub use error::Error;
pub mod format;
pub use format::*;
pub mod de;
pub use de::*;
pub mod ser;
pub use ser::*;
#[cfg(test)]
mod tests {
use super::*;
use test_util::*;
use std::io::Cursor;
use std::fs::{remove_file, File};
use std::path::Path;
use std::io::Write;
#[test]
fn to_vec_and_back_and_to_vec_again() {
let gandalf = gandalf_the_grey();
let formats = vec![Format::Json, Format::Toml, Format::Yaml, Format::Ron];
for format in formats {
assert!(format.is_supported());
let gandalf_the_serialized = to_vec(&gandalf, format).unwrap();
let gandalf_the_deserialized: Wizard = from_slice(&gandalf_the_serialized, format).unwrap();
assert_eq!(gandalf_the_deserialized, gandalf);
let gandalf_the_serialized_again = to_vec(&gandalf_the_deserialized, format).unwrap();
assert_eq!(gandalf_the_serialized_again, gandalf_the_serialized);
}
}
#[test]
fn to_string_and_back_and_to_string_again() {
let gandalf = gandalf_the_white();
let formats = vec![Format::Json, Format::Toml, Format::Yaml, Format::Ron];
for format in formats {
assert!(format.is_supported());
let gandalf_the_serialized = to_string(&gandalf, format).unwrap();
let gandalf_the_deserialized: Wizard = from_str(&gandalf_the_serialized, format).unwrap();
assert_eq!(gandalf_the_deserialized, gandalf);
let gandalf_the_serialized_again = to_string(&gandalf_the_deserialized, format).unwrap();
assert_eq!(gandalf_the_serialized_again, gandalf_the_serialized);
}
}
#[test]
fn to_cursor_and_back_again() {
let gandalf = gandalf_the_white();
let formats = vec![Format::Json, Format::Toml, Format::Yaml, Format::Ron];
for format in formats {
assert!(format.is_supported());
let mut v: Vec<u8> = Vec::new();
to_writer(Cursor::new(&mut v), &gandalf, format).unwrap();
let gandalf_the_deserialized_from_reader: Wizard = from_reader(Cursor::new(&mut v), format).unwrap();
assert_eq!(gandalf_the_deserialized_from_reader, gandalf);
let gandalf_the_deserialized_from_slice: Wizard = from_slice(&v, format).unwrap();
assert_eq!(gandalf_the_deserialized_from_slice, gandalf);
let gandalf_the_deserialized_from_slice_any: Wizard = from_slice_any(&v).unwrap();
assert_eq!(gandalf_the_deserialized_from_slice_any, gandalf);
}
}
#[test]
fn to_file_and_back_again() {
let gandalf = gandalf_the_white();
let extensions = vec!["json", "toml", "yaml", "ron"];
let stem = Path::new("gandalf_1");
for ext in extensions {
let file_name = stem.with_extension(ext);
to_file(&file_name, &gandalf).unwrap();
let gandalf_the_deserialized: Wizard = from_file(&file_name).unwrap();
remove_file(&file_name).unwrap();
assert_eq!(gandalf_the_deserialized, gandalf);
}
}
#[test]
fn valid_but_unknown_extension() {
let gandalf = gandalf_the_white();
let json = to_vec(&gandalf, Format::Json).unwrap();
let file_name = "gandalf_2.dat";
{
let mut file = File::create(file_name).unwrap();
file.write(&json).unwrap();
}
{
let gandalf_the_deserialized: Wizard = from_file(file_name).unwrap();
assert_eq!(gandalf_the_deserialized, gandalf);
}
remove_file(file_name).unwrap();
}
#[test]
fn valid_file_stem() {
let gandalf = gandalf_the_white();
for ext in supported_extensions() {
let file_name = Path::new("gandalf_3").with_extension(ext);
to_file(&file_name, &gandalf).unwrap();
let gandalf_the_deserialized: Wizard = from_file_stem("gandalf_3").unwrap();
assert_eq!(gandalf_the_deserialized, gandalf);
remove_file(&file_name).unwrap();
}
}
}