1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use ::cmake_parser_derive::CMake;

use crate::{CommandScope, ToCommandScope, Token};

/// Define and document custom properties.
///
/// Reference: <https://cmake.org/cmake/help/v3.26/command/define_property.html>
#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub struct DefineProperty<'t> {
    #[cmake(positional)]
    pub property_scope: PropertyScope,
    #[cmake(rename = "PROPERTY", positional, transparent)]
    pub property_name: Token<'t>,
    pub inherited: bool,
    pub brief_docs: Option<Vec<Token<'t>>>,
    pub full_docs: Option<Vec<Token<'t>>>,
    pub initialize_from_variable: Option<Token<'t>>,
}

impl<'t> ToCommandScope for DefineProperty<'t> {
    fn to_command_scope(&self) -> CommandScope {
        CommandScope::Project
    }
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub enum PropertyScope {
    Global,
    Directory,
    Target,
    Source,
    Test,
    Variable,
    CachedVariable,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doc::cmake_parse::tests::quoted_tokens;
    use crate::*;

    #[test]
    fn define_property() {
        let src = include_bytes!("../../../../../fixture/commands/project/define_property");
        let cmakelists = parse_cmakelists(src).unwrap();
        let doc = Doc::from(cmakelists);

        assert_eq!(
            doc.commands(),
            Ok(vec![
                Command::DefineProperty(Box::new(DefineProperty {
                    property_scope: PropertyScope::Target,
                    property_name: b"EXAMPLE_TYPE".into(),
                    inherited: false,
                    brief_docs: Some(
                        quoted_tokens([b"Whether given target describes example."]).to_vec()
                    ),
                    full_docs: Some(
                        quoted_tokens([b"Whether given target describes example."]).to_vec()
                    ),
                    initialize_from_variable: None,
                })),
                Command::DefineProperty(Box::new(DefineProperty {
                    property_scope: PropertyScope::CachedVariable,
                    property_name: b"A_PROPERTY".into(),
                    inherited: false,
                    brief_docs: Some(quoted_tokens([b"brief"]).to_vec()),
                    full_docs: Some(quoted_tokens([b"full"]).to_vec()),
                    initialize_from_variable: None,
                })),
            ])
        )
    }
}