cmake_parser/doc/command/scripting/
get_filename_component.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Get a specific component of a full filename.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/get_filename_component.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate")]
13pub struct GetFilenameComponent<'t> {
14    #[cmake(positional)]
15    pub variable: Token<'t>,
16    #[cmake(positional)]
17    pub filename: Token<'t>,
18    pub mode: Mode<'t>,
19    pub cache: bool,
20}
21
22impl<'t> ToCommandScope for GetFilenameComponent<'t> {
23    fn to_command_scope(&self) -> CommandScope {
24        CommandScope::Scripting
25    }
26}
27
28#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
29#[cmake(pkg = "crate", transparent)]
30pub enum Mode<'t> {
31    /// Directory without file name
32    Directory,
33    /// File name without directory
34    Name,
35    /// File name longest extension (.b.c from d/a.b.c)
36    Ext,
37    /// File name with neither the directory nor the longest extension
38    NameWe,
39    /// File name last extension (.c from d/a.b.c)
40    LastExt,
41    /// File name with neither the directory nor the last extension
42    NameWle,
43    /// Legacy alias for DIRECTORY (use for CMake <= 2.8.11)
44    Path,
45    /// Full path to file
46    Absolute(BaseDir<'t>),
47    /// Full path to existing file with symlinks resolved
48    #[cmake(rename = "REALPATH")]
49    RealPath(BaseDir<'t>),
50    Program(Program<'t>),
51}
52
53#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
54#[cmake(pkg = "crate", allow_empty)]
55pub struct BaseDir<'t> {
56    pub base_dir: Option<Token<'t>>,
57}
58
59#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
60#[cmake(pkg = "crate", allow_empty)]
61pub struct Program<'t> {
62    pub program_args: Option<Token<'t>>,
63}
64
65#[cfg(test)]
66mod tests {
67    use super::*;
68    use crate::doc::cmake_parse::tests::token;
69    use crate::*;
70    use pretty_assertions::assert_eq;
71
72    #[test]
73    fn get_filename_component() {
74        let src =
75            include_bytes!("../../../../../fixture/commands/scripting/get_filename_component");
76        let cmakelists = parse_cmakelists(src).unwrap();
77        let doc = Doc::from(cmakelists);
78        assert_eq!(
79            doc.to_commands_iter().collect::<Vec<_>>(),
80            vec![
81                Ok(Command::GetFilenameComponent(Box::new(
82                    GetFilenameComponent {
83                        variable: token(b"var1"),
84                        filename: token(b"filename1"),
85                        mode: Mode::Directory,
86                        cache: false,
87                    }
88                ))),
89                Ok(Command::GetFilenameComponent(Box::new(
90                    GetFilenameComponent {
91                        variable: token(b"var1"),
92                        filename: token(b"filename1"),
93                        mode: Mode::Name,
94                        cache: true,
95                    }
96                ))),
97                Ok(Command::GetFilenameComponent(Box::new(
98                    GetFilenameComponent {
99                        variable: token(b"var1"),
100                        filename: token(b"filename1"),
101                        mode: Mode::NameWe,
102                        cache: false,
103                    }
104                ))),
105                Ok(Command::GetFilenameComponent(Box::new(
106                    GetFilenameComponent {
107                        variable: token(b"var1"),
108                        filename: token(b"filename1"),
109                        mode: Mode::Absolute(BaseDir { base_dir: None }),
110                        cache: false,
111                    }
112                ))),
113                Ok(Command::GetFilenameComponent(Box::new(
114                    GetFilenameComponent {
115                        variable: token(b"var1"),
116                        filename: token(b"filename1"),
117                        mode: Mode::RealPath(BaseDir {
118                            base_dir: Some(token(b"base_dir1"))
119                        }),
120                        cache: false,
121                    }
122                ))),
123                Ok(Command::GetFilenameComponent(Box::new(
124                    GetFilenameComponent {
125                        variable: token(b"var1"),
126                        filename: token(b"filename1"),
127                        mode: Mode::Absolute(BaseDir {
128                            base_dir: Some(token(b"base_dir1"))
129                        }),
130                        cache: true,
131                    }
132                ))),
133                Ok(Command::GetFilenameComponent(Box::new(
134                    GetFilenameComponent {
135                        variable: token(b"var1"),
136                        filename: token(b"filename1"),
137                        mode: Mode::RealPath(BaseDir { base_dir: None }),
138                        cache: true,
139                    }
140                ))),
141                Ok(Command::GetFilenameComponent(Box::new(
142                    GetFilenameComponent {
143                        variable: token(b"var1"),
144                        filename: token(b"filename1"),
145                        mode: Mode::Program(Program { program_args: None }),
146                        cache: false,
147                    }
148                ))),
149                Ok(Command::GetFilenameComponent(Box::new(
150                    GetFilenameComponent {
151                        variable: token(b"var1"),
152                        filename: token(b"filename1"),
153                        mode: Mode::Program(Program {
154                            program_args: Some(token(b"program_args1"))
155                        }),
156                        cache: true,
157                    }
158                ))),
159            ]
160        )
161    }
162}