cmake_parser/doc/command/scripting/
find_file.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    command::common::{FindPath, FindRoot, Names, WindowsRegistryView},
5    doc::command_scope::{CommandScope, ToCommandScope},
6    Token,
7};
8
9/// This command is used to find a full path to named file.
10///
11/// Reference: <https://cmake.org/cmake/help/v3.26/command/find_file.html>
12#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
13#[cmake(pkg = "crate", untagged)]
14pub enum FindFile<'t> {
15    General(FindFileGeneral<'t>),
16    Short(FindFileShort<'t>),
17}
18
19impl<'t> ToCommandScope for FindFile<'t> {
20    fn to_command_scope(&self) -> CommandScope {
21        CommandScope::Scripting
22    }
23}
24
25#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
26#[cmake(pkg = "crate", default = "names")]
27pub struct FindFileGeneral<'t> {
28    #[cmake(positional)]
29    pub variable: Token<'t>,
30    #[cmake(rename = "")]
31    pub names: Names<'t>,
32    pub hints: Option<Vec<FindPath<'t>>>,
33    pub paths: Option<Vec<FindPath<'t>>>,
34    pub registry_view: Option<WindowsRegistryView>,
35    pub path_suffixes: Option<Vec<Token<'t>>>,
36    pub validator: Option<Token<'t>>,
37    pub doc: Option<Token<'t>>,
38    pub no_cache: bool,
39    pub required: bool,
40    pub no_default_path: bool,
41    pub no_package_root_path: bool,
42    pub no_cmake_path: bool,
43    pub no_cmake_environment_path: bool,
44    pub no_system_environment_path: bool,
45    pub no_cmake_system_path: bool,
46    pub no_cmake_install_prefix: bool,
47    pub find_root: Option<FindRoot>,
48}
49
50#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
51#[cmake(pkg = "crate", positional)]
52pub struct FindFileShort<'t> {
53    pub variable: Token<'t>,
54    pub name: Token<'t>,
55    pub paths: Vec<Token<'t>>,
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61    use crate::doc::cmake_parse::tests::{token, tokens_vec};
62    use crate::*;
63    use pretty_assertions::assert_eq;
64
65    #[test]
66    fn find_file() {
67        let src = include_bytes!("../../../../../fixture/commands/scripting/find_file");
68        let cmakelists = parse_cmakelists(src).unwrap();
69        let doc = Doc::from(cmakelists);
70        assert_eq!(
71            doc.commands(),
72            Ok(vec![
73                Command::FindFile(Box::new(FindFile::General(FindFileGeneral {
74                    variable: token(b"variable1"),
75                    names: Names::Single(token(b"name1")),
76                    hints: None,
77                    paths: None,
78                    registry_view: None,
79                    path_suffixes: None,
80                    validator: None,
81                    doc: None,
82                    no_cache: false,
83                    required: false,
84                    no_default_path: false,
85                    no_package_root_path: false,
86                    no_cmake_path: false,
87                    no_cmake_environment_path: false,
88                    no_system_environment_path: false,
89                    no_cmake_system_path: false,
90                    no_cmake_install_prefix: false,
91                    find_root: None,
92                }))),
93                Command::FindFile(Box::new(FindFile::Short(FindFileShort {
94                    variable: token(b"variable1"),
95                    name: token(b"name1"),
96                    paths: tokens_vec([b"path1"]),
97                }))),
98                Command::FindFile(Box::new(FindFile::General(FindFileGeneral {
99                    variable: token(b"variable1"),
100                    names: Names::Multi(tokens_vec([b"name1", b"name2"])),
101                    hints: None,
102                    paths: None,
103                    registry_view: None,
104                    path_suffixes: None,
105                    validator: None,
106                    doc: None,
107                    no_cache: false,
108                    required: false,
109                    no_default_path: false,
110                    no_package_root_path: false,
111                    no_cmake_path: false,
112                    no_cmake_environment_path: false,
113                    no_system_environment_path: false,
114                    no_cmake_system_path: false,
115                    no_cmake_install_prefix: false,
116                    find_root: None,
117                }))),
118                Command::FindFile(Box::new(FindFile::General(FindFileGeneral {
119                    variable: token(b"variable1"),
120                    names: Names::Multi(tokens_vec([b"name1", b"name2"])),
121                    hints: Some(vec![
122                        FindPath::Path(token(b"path1")),
123                        FindPath::Path(token(b"path2")),
124                        FindPath::Env(token(b"env1")),
125                    ]),
126                    paths: Some(vec![
127                        FindPath::Env(token(b"env1")),
128                        FindPath::Env(token(b"env2")),
129                        FindPath::Path(token(b"path1")),
130                    ]),
131                    registry_view: Some(WindowsRegistryView::Target),
132                    path_suffixes: Some(tokens_vec([b"suffix1", b"suffix2"])),
133                    validator: Some(token(b"validator1")),
134                    doc: Some(token(b"doc1")),
135                    no_cache: true,
136                    required: true,
137                    no_default_path: true,
138                    no_package_root_path: true,
139                    no_cmake_path: true,
140                    no_cmake_environment_path: true,
141                    no_system_environment_path: true,
142                    no_cmake_system_path: true,
143                    no_cmake_install_prefix: true,
144                    find_root: Some(FindRoot::CMakeFindRootPathBoth),
145                }))),
146            ])
147        )
148    }
149}