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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
use cmake_parser_derive::CMake;

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

/// Call meta-operations on CMake commands.
///
/// Reference: <https://cmake.org/cmake/help/v3.26/command/cmake_language.html>
#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", transparent)]
pub enum CMakeLanguage<'t> {
    Call(CMakeLanguageCall<'t>),
    Eval(CMakeLanguageEval<'t>),
    Defer(CMakeLanguageDefer<'t>),
    SetDependencyProvider(CMakeLanguageSetDependencyProvider<'t>),
    GetMessageLogLevel(CMakeLanguageGetMessageLogLevel<'t>),
}

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

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct CMakeLanguageCall<'t> {
    pub command: Token<'t>,
    pub args: Option<Vec<Token<'t>>>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate")]
pub struct CMakeLanguageEval<'t> {
    pub code: Vec<Token<'t>>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", untagged)]
pub enum CMakeLanguageDefer<'t> {
    Call(CMakeLanguageDeferCall<'t>),
    GetCallIds(CMakeLanguageDeferGetCallIds<'t>),
    GetCall(CMakeLanguageDeferGetCall<'t>),
    CancelCall(CMakeLanguageDeferCancelCall<'t>),
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", default = "options")]
pub struct CMakeLanguageDeferCall<'t> {
    #[cmake(rename = "")]
    pub options: Option<Vec<DeferCallOption<'t>>>,
    pub call: CMakeLanguageCall<'t>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", list, transparent)]
pub enum DeferCallOption<'t> {
    Directory(Token<'t>),
    Id(Token<'t>),
    IdVar(Token<'t>),
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional, transparent)]
pub struct CMakeLanguageDeferGetCallIds<'t> {
    pub directory: Option<Token<'t>>,
    #[cmake(rename = "GET_CALL_IDS")]
    pub var: Token<'t>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct CMakeLanguageDeferGetCall<'t> {
    #[cmake(transparent)]
    pub directory: Option<Token<'t>>,
    #[cmake(transparent, rename = "GET_CALL")]
    pub id: Token<'t>,
    pub var: Token<'t>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional, transparent)]
pub struct CMakeLanguageDeferCancelCall<'t> {
    pub directory: Option<Token<'t>>,
    #[cmake(rename = "CANCEL_CALL")]
    pub ids: Vec<Token<'t>>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct CMakeLanguageSetDependencyProvider<'t> {
    pub command: Token<'t>,
    #[cmake(transparent)]
    pub supported_methods: Vec<SuportedMethod>,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", list)]
pub enum SuportedMethod {
    FindPackage,
    #[cmake(rename = "FETCHCONTENT_MAKEAVAILABLE_SERIAL")]
    FetchContentMakeAvailableSerial,
}

#[derive(CMake, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cmake(pkg = "crate", positional)]
pub struct CMakeLanguageGetMessageLogLevel<'t> {
    pub out_var: Token<'t>,
}

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

    #[test]
    fn cmake_language() {
        let src = include_bytes!("../../../../../fixture/commands/scripting/cmake_language");
        let cmakelists = parse_cmakelists(src).unwrap();
        let doc = Doc::from(cmakelists);
        assert_eq!(
            doc.commands(),
            Ok(vec![
                Command::CMakeLanguage(Box::new(CMakeLanguage::Call(CMakeLanguageCall {
                    command: token(b"${message_command}"),
                    args: Some(vec![token(b"STATUS"), quoted_token(b"Hello World!")]),
                }))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Eval(CMakeLanguageEval {
                    code: quoted_tokens_vec([br###"
  if (${condition})
    message(STATUS TRUE)
  else()
    message(STATUS FALSE)
  endif()"###]),
                }))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(CMakeLanguageDefer::Call(
                    CMakeLanguageDeferCall {
                        options: None,
                        call: CMakeLanguageCall {
                            command: token(b"message"),
                            args: Some(vec![quoted_token(b"${deferred_message}")]),
                        }
                    }
                )))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(CMakeLanguageDefer::Call(
                    CMakeLanguageDeferCall {
                        options: Some(vec![DeferCallOption::IdVar(token(b"id"))]),
                        call: CMakeLanguageCall {
                            command: token(b"message"),
                            args: Some(vec![quoted_token(b"Canceled Message")]),
                        }
                    }
                )))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(
                    CMakeLanguageDefer::GetCallIds(CMakeLanguageDeferGetCallIds {
                        directory: None,
                        var: token(b"ids1"),
                    })
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(
                    CMakeLanguageDefer::GetCallIds(CMakeLanguageDeferGetCallIds {
                        directory: Some(token(b"dir1")),
                        var: token(b"ids1"),
                    })
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(
                    CMakeLanguageDefer::GetCall(CMakeLanguageDeferGetCall {
                        directory: None,
                        id: token(b"id1"),
                        var: token(b"var1"),
                    })
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(
                    CMakeLanguageDefer::GetCall(CMakeLanguageDeferGetCall {
                        directory: Some(token(b"dir1")),
                        id: token(b"id1"),
                        var: token(b"var1"),
                    })
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(
                    CMakeLanguageDefer::CancelCall(CMakeLanguageDeferCancelCall {
                        directory: None,
                        ids: tokens_vec([b"${id}"]),
                    })
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::Defer(
                    CMakeLanguageDefer::CancelCall(CMakeLanguageDeferCancelCall {
                        directory: Some(token(b"dir1")),
                        ids: tokens_vec([b"id1", b"id2"]),
                    })
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::SetDependencyProvider(
                    CMakeLanguageSetDependencyProvider {
                        command: token(b"cmd1"),
                        supported_methods: vec![
                            SuportedMethod::FindPackage,
                            SuportedMethod::FetchContentMakeAvailableSerial
                        ],
                    }
                ))),
                Command::CMakeLanguage(Box::new(CMakeLanguage::GetMessageLogLevel(
                    CMakeLanguageGetMessageLogLevel {
                        out_var: token(b"out_var1"),
                    }
                ))),
            ])
        )
    }
}