1
use ron::ser::{path_meta::Field, PrettyConfig};
2

            
3
#[test]
4
4
fn serialize_field() {
5
    #[derive(serde::Serialize)]
6
    enum PetKind {
7
        Isopod,
8
    }
9

            
10
    #[derive(serde::Serialize)]
11
    struct Pet {
12
        name: &'static str,
13
        age: u8,
14
        kind: PetKind,
15
    }
16

            
17
    #[derive(serde::Serialize)]
18
    struct Person {
19
        name: &'static str,
20
        age: u8,
21
        knows: Vec<usize>,
22
        pet: Option<Pet>,
23
    }
24

            
25
4
    let value = (
26
4
        Person {
27
4
            name: "Walter",
28
4
            age: 43,
29
4
            knows: vec![0, 1],
30
4
            pet: None,
31
4
        },
32
4
        vec![
33
4
            Person {
34
4
                name: "Alice",
35
4
                age: 29,
36
4
                knows: vec![1],
37
4
                pet: Some(Pet {
38
4
                    name: "Herbert",
39
4
                    age: 7,
40
4
                    kind: PetKind::Isopod,
41
4
                }),
42
4
            },
43
4
            Person {
44
4
                name: "Bob",
45
4
                age: 29,
46
4
                knows: vec![0],
47
4
                pet: None,
48
4
            },
49
4
        ],
50
4
    );
51
4

            
52
4
    let mut config = PrettyConfig::default();
53
4

            
54
4
    config
55
4
        .path_meta
56
4
        .get_or_insert_with(Field::empty)
57
4
        .build_fields(|fields| {
58
4
            fields
59
4
                .field("age")
60
4
                .with_doc("0@age (person)\nmust be within range 0..256");
61
4
            fields
62
4
                .field("knows")
63
4
                .with_doc("0@knows (person)\nmust be list of valid person indices");
64
4
            fields.field("pet").build_fields(|fields| {
65
4
                fields
66
4
                    .field("age")
67
4
                    .with_doc("1@age (pet)\nmust be valid range 0..256");
68
4
                fields
69
4
                    .field("kind")
70
4
                    .with_doc("1@kind (pet)\nmust be `Isopod`");
71
4
            });
72
4

            
73
4
            // provide meta for a field that doesn't exist;
74
4
            // this should not end up anywhere in the final string
75
4
            fields.field("0").with_doc("unreachable");
76
4
        });
77
4

            
78
4
    let s = ron::ser::to_string_pretty(&value, config).unwrap();
79
4

            
80
4
    assert_eq!(
81
4
        s,
82
4
        r#"((
83
4
    name: "Walter",
84
4
    /// 0@age (person)
85
4
    /// must be within range 0..256
86
4
    age: 43,
87
4
    /// 0@knows (person)
88
4
    /// must be list of valid person indices
89
4
    knows: [
90
4
        0,
91
4
        1,
92
4
    ],
93
4
    pet: None,
94
4
), [
95
4
    (
96
4
        name: "Alice",
97
4
        /// 0@age (person)
98
4
        /// must be within range 0..256
99
4
        age: 29,
100
4
        /// 0@knows (person)
101
4
        /// must be list of valid person indices
102
4
        knows: [
103
4
            1,
104
4
        ],
105
4
        pet: Some((
106
4
            name: "Herbert",
107
4
            /// 1@age (pet)
108
4
            /// must be valid range 0..256
109
4
            age: 7,
110
4
            /// 1@kind (pet)
111
4
            /// must be `Isopod`
112
4
            kind: Isopod,
113
4
        )),
114
4
    ),
115
4
    (
116
4
        name: "Bob",
117
4
        /// 0@age (person)
118
4
        /// must be within range 0..256
119
4
        age: 29,
120
4
        /// 0@knows (person)
121
4
        /// must be list of valid person indices
122
4
        knows: [
123
4
            0,
124
4
        ],
125
4
        pet: None,
126
4
    ),
127
4
])"#
128
4
    );
129
4
}