File: 526_flatten.rs

package info (click to toggle)
rust-ron 0.12.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,096 kB
  • sloc: makefile: 2
file content (54 lines) | stat: -rw-r--r-- 1,252 bytes parent folder | download | duplicates (20)
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
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct SomeCollection {
    inner: Vec<SomeItem>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct SomeItem {
    #[serde(flatten)]
    foo: Foo,
    #[serde(flatten)]
    bar: Bar,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Bar {
    name: String,
    some_enum: Option<SomeEnum>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Foo {
    something: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
enum SomeEnum {
    A,
    B,
}

#[test]
fn roundtrip() {
    let scene = SomeCollection {
        inner: vec![SomeItem {
            foo: Foo {
                something: "something".to_string(),
            },
            bar: Bar {
                name: "name".to_string(),
                some_enum: Some(SomeEnum::A),
            },
        }],
    };

    let ron = ron::ser::to_string(&scene).unwrap();
    let de: SomeCollection = ron::de::from_str(&ron).unwrap();
    assert_eq!(de, scene);

    let ron = ron::ser::to_string_pretty(&scene, Default::default()).unwrap();
    let _deser_scene: SomeCollection = ron::de::from_str(&ron).unwrap();
    assert_eq!(de, scene);
}