cmake_parser/doc/command/scripting/
set.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Set a normal, cache, or environment variable to a given value.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/set.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", untagged)]
13pub enum Set<'t> {
14    Cache(SetCache<'t>),
15    Normal(SetNormal<'t>),
16}
17
18impl<'t> ToCommandScope for Set<'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 struct SetNormal<'t> {
27    pub variable: Token<'t>,
28    #[cmake(in_range, allow_empty)]
29    pub value: Vec<Token<'t>>,
30    pub parent_scope: bool,
31}
32
33#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
34#[cmake(pkg = "crate", positional)]
35pub struct SetCache<'t> {
36    pub variable: Token<'t>,
37    #[cmake(in_range)]
38    pub value: Vec<Token<'t>>,
39    #[cmake(transparent)]
40    pub cache: Cache,
41    pub docstring: Token<'t>,
42    pub force: bool,
43}
44
45#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
46#[cmake(pkg = "crate")]
47pub enum Cache {
48    Bool,
49    #[cmake(rename = "FILEPATH")]
50    FilePath,
51    Path,
52    String,
53    Internal,
54}
55
56#[cfg(test)]
57mod tests {
58    use super::*;
59    use crate::doc::cmake_parse::tests::{quoted_token, token, tokens_vec};
60    use crate::*;
61    use pretty_assertions::assert_eq;
62
63    #[test]
64    fn set() {
65        let src = include_bytes!("../../../../../fixture/commands/scripting/set");
66        let cmakelists = parse_cmakelists(src).unwrap();
67        let doc = Doc::from(cmakelists);
68        assert_eq!(
69            doc.to_commands_iter().collect::<Vec<_>>(),
70            vec![
71                Ok(Command::Set(Box::new(Set::Normal(SetNormal {
72                    variable: token(b"var1"),
73                    value: tokens_vec([b"value1", b"value2"]),
74                    parent_scope: false,
75                })))),
76                Ok(Command::Set(Box::new(Set::Normal(SetNormal {
77                    variable: token(b"var1"),
78                    value: tokens_vec([b"value1", b"value2"]),
79                    parent_scope: true,
80                })))),
81                Ok(Command::Set(Box::new(Set::Cache(SetCache {
82                    variable: token(b"var1"),
83                    value: tokens_vec([b"value1", b"value2"]),
84                    cache: Cache::Bool,
85                    docstring: quoted_token(b"docstring1"),
86                    force: false,
87                })))),
88                Ok(Command::Set(Box::new(Set::Cache(SetCache {
89                    variable: token(b"var1"),
90                    value: tokens_vec([b"value1", b"value2"]),
91                    cache: Cache::FilePath,
92                    docstring: quoted_token(b"docstring1"),
93                    force: true,
94                })))),
95            ]
96        )
97    }
98}