cmake_file_api/objects/
cache_v2.rs

1use crate::objects::{MajorMinor, Object, ObjectKind};
2use serde::{Deserialize, Serialize};
3
4/// The cache object kind lists cache entries.
5/// These are the Variables stored in the persistent cache (CMakeCache.txt) for the build tree.
6#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
7#[serde(rename_all = "camelCase")]
8#[non_exhaustive]
9pub struct Cache {
10    /// Kind of the cache object
11    pub kind: ObjectKind,
12
13    /// Version of the cache object
14    pub version: MajorMinor,
15
16    /// Entries in the cache
17    pub entries: Vec<Entry>,
18}
19
20/// Entry in the cache
21#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
22#[serde(rename_all = "camelCase")]
23#[non_exhaustive]
24pub struct Entry {
25    /// Name of the entry
26    pub name: String,
27
28    /// Value of the entry
29    pub value: String,
30
31    /// Type of the entry
32    #[serde(rename = "type")]
33    pub type_name: String,
34
35    /// Properties of the entry
36    pub properties: Vec<Property>,
37}
38
39#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)]
40#[serde(rename_all = "camelCase")]
41#[non_exhaustive]
42pub struct Property {
43    /// Name of the property
44    pub name: String,
45
46    /// Value of the property
47    pub value: String,
48}
49
50impl Object for Cache {
51    fn kind() -> ObjectKind {
52        ObjectKind::Cache
53    }
54
55    fn major() -> u32 {
56        2
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use crate::objects::cache_v2::*;
63    use serde_json::json;
64
65    #[test]
66    fn test_configure_log() {
67        let json = json!({
68          "kind": "cache",
69          "version": { "major": 2, "minor": 0 },
70          "entries": [
71            {
72              "name": "BUILD_SHARED_LIBS",
73              "value": "ON",
74              "type": "BOOL",
75              "properties": [
76                {
77                  "name": "HELPSTRING",
78                  "value": "Build shared libraries"
79                }
80              ]
81            },
82            {
83              "name": "CMAKE_GENERATOR",
84              "value": "Unix Makefiles",
85              "type": "INTERNAL",
86              "properties": [
87                {
88                  "name": "HELPSTRING",
89                  "value": "Name of generator."
90                }
91              ]
92            }
93          ]
94        });
95
96        let cache = serde_json::from_value::<Cache>(json).unwrap();
97        assert_eq!(
98            cache,
99            Cache {
100                kind: ObjectKind::Cache,
101                version: MajorMinor { major: 2, minor: 0 },
102                entries: vec![
103                    Entry {
104                        name: "BUILD_SHARED_LIBS".into(),
105                        value: "ON".into(),
106                        type_name: "BOOL".into(),
107                        properties: vec![Property {
108                            name: "HELPSTRING".into(),
109                            value: "Build shared libraries".into(),
110                        }]
111                    },
112                    Entry {
113                        name: "CMAKE_GENERATOR".into(),
114                        value: "Unix Makefiles".into(),
115                        type_name: "INTERNAL".into(),
116                        properties: vec![Property {
117                            name: "HELPSTRING".into(),
118                            value: "Name of generator.".into(),
119                        }]
120                    }
121                ]
122            }
123        );
124    }
125}