1
use std::collections::HashMap;
2

            
3
use serde::{Deserialize, Serialize};
4

            
5
84
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
6
struct Main {
7
    #[serde(flatten)]
8
    required: Required,
9
    #[serde(flatten)]
10
    optional: Optional,
11

            
12
    some_other_field: u32,
13
}
14

            
15
24
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
16
struct Required {
17
    first: u32,
18
    second: u32,
19
}
20

            
21
16
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
22
struct Optional {
23
    third: Option<u32>,
24
}
25

            
26
72
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
27
struct MyType {
28
    first: u32,
29
    second: u32,
30
    #[serde(flatten)]
31
    everything_else: HashMap<String, ron::Value>,
32
}
33

            
34
8
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
35
struct AllOptional {
36
    #[serde(flatten)]
37
    everything_else: HashMap<String, ron::Value>,
38
}
39

            
40
24
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
41
enum Newtype {
42
    Main(Main),
43
    MyType(MyType),
44
    AllOptional(AllOptional),
45
}
46

            
47
#[test]
48
4
fn test_flatten_struct_into_struct() {
49
4
    let val = Main {
50
4
        required: Required {
51
4
            first: 1,
52
4
            second: 2,
53
4
        },
54
4
        optional: Optional { third: Some(3) },
55
4
        some_other_field: 1337,
56
4
    };
57
4

            
58
4
    let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
59
4

            
60
4
    assert_eq!(
61
4
        ron,
62
4
        "{
63
4
    \"first\": 1,
64
4
    \"second\": 2,
65
4
    \"third\": Some(3),
66
4
    \"some_other_field\": 1337,
67
4
}"
68
4
    );
69

            
70
4
    let de: Main = ron::from_str(&ron).unwrap();
71
4

            
72
4
    assert_eq!(de, val);
73

            
74
4
    let val = Newtype::Main(Main {
75
4
        required: Required {
76
4
            first: 1,
77
4
            second: 2,
78
4
        },
79
4
        optional: Optional { third: Some(3) },
80
4
        some_other_field: 1337,
81
4
    });
82
4

            
83
4
    let ron = ron::ser::to_string_pretty(
84
4
        &val,
85
4
        ron::ser::PrettyConfig::default()
86
4
            .extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
87
4
    )
88
4
    .unwrap();
89
4

            
90
4
    assert_eq!(
91
4
        ron,
92
4
        "#![enable(unwrap_variant_newtypes)]
93
4
Main({
94
4
    \"first\": 1,
95
4
    \"second\": 2,
96
4
    \"third\": Some(3),
97
4
    \"some_other_field\": 1337,
98
4
})"
99
4
    );
100

            
101
4
    let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
102
4

            
103
4
    let de: Newtype = ron::from_str(&ron).unwrap();
104
4

            
105
4
    assert_eq!(de, val);
106

            
107
4
    assert_eq!(
108
4
        ron::from_str::<Main>(
109
4
            "{
110
4
        first\": 1,
111
4
        \"second\": 2,
112
4
        \"third\": Some(3),
113
4
        \"some_other_field\": 1337,
114
4
    }"
115
4
        ),
116
4
        Err(ron::error::SpannedError {
117
4
            code: ron::error::Error::ExpectedString,
118
4
            position: ron::error::Position { line: 2, col: 9 },
119
4
        })
120
4
    );
121

            
122
4
    assert_eq!(
123
4
        ron::from_str::<Main>(
124
4
            "{
125
4
        \"first\": 1,
126
4
        \"second: 2,
127
4
        \"third\": Some(3),
128
4
        \"some_other_field\": 1337,
129
4
    }"
130
4
        ),
131
4
        Err(ron::error::SpannedError {
132
4
            code: ron::error::Error::ExpectedMapColon,
133
4
            position: ron::error::Position { line: 4, col: 10 },
134
4
        })
135
4
    );
136

            
137
4
    assert_eq!(
138
4
        ron::from_str::<Main>(
139
4
            "{
140
4
        \"first\": 1,
141
4
        \"second\": 2,
142
4
        third\": Some(3),
143
4
        \"some_other_field\": 1337,
144
4
    }"
145
4
        ),
146
4
        Err(ron::error::SpannedError {
147
4
            code: ron::error::Error::ExpectedString,
148
4
            position: ron::error::Position { line: 4, col: 9 },
149
4
        })
150
4
    );
151

            
152
4
    assert_eq!(
153
4
        ron::from_str::<Main>(
154
4
            "{
155
4
        \"first\": 1,
156
4
        \"second\": 2,
157
4
        \"third\": Some(3),
158
4
        \"some_other_field: 1337,
159
4
    }"
160
4
        ),
161
4
        Err(ron::error::SpannedError {
162
4
            code: ron::error::Error::ExpectedStringEnd,
163
4
            position: ron::error::Position { line: 5, col: 10 },
164
4
        })
165
4
    );
166
4
}
167

            
168
#[test]
169
4
fn test_flatten_rest() {
170
4
    let val = MyType {
171
4
        first: 1,
172
4
        second: 2,
173
4
        everything_else: {
174
4
            let mut map = HashMap::new();
175
4
            map.insert(
176
4
                String::from("third"),
177
4
                ron::Value::Number(ron::value::Number::U8(3)),
178
4
            );
179
4
            map
180
4
        },
181
4
    };
182
4

            
183
4
    let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
184
4

            
185
4
    assert_eq!(
186
4
        ron,
187
4
        "{
188
4
    \"first\": 1,
189
4
    \"second\": 2,
190
4
    \"third\": 3,
191
4
}"
192
4
    );
