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
use cmake_parser_derive::CMake;

use crate::{
    command::common::WindowsRegistryView,
    doc::command_scope::{CommandScope, ToCommandScope},
    Token,
};

/// Query various host system information.
///
/// Reference: <https://cmake.org/cmake/help/v3.26/command/cmake_host_system_information.html>
#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub struct CMakeHostSystemInformation<'t> {
    pub result: Token<'t>,
    pub query: Query<'t>,
}

impl<'t> ToCommandScope for CMakeHostSystemInformation<'t> {
    fn to_command_scope(&self) -> CommandScope {
        CommandScope::Scripting
    }
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", untagged)]
pub enum Query<'t> {
    #[cmake(transparent)]
    WindowsRegistry(WindowsRegistryQuery<'t>),
    Regular(Vec<Token<'t>>),
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub struct WindowsRegistryQuery<'t> {
    #[cmake(positional)]
    pub key: Token<'t>,
    pub selector: Option<WindowsRegistrySelector<'t>>,
    pub view: Option<WindowsRegistryView>,
    pub separator: Option<Token<'t>>,
    pub error_variable: Option<Token<'t>>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub enum WindowsRegistrySelector<'t> {
    ValueNames,
    Subkeys,
    #[cmake(transparent)]
    Value(Token<'t>),
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doc::cmake_parse::tests::{quoted_token, token, tokens_vec};
    use crate::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn cmake_host_system_information() {
        let src = include_bytes!(
            "../../../../../fixture/commands/scripting/cmake_host_system_information"
        );
        let cmakelists = parse_cmakelists(src).unwrap();
        let doc = Doc::from(cmakelists);
        assert_eq!(
            doc.commands(),
            Ok(vec![
                Command::CMakeHostSystemInformation(Box::new(CMakeHostSystemInformation {
                    result: token(b"Ncpu"),
                    query: Query::Regular(tokens_vec([b"NUMBER_OF_PHYSICAL_CORES"])),
                })),
                Command::CMakeHostSystemInformation(Box::new(CMakeHostSystemInformation {
                    result: token(b"result"),
                    query: Query::WindowsRegistry(WindowsRegistryQuery {
                        key: quoted_token(b"HKLM/SOFTWARE/Kitware"),
                        selector: None,
                        view: None,
                        separator: None,
                        error_variable: None,
                    }),
                })),
                Command::CMakeHostSystemInformation(Box::new(CMakeHostSystemInformation {
                    result: token(b"result"),
                    query: Query::WindowsRegistry(WindowsRegistryQuery {
                        key: quoted_token(b"HKLM/SOFTWARE/Kitware"),
                        selector: Some(WindowsRegistrySelector::Value(quoted_token(b"(default)"))),
                        view: None,
                        separator: None,
                        error_variable: None,
                    }),
                })),
                Command::CMakeHostSystemInformation(Box::new(CMakeHostSystemInformation {
                    result: token(b"result"),
                    query: Query::WindowsRegistry(WindowsRegistryQuery {
                        key: quoted_token(b"HKLM/SOFTWARE/Kitware"),
                        selector: Some(WindowsRegistrySelector::Subkeys),
                        view: Some(WindowsRegistryView::Bits32Fallback64),
                        separator: Some(token(b"separator1")),
                        error_variable: Some(token(b"error_variable1")),
                    }),
                })),
                Command::CMakeHostSystemInformation(Box::new(CMakeHostSystemInformation {
                    result: token(b"_vs_dir"),
                    query: Query::Regular(tokens_vec([b"VS_${_vs_ver}_DIR"])),
                })),
            ])
        )
    }
}