cmake_parser/doc/command/project/
load_cache.rs

1use cmake_parser_derive::CMake;
2
3use crate::{
4    doc::command_scope::{CommandScope, ToCommandScope},
5    Token,
6};
7
8/// Load in the values from another project's CMake cache.
9///
10/// Reference: <https://cmake.org/cmake/help/v3.26/command/load_cache.html>
11#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
12#[cmake(pkg = "crate", untagged)]
13pub enum LoadCache<'t> {
14    Local(LocalLoadCache<'t>),
15    External(ExternalLoadCache<'t>),
16}
17
18impl<'t> ToCommandScope for LoadCache<'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", positional)]
26pub struct LocalLoadCache<'t> {
27    pub build_dir: Token<'t>,
28    #[cmake(rename = "READ_WITH_PREFIX", transparent)]
29    pub prefix: Token<'t>,
30    pub entries: Vec<Token<'t>>,
31}
32
33#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
34#[cmake(pkg = "crate")]
35pub struct ExternalLoadCache<'t> {
36    #[cmake(positional)]
37    pub build_dir: Token<'t>,
38    pub exclude: Option<Vec<Token<'t>>>,
39    pub include_internals: Option<Vec<Token<'t>>>,
40}
41
42#[cfg(test)]
43mod tests {
44    use super::*;
45    use crate::doc::cmake_parse::tests::{token, tokens_vec};
46    use crate::*;
47    use pretty_assertions::assert_eq;
48
49    #[test]
50    fn load_cache() {
51        let src = include_bytes!("../../../../../fixture/commands/project/load_cache");
52        let cmakelists = parse_cmakelists(src).unwrap();
53        let doc = Doc::from(cmakelists);
54        assert_eq!(
55            doc.commands(),
56            Ok(vec![
57                Command::LoadCache(Box::new(LoadCache::Local(LocalLoadCache {
58                    build_dir: token(b"qqq"),
59                    prefix: token(b"prefix"),
60                    entries: tokens_vec([b"entry1", b"entry2"]),
61                }))),
62                Command::LoadCache(Box::new(LoadCache::External(ExternalLoadCache {
63                    build_dir: token(b"qqq"),
64                    exclude: None,
65                    include_internals: None,
66                }))),
67                Command::LoadCache(Box::new(LoadCache::External(ExternalLoadCache {
68                    build_dir: token(b"qqq"),
69                    exclude: Some(tokens_vec([b"abc", b"def"])),
70                    include_internals: None,
71                }))),
72                Command::LoadCache(Box::new(LoadCache::External(ExternalLoadCache {
73                    build_dir: token(b"qqq"),
74                    exclude: None,
75                    include_internals: Some(tokens_vec([b"abc", b"def", b"ghk"])),
76                }))),
77                Command::LoadCache(Box::new(LoadCache::External(ExternalLoadCache {
78                    build_dir: token(b"qqq"),
79                    exclude: Some(tokens_vec([b"abc", b"def"])),
80                    include_internals: Some(tokens_vec([b"abc", b"def", b"ghk"])),
81                }))),
82            ])
83        )
84    }
85}