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
use crate::objects::{MajorMinor, Object, ObjectKind};
use serde::{Deserialize, Serialize};

/// The cache object kind lists cache entries.
/// These are the Variables stored in the persistent cache (CMakeCache.txt) for the build tree.
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Cache {
    /// Kind of the cache object
    pub kind: ObjectKind,

    /// Version of the cache object
    pub version: MajorMinor,

    /// Entries in the cache
    pub entries: Vec<Entry>,
}

/// Entry in the cache
#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Entry {
    /// Name of the entry
    pub name: String,

    /// Value of the entry
    pub value: String,

    /// Type of the entry
    #[serde(rename = "type")]
    pub type_name: String,

    /// Properties of the entry
    pub properties: Vec<Property>,
}

#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct Property {
    /// Name of the property
    pub name: String,

    /// Value of the property
    pub value: String,
}

impl Object for Cache {
    fn kind() -> ObjectKind {
        ObjectKind::Cache
    }

    fn major() -> u32 {
        2
    }
}

#[cfg(test)]
mod tests {
    use crate::objects::cache_v2::*;
    use serde_json::json;

    #[test]
    fn test_configure_log() {
        let json = json!({
          "kind": "cache",
          "version": { "major": 2, "minor": 0 },
          "entries": [
            {
              "name": "BUILD_SHARED_LIBS",
              "value": "ON",
              "type": "BOOL",
              "properties": [
                {
                  "name": "HELPSTRING",
                  "value": "Build shared libraries"
                }
              ]
            },
            {
              "name": "CMAKE_GENERATOR",
              "value": "Unix Makefiles",
              "type": "INTERNAL",
              "properties": [
                {
                  "name": "HELPSTRING",
                  "value": "Name of generator."
                }
              ]
            }
          ]
        });

        let cache = serde_json::from_value::<Cache>(json).unwrap();
        assert_eq!(
            cache,
            Cache {
                kind: ObjectKind::Cache,
                version: MajorMinor { major: 2, minor: 0 },
                entries: vec![
                    Entry {
                        name: "BUILD_SHARED_LIBS".into(),
                        value: "ON".into(),
                        type_name: "BOOL".into(),
                        properties: vec![Property {
                            name: "HELPSTRING".into(),
                            value: "Build shared libraries".into(),
                        }]
                    },
                    Entry {
                        name: "CMAKE_GENERATOR".into(),
                        value: "Unix Makefiles".into(),
                        type_name: "INTERNAL".into(),
                        properties: vec![Property {
                            name: "HELPSTRING".into(),
                            value: "Name of generator.".into(),
                        }]
                    }
                ]
            }
        );
    }
}