193

            
194
4
    let de: MyType = ron::from_str(&ron).unwrap();
195
4

            
196
4
    assert_eq!(de, val);
197

            
198
4
    let val = Newtype::MyType(MyType {
199
4
        first: 1,
200
4
        second: 2,
201
4
        everything_else: {
202
4
            let mut map = HashMap::new();
203
4
            map.insert(
204
4
                String::from("third"),
205
4
                ron::Value::Number(ron::value::Number::U8(3)),
206
4
            );
207
4
            map
208
4
        },
209
4
    });
210
4

            
211
4
    let ron = ron::ser::to_string_pretty(
212
4
        &val,
213
4
        ron::ser::PrettyConfig::default()
214
4
            .extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
215
4
    )
216
4
    .unwrap();
217
4

            
218
4
    assert_eq!(
219
4
        ron,
220
4
        "#![enable(unwrap_variant_newtypes)]
221
4
MyType({
222
4
    \"first\": 1,
223
4
    \"second\": 2,
224
4
    \"third\": 3,
225
4
})"
226
4
    );
227

            
228
4
    let de: Newtype = ron::from_str(&ron).unwrap();
229
4

            
230
4
    assert_eq!(de, val);
231

            
232
4
    assert_eq!(
233
4
        ron::from_str::<MyType>(
234
4
            "{
235
4
        first\": 1,
236
4
        \"second\": 2,
237
4
        \"third\": 3,
238
4
    }"
239
4
        ),
240
4
        Err(ron::error::SpannedError {
241
4
            code: ron::error::Error::ExpectedString,
242
4
            position: ron::error::Position { line: 2, col: 9 },
243
4
        })
244
4
    );
245

            
246
4
    assert_eq!(
247
4
        ron::from_str::<MyType>(
248
4
            "{
249
4
        \"first\": 1,
250
4
        \"second: 2,
251
4
        \"third\": 3,
252
4
    }"
253
4
        ),
254
4
        Err(ron::error::SpannedError {
255
4
            code: ron::error::Error::ExpectedMapColon,
256
4
            position: ron::error::Position { line: 4, col: 10 },
257
4
        })
258
4
    );
259

            
260
4
    assert_eq!(
261
4
        ron::from_str::<MyType>(
262
4
            "{
263
4
        \"first\": 1,
264
4
        \"second\": 2,
265
4
        third\": 3,
266
4
    }"
267
4
        ),
268
4
        Err(ron::error::SpannedError {
269
4
            code: ron::error::Error::ExpectedString,
270
4
            position: ron::error::Position { line: 4, col: 9 },
271
4
        })
272
4
    );
273

            
274
4
    assert_eq!(
275
4
        ron::from_str::<MyType>(
276
4
            "{
277
4
        \"first\": 1,
278
4
        \"second\": 2,
279
4
        \"third: 3,
280
4
    }"
281
4
        ),
282
4
        Err(ron::error::SpannedError {
283
4
            code: ron::error::Error::ExpectedStringEnd,
284
4
            position: ron::error::Position { line: 4, col: 10 },
285
4
        })
286
4
    );
287
4
}
288

            
289
#[test]
290
4
fn test_flatten_only_rest() {
291
4
    let val = AllOptional {
292
4
        everything_else: HashMap::new(),
293
4
    };
294
4

            
295
4
    let ron = ron::ser::to_string(&val).unwrap();
296
4

            
297
4
    assert_eq!(ron, "{}");
298

            
299
4
    let de: AllOptional = ron::from_str(&ron).unwrap();
300
4

            
301
4
    assert_eq!(de, val);
302

            
303
4
    let val = Newtype::AllOptional(AllOptional {
304
4
        everything_else: HashMap::new(),
305
4
    });
306
4

            
307
4
    let ron = ron::ser::to_string_pretty(
308
4
        &val,
309
4
        ron::ser::PrettyConfig::default()
310
4
            .extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
311
4
    )
312
4
    .unwrap();
313
4

            
314
4
    assert_eq!(
315
4
        ron,
316
4
        "#![enable(unwrap_variant_newtypes)]
317
4
AllOptional({
318
4
})"
319
4
    );
320

            
321
4
    let de: Newtype = ron::from_str(&ron).unwrap();
322
4

            
323
4
    assert_eq!(de, val);
324
4
}
325

            
326
12
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
327
#[serde(deny_unknown_fields)]
328
pub struct AvailableCards {
329
    pub left: u8,
330
    pub right: u8,
331
}
332

            
333
4
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
334
struct MapProperties {
335
    #[serde(flatten)]
336
    cards: AvailableCards,
337
}
338

            
339
#[test]
340
4
fn test_issue_456() {
341
4
    let map_properties = MapProperties {
342
4
        cards: AvailableCards {
343
4
            ..Default::default()
344
4
        },
345
4
    };
346
4
    let ron = ron::to_string(&map_properties).unwrap();
347
4

            
348
4
    let de: MapProperties = ron::from_str(&ron).unwrap();
349
4

            
350
4
    assert_eq!(map_properties, de);
351
4
}