cmake_parser/doc/command/scripting/
unset.rs

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