cmake_parser/doc/command/scripting/
variable_watch.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Watch the CMake variable for change.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/variable_watch.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", positional)]
13pub struct VariableWatch<'t> {
14    pub variable: Token<'t>,
15    pub command: Option<Token<'t>>,
16}
17
18impl<'t> ToCommandScope for VariableWatch<'t> {
19    fn to_command_scope(&self) -> CommandScope {
20        CommandScope::Scripting
21    }
22}
23
24#[cfg(test)]
25mod tests {
26    use super::*;
27    use crate::doc::cmake_parse::tests::token;
28    use crate::*;
29    use pretty_assertions::assert_eq;
30
31    #[test]
32    fn variable_watch() {
33        let src = include_bytes!("../../../../../fixture/commands/scripting/variable_watch");
34        let cmakelists = parse_cmakelists(src).unwrap();
35        let doc = Doc::from(cmakelists);
36        assert_eq!(
37            doc.to_commands_iter().collect::<Vec<_>>(),
38            vec![
39                Ok(Command::VariableWatch(Box::new(VariableWatch {
40                    variable: token(b"var1"),
41                    command: None,
42                }))),
43                Ok(Command::VariableWatch(Box::new(VariableWatch {
44                    variable: token(b"var1"),
45                    command: Some(token(b"cmd1")),
46                }))),
47            ]
48        )
49    }
50}