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
use cmake_parser_derive::CMake;

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

/// Set the name of the project.
///
/// Reference: <https://cmake.org/cmake/help/v3.26/command/project.html>
#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct Project<'t> {
    pub project_name: Token<'t>,
    pub details: Option<ProjectDetails<'t>>,
}

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

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", untagged)]
pub enum ProjectDetails<'t> {
    General(GeneralProjectDetails<'t>),
    Short(Vec<Token<'t>>),
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub struct GeneralProjectDetails<'t> {
    pub version: Option<Token<'t>>,
    pub description: Option<Token<'t>>,
    pub homepage_url: Option<Token<'t>>,
    pub languages: Option<Vec<Token<'t>>>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::doc::cmake_parse::tests::{token, tokens_vec};
    use crate::*;
    use pretty_assertions::assert_eq;

    #[test]
    fn project() {
        let src = include_bytes!("../../../../../fixture/commands/project/project");
        let cmakelists = parse_cmakelists(src).unwrap();
        let doc = Doc::from(cmakelists);
        assert_eq!(
            doc.commands(),
            Ok(vec![
                Command::Project(Box::new(Project {
                    project_name: token(b"qqq"),
                    details: None,
                })),
                Command::Project(Box::new(Project {
                    project_name: token(b"aaa"),
                    details: Some(ProjectDetails::Short(tokens_vec([b"C", b"Rust"]))),
                })),
                Command::Project(Box::new(Project {
                    project_name: token(b"bbb"),
                    details: Some(ProjectDetails::General(GeneralProjectDetails {
                        version: Some(token(b"1.0.0")),
                        description: Some(token(b"Project bbb")),
                        homepage_url: Some(token(b"https://qqq.qqq")),
                        languages: Some(tokens_vec([b"C", b"Rust"])),
                    })),
                })),
            ])
        )
    }
}