cmake_parser/doc/command/project/
project.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Set the name of the project.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/project.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", positional)]
13pub struct Project<'t> {
14    pub project_name: Token<'t>,
15    pub details: Option<ProjectDetails<'t>>,
16}
17
18impl<'t> ToCommandScope for Project<'t> {
19    fn to_command_scope(&self) -> CommandScope {
20        CommandScope::Project
21    }
22}
23
24#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
25#[cmake(pkg = "crate", untagged)]
26pub enum ProjectDetails<'t> {
27    General(GeneralProjectDetails<'t>),
28    Short(Vec<Token<'t>>),
29}
30
31#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
32#[cmake(pkg = "crate")]
33pub struct GeneralProjectDetails<'t> {
34    pub version: Option<Token<'t>>,
35    pub description: Option<Token<'t>>,
36    pub homepage_url: Option<Token<'t>>,
37    pub languages: Option<Vec<Token<'t>>>,
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use crate::doc::cmake_parse::tests::{token, tokens_vec};
44    use crate::*;
45    use pretty_assertions::assert_eq;
46
47    #[test]
48    fn project() {
49        let src = include_bytes!("../../../../../fixture/commands/project/project");
50        let cmakelists = parse_cmakelists(src).unwrap();
51        let doc = Doc::from(cmakelists);
52        assert_eq!(
53            doc.commands(),
54            Ok(vec![
55                Command::Project(Box::new(Project {
56                    project_name: token(b"qqq"),
57                    details: None,
58                })),
59                Command::Project(Box::new(Project {
60                    project_name: token(b"aaa"),
61                    details: Some(ProjectDetails::Short(tokens_vec([b"C", b"Rust"]))),
62                })),
63                Command::Project(Box::new(Project {
64                    project_name: token(b"bbb"),
65                    details: Some(ProjectDetails::General(GeneralProjectDetails {
66                        version: Some(token(b"1.0.0")),
67                        description: Some(token(b"Project bbb")),
68                        homepage_url: Some(token(b"https://qqq.qqq")),
69                        languages: Some(tokens_vec([b"C", b"Rust"])),
70                    })),
71                })),
72            ])
73        )
74    }
75}