cmake_parser/doc/command/scripting/
math.rs

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