1
use std::collections::HashMap;
2

            
3
use ron::{
4
    de::from_str,
5
    error::Error,
6
    extensions::Extensions,
7
    ser::{to_string_pretty, PrettyConfig},
8
};
9
use serde::{Deserialize, Serialize};
10

            
11
504
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
12
enum TestEnum {
13
    Unit,
14
    PrimitiveNewtype(String),
15
    Tuple(u32, bool),
16
    Struct { a: u32, b: bool },
17
    TupleNewtypeUnit(Unit),
18
    TupleNewtypeNewtype(Newtype),
19
    TupleNewtypeTuple((u32, bool)),
20
    TupleNewtypeTupleStruct(TupleStruct),
21
    TupleNewtypeStruct(Struct),
22
    TupleNewtypeEnum(Enum),
23
    TupleNewtypeOption(Option<Struct>),
24
    TupleNewtypeSeq(Vec<Struct>),
25
    TupleNewtypeMap(HashMap<u32, Struct>),
26
}
27

            
28
16
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
29
struct Unit;
30

            
31
40
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
32
struct Newtype(i32);
33

            
34
16
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
35
struct TupleStruct(u32, bool);
36

            
37
216
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
38
struct Struct {
39
    a: u32,
40
    b: bool,
41
}
42

            
43
112
#[derive(Debug, Deserialize, Serialize, Eq, PartialEq)]
44
enum Enum {
45
    A,
46
    B(Struct),
47
    C(u32, bool),
48
    D { a: u32, b: bool },
49
}
50

            
51
#[test]
52
4
fn test_deserialise_non_newtypes() {
53
4
    assert_eq!(
54
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Unit"#).unwrap(),
55
4
        TestEnum::Unit,
56
4
    );
57

            
58
4
    assert_eq!(
59
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] PrimitiveNewtype("hi")"#)
60
4
            .unwrap(),
61
4
        TestEnum::PrimitiveNewtype(String::from("hi")),
62
4
    );
63

            
64
4
    assert_eq!(
65
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Tuple(4, false)"#).unwrap(),
66
4
        TestEnum::Tuple(4, false),
67
4
    );
68

            
69
4
    assert_eq!(
70
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] Struct(a: 4, b: false)"#)
71
4
            .unwrap(),
72
4
        TestEnum::Struct { a: 4, b: false },
73
4
    );
74
4
}
75

            
76
#[test]
77
4
fn test_deserialise_tuple_newtypes() {
78
4
    assert_eq!(
79
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(Unit)"#)
80
4
            .unwrap_err()
81
4
            .code,
82
4
        Error::ExpectedStructLikeEnd,
83
4
    );
84
4
    assert_eq!(
85
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit(())"#)
86
4
            .unwrap_err()
87
4
            .code,
88
4
        Error::ExpectedStructLikeEnd,
89
4
    );
90
4
    assert_eq!(
91
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeUnit()"#).unwrap(),
92
4
        TestEnum::TupleNewtypeUnit(Unit),
93
4
    );
94

            
95
4
    assert_eq!(
96
4
        from_str::<TestEnum>(
97
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(Newtype(4))"#
98
4
        )
99
4
        .unwrap_err()
100
4
        .code,
101
4
        Error::ExpectedInteger,
102
4
    );
103
4
    assert_eq!(
104
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype((4))"#)
105
4
            .unwrap_err()
106
4
            .code,
107
4
        Error::ExpectedInteger,
108
4
    );
109
4
    assert_eq!(
110
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#)
111
4
            .unwrap(),
112
4
        TestEnum::TupleNewtypeNewtype(Newtype(4)),
113
4
    );
114
4
    assert_eq!(
115
4
        from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(),
116
4
        TestEnum::TupleNewtypeNewtype(Newtype(4)),
117
4
    );
118
4
    assert_eq!(
119
4
        from_str::<TestEnum>(r#"#![enable(unwrap_newtypes)] #![enable(unwrap_variant_newtypes)] TupleNewtypeNewtype(4)"#).unwrap(),
120
4
        TestEnum::TupleNewtypeNewtype(Newtype(4)),
121
4
    );
122

            
123
4
    assert_eq!(
124
4
        from_str::<TestEnum>(
125
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple((4, false))"#
126
4
        )
127
4
        .unwrap_err()
128
4
        .code,
129
4
        Error::ExpectedInteger,
130
4
    );
131
4
    assert_eq!(
132
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTuple(4, false)"#)
133
4
            .unwrap(),
134
4
        TestEnum::TupleNewtypeTuple((4, false)),
135
4
    );
136

            
137
4
    assert_eq!(
138
4
        from_str::<TestEnum>(
139
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(TupleStruct(4, false))"#
140
4
        )
141
4
        .unwrap_err()
142
4
        .code,
143
4
        Error::ExpectedInteger,
144
4
    );
145
4
    assert_eq!(
146
4
        from_str::<TestEnum>(
147
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct((4, false))"#
148
4
        )
149
4
        .unwrap_err()
150
4
        .code,
151
4
        Error::ExpectedInteger,
152
4
    );
153
4
    assert_eq!(
154
4
        from_str::<TestEnum>(
155
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeTupleStruct(4, false)"#
156
4
        )
157
4
        .unwrap(),
158
4
        TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)),
159
4
    );
