cmake_parser/doc/command/scripting/
block.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Evaluate a group of commands with a dedicated variable and/or policy scope.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/block.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", allow_empty)]
13pub struct Block<'t> {
14    scope_for: Option<ScopeFor>,
15    propagate: Option<Vec<Token<'t>>>,
16}
17
18impl<'t> ToCommandScope for Block<'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")]
26pub struct ScopeFor {
27    policies: bool,
28    variables: bool,
29}
30
31#[cfg(test)]
32mod tests {
33    use super::*;
34    use crate::doc::cmake_parse::tests::tokens_vec;
35    use crate::*;
36    use pretty_assertions::assert_eq;
37
38    #[test]
39    fn block() {
40        let src = include_bytes!("../../../../../fixture/commands/scripting/block");
41        let cmakelists = parse_cmakelists(src).unwrap();
42        let doc = Doc::from(cmakelists);
43        assert_eq!(
44            doc.commands(),
45            Ok(vec![
46                Command::Block(Box::new(Block {
47                    scope_for: None,
48                    propagate: None,
49                })),
50                Command::Block(Box::new(Block {
51                    scope_for: Some(ScopeFor {
52                        policies: true,
53                        variables: false,
54                    }),
55                    propagate: None,
56                })),
57                Command::Block(Box::new(Block {
58                    scope_for: Some(ScopeFor {
59                        policies: false,
60                        variables: true,
61                    }),
62                    propagate: None,
63                })),
64                Command::Block(Box::new(Block {
65                    scope_for: None,
66                    propagate: Some(tokens_vec([b"var1", b"var2"])),
67                })),
68                Command::Block(Box::new(Block {
69                    scope_for: Some(ScopeFor {
70                        policies: true,
71                        variables: true,
72                    }),
73                    propagate: Some(tokens_vec([b"var1", b"var2"])),
74                })),
75            ])
76        )
77    }
78}