160

            
161
4
    assert_eq!(
162
4
        from_str::<TestEnum>(
163
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(Struct(a: 4, b: false))"#
164
4
        )
165
4
        .unwrap_err()
166
4
        .code,
167
4
        Error::ExpectedMapColon,
168
4
    );
169
4
    assert_eq!(
170
4
        from_str::<TestEnum>(
171
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct((a: 4, b: false))"#
172
4
        )
173
4
        .unwrap_err()
174
4
        .code,
175
4
        Error::ExpectedIdentifier,
176
4
    );
177
4
    assert_eq!(
178
4
        from_str::<TestEnum>(
179
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeStruct(a: 4, b: false)"#
180
4
        )
181
4
        .unwrap(),
182
4
        TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }),
183
4
    );
184

            
185
4
    assert_eq!(
186
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(A)"#).unwrap(),
187
4
        TestEnum::TupleNewtypeEnum(Enum::A),
188
4
    );
189
4
    assert_eq!(
190
4
        from_str::<TestEnum>(
191
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(B(a: 4, b: false))"#
192
4
        )
193
4
        .unwrap(),
194
4
        TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })),
195
4
    );
196
4
    assert_eq!(
197
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C 4, false)"#)
198
4
            .unwrap_err()
199
4
            .code,
200
4
        Error::ExpectedStructLike,
201
4
    );
202
4
    assert_eq!(
203
4
        from_str::<TestEnum>(
204
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C(4, false))"#
205
4
        )
206
4
        .unwrap(),
207
4
        TestEnum::TupleNewtypeEnum(Enum::C(4, false)),
208
4
    );
209
4
    assert_eq!(
210
4
        from_str::<TestEnum>(
211
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D a: 4, b: false)"#
212
4
        )
213
4
        .unwrap_err()
214
4
        .code,
215
4
        Error::ExpectedStructLike,
216
4
    );
217
4
    assert_eq!(
218
4
        from_str::<TestEnum>(
219
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D(a: 4, b: false))"#
220
4
        )
221
4
        .unwrap(),
222
4
        TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }),
223
4
    );
224

            
225
4
    assert_eq!(
226
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(None)"#)
227
4
            .unwrap(),
228
4
        TestEnum::TupleNewtypeOption(None),
229
4
    );
230
4
    assert_eq!(
231
4
        from_str::<TestEnum>(
232
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"#
233
4
        )
234
4
        .unwrap(),
235
4
        TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
236
4
    );
237
4
    assert_eq!(
238
4
        from_str::<TestEnum>(
239
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(a: 4, b: false)"#
240
4
        )
241
4
        .unwrap_err()
242
4
        .code,
243
4
        Error::ExpectedOption,
244
4
    );
245
4
    assert_eq!(
246
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes, implicit_some)] TupleNewtypeOption(a: 4, b: false)"#).unwrap(),
247
4
        TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
248
4
    );
249

            
250
4
    assert_eq!(
251
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([])"#).unwrap(),
252
4
        TestEnum::TupleNewtypeSeq(vec![]),
253
4
    );
254
4
    assert_eq!(
255
4
        from_str::<TestEnum>(
256
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([(a: 4, b: false)])"#
257
4
        )
258
4
        .unwrap(),
259
4
        TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]),
260
4
    );
261
4
    assert_eq!(
262
4
        from_str::<TestEnum>(
263
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([Struct(a: 4, b: false)])"#
264
4
        )
265
4
        .unwrap(),
266
4
        TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]),
267
4
    );
268

            
269
4
    assert_eq!(
270
4
        from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({})"#).unwrap(),
271
4
        TestEnum::TupleNewtypeMap(vec![].into_iter().collect()),
272
4
    );
273
4
    assert_eq!(
274
4
        from_str::<TestEnum>(
275
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({2: (a: 4, b: false)})"#
276
4
        )
277
4
        .unwrap(),
278
4
        TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()),
279
4
    );
280
4
    assert_eq!(
281
4
        from_str::<TestEnum>(
282
4
            r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({8: Struct(a: 4, b: false)})"#
283
4
        )
284
4
        .unwrap(),
285
4
        TestEnum::TupleNewtypeMap(vec![(8, Struct { a: 4, b: false })].into_iter().collect()),
286
4
    );
287
4
}
288

            
289
#[test]
290
4
fn test_newtype_some() {
291
4
    assert_eq!(
292
4
        from_str::<Option<Struct>>(r#"Some((a: 4, b: false))"#).unwrap(),
293
4
        Some(Struct { a: 4, b: false }),
294
4
    );
295

            
296
4
    assert_eq!(
297
4
        from_str::<Option<Struct>>(r#"Some(a: 4, b: false)"#)
298
4
            .unwrap_err()
299
4
            .code,
300
4
        Error::ExpectedDifferentStructName {
301
4
            expected: "Struct",
302
4
            found: String::from("a"),
303
4
        },
304
4
    );
305

            
306
4
    assert_eq!(
307
4
        from_str::<Option<Struct>>(r#"#![enable(unwrap_variant_newtypes)] Some(a: 4, b: false)"#)
308
4
            .unwrap(),
309
4
        Some(Struct { a: 4, b: false }),
310
4
    );
311
4
}
312

            
313
#[test]
314
4
fn test_serialise_non_newtypes() {
315
4
    assert_eq_serialize_roundtrip(TestEnum::Unit, Extensions::UNWRAP_VARIANT_NEWTYPES);
316
4

            
317
4
    assert_eq_serialize_roundtrip(
318
4
        TestEnum::PrimitiveNewtype(String::from("hi")),
319
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
320
4
    );
321
4

            
322
4
    assert_eq_serialize_roundtrip(
323
4
        TestEnum::Tuple(4, false),
324
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
325
4
    );
326
4

            
327
4
    assert_eq_serialize_roundtrip(
328
4
        TestEnum::Struct { a: 4, b: false },
329
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
330
4
    );
331
4
}
332

            
333
#[test]
334
4
fn test_serialise_tuple_newtypes() {
335
4
    assert_eq_serialize_roundtrip(
336
4
        TestEnum::TupleNewtypeUnit(Unit),
337
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
338
4
    );
339
4

            
340
4
    assert_eq_serialize_roundtrip(
341
4
        TestEnum::TupleNewtypeNewtype(Newtype(4)),
342
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
343
4
    );
344
4
    assert_eq_serialize_roundtrip(
345
4
        TestEnum::TupleNewtypeNewtype(Newtype(4)),
346
4
        Extensions::UNWRAP_NEWTYPES,
347
4
    );
348
4
    assert_eq_serialize_roundtrip(
349
4
        TestEnum::TupleNewtypeNewtype(Newtype(4)),
350
4
        Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::UNWRAP_NEWTYPES,
351
4
    );
352
4

            
353
4
    assert_eq_serialize_roundtrip(
354
4
        TestEnum::TupleNewtypeTuple((4, false)),
355
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
356
4
    );
357
4

            
358
4
    assert_eq_serialize_roundtrip(
359
4
        TestEnum::TupleNewtypeTupleStruct(TupleStruct(4, false)),
360
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
361
4
    );
362
4

            
363
4
    assert_eq_serialize_roundtrip(
364
4
        TestEnum::TupleNewtypeStruct(Struct { a: 4, b: false }),
365
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
366
4
    );
367
4

            
368
4
    assert_eq_serialize_roundtrip(
369
4
        TestEnum::TupleNewtypeEnum(Enum::A),
370
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
371
4
    );
372
4
    assert_eq_serialize_roundtrip(
373
4
        TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })),
374
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
375
4
    );
376
4
    assert_eq_serialize_roundtrip(
377
4
        TestEnum::TupleNewtypeEnum(Enum::C(4, false)),
378
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
379
4
    );
380
4
    assert_eq_serialize_roundtrip(
381
4
        TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }),
382
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
383
4
    );
384
4

            
385
4
    assert_eq_serialize_roundtrip(
386
4
        TestEnum::TupleNewtypeOption(None),
387
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
388
4
    );
389
4
    assert_eq_serialize_roundtrip(
390
4
        TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
391
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
392
4
    );
393
4
    assert_eq_serialize_roundtrip(
394
4
        TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
395
4
        Extensions::IMPLICIT_SOME,
396
4
    );
397
4
    assert_eq_serialize_roundtrip(
398
4
        TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
399
4
        Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::IMPLICIT_SOME,
400
4
    );
401
4

            
402
4
    assert_eq_serialize_roundtrip(
403
4
        TestEnum::TupleNewtypeSeq(vec![]),
404
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
405
4
    );
406
4
    assert_eq_serialize_roundtrip(
407
4
        TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]),
408
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
409
4
    );
410
4

            
411
4
    assert_eq_serialize_roundtrip(
412
4
        TestEnum::TupleNewtypeMap(vec![].into_iter().collect()),
413
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
414
4
    );
415
4
    assert_eq_serialize_roundtrip(
416
4
        TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()),
417
4
        Extensions::UNWRAP_VARIANT_NEWTYPES,
418
4
    );
419
4
}
420

            
421
92
fn assert_eq_serialize_roundtrip<
422
92
    S: Serialize + serde::de::DeserializeOwned + PartialEq + std::fmt::Debug,
423
92
>(
424
92
    value: S,
425
92
    extensions: Extensions,
426
92
) {
427
92
    let ron = to_string_pretty(&value, PrettyConfig::default().extensions(extensions)).unwrap();
428
92
    let result = from_str::<S>(&ron);
429
92

            
430
92
    assert_eq!(result.as_ref(), Ok(&value), "{}", ron,);
431
92
}