darklua_core/nodes/statements/
local_function.rs1use crate::nodes::{
2 Block, FunctionBodyTokens, FunctionReturnType, FunctionVariadicType, GenericParameters,
3 Identifier, Token, TypedIdentifier,
4};
5
6#[derive(Clone, Debug, PartialEq, Eq)]
8pub struct LocalFunctionTokens {
9 pub local: Token,
10 pub function_body: FunctionBodyTokens,
11}
12
13impl LocalFunctionTokens {
14 super::impl_token_fns!(target = [local, function_body]);
15}
16
17impl std::ops::Deref for LocalFunctionTokens {
18 type Target = FunctionBodyTokens;
19
20 fn deref(&self) -> &Self::Target {
21 &self.function_body
22 }
23}
24
25impl std::ops::DerefMut for LocalFunctionTokens {
26 fn deref_mut(&mut self) -> &mut Self::Target {
27 &mut self.function_body
28 }
29}
30
31#[derive(Clone, Debug, PartialEq, Eq)]
33pub struct LocalFunctionStatement {
34 identifier: Identifier,
35 block: Block,
36 parameters: Vec<TypedIdentifier>,
37 is_variadic: bool,
38 variadic_type: Option<FunctionVariadicType>,
39 return_type: Option<FunctionReturnType>,
40 generic_parameters: Option<GenericParameters>,
41 tokens: Option<Box<LocalFunctionTokens>>,
42}
43
44impl LocalFunctionStatement {
45 pub fn new(
47 identifier: impl Into<Identifier>,
48 block: Block,
49 parameters: Vec<TypedIdentifier>,
50 is_variadic: bool,
51 ) -> Self {
52 Self {
53 identifier: identifier.into(),
54 block,
55 parameters,
56 is_variadic,
57 variadic_type: None,
58 return_type: None,
59 generic_parameters: None,
60 tokens: None,
61 }
62 }
63
64 pub fn from_name(identifier: impl Into<Identifier>, block: impl Into<Block>) -> Self {
66 Self {
67 identifier: identifier.into(),
68 block: block.into(),
69 parameters: Vec::new(),
70 is_variadic: false,
71 variadic_type: None,
72 return_type: None,
73 generic_parameters: None,
74 tokens: None,
75 }
76 }
77
78 pub fn with_tokens(mut self, tokens: LocalFunctionTokens) -> Self {
80 self.tokens = Some(tokens.into());
81 self
82 }
83
84 #[inline]
86 pub fn set_tokens(&mut self, tokens: LocalFunctionTokens) {
87 self.tokens = Some(tokens.into());
88 }
89
90 #[inline]
92 pub fn get_tokens(&self) -> Option<&LocalFunctionTokens> {
93 self.tokens.as_deref()
94 }
95
96 #[inline]
98 pub fn mutate_tokens(&mut self) -> Option<&mut LocalFunctionTokens> {
99 self.tokens.as_deref_mut()
100 }
101
102 pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
104 self.parameters.push(parameter.into());
105 self
106 }
107
108 pub fn variadic(mut self) -> Self {
110 self.is_variadic = true;
111 self
112 }
113
114 pub fn with_variadic_type(mut self, r#type: impl Into<FunctionVariadicType>) -> Self {
118 self.is_variadic = true;
119 self.variadic_type = Some(r#type.into());
120 self
121 }
122
123 pub fn set_variadic_type(&mut self, r#type: impl Into<FunctionVariadicType>) {
127 self.is_variadic = true;
128 self.variadic_type = Some(r#type.into());
129 }
130
131 #[inline]
133 pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
134 self.variadic_type.as_ref()
135 }
136
137 #[inline]
139 pub fn has_variadic_type(&self) -> bool {
140 self.variadic_type.is_some()
141 }
142
143 #[inline]
145 pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
146 self.variadic_type.as_mut()
147 }
148
149 pub fn with_return_type(mut self, return_type: impl Into<FunctionReturnType>) -> Self {
151 self.return_type = Some(return_type.into());
152 self
153 }
154
155 pub fn set_return_type(&mut self, return_type: impl Into<FunctionReturnType>) {
157 self.return_type = Some(return_type.into());
158 }
159
160 #[inline]
162 pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
163 self.return_type.as_ref()
164 }
165
166 #[inline]
168 pub fn has_return_type(&self) -> bool {
169 self.return_type.is_some()
170 }
171
172 #[inline]
174 pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
175 self.return_type.as_mut()
176 }
177
178 pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
180 self.generic_parameters = Some(generic_parameters);
181 self
182 }
183
184 #[inline]
186 pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
187 self.generic_parameters = Some(generic_parameters);
188 }
189
190 #[inline]
192 pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
193 self.generic_parameters.as_ref()
194 }
195
196 #[inline]
198 pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
199 &mut self.parameters
200 }
201
202 #[inline]
204 pub fn mutate_block(&mut self) -> &mut Block {
205 &mut self.block
206 }
207
208 #[inline]
210 pub fn mutate_identifier(&mut self) -> &mut Identifier {
211 &mut self.identifier
212 }
213
214 #[inline]
216 pub fn get_block(&self) -> &Block {
217 &self.block
218 }
219
220 #[inline]
222 pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
223 &self.parameters
224 }
225
226 #[inline]
228 pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
229 self.parameters.iter()
230 }
231
232 #[inline]
234 pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
235 self.parameters.iter_mut()
236 }
237
238 #[inline]
240 pub fn get_identifier(&self) -> &Identifier {
241 &self.identifier
242 }
243
244 #[inline]
246 pub fn get_name(&self) -> &str {
247 self.identifier.get_name()
248 }
249
250 #[inline]
252 pub fn has_parameter(&self, name: &str) -> bool {
253 self.parameters
254 .iter()
255 .any(|parameter| parameter.get_name() == name)
256 }
257
258 #[inline]
260 pub fn has_parameters(&self) -> bool {
261 !self.parameters.is_empty()
262 }
263
264 #[inline]
266 pub fn is_variadic(&self) -> bool {
267 self.is_variadic
268 }
269
270 #[inline]
272 pub fn parameters_count(&self) -> usize {
273 self.parameters.len()
274 }
275
276 pub fn clear_types(&mut self) {
278 self.return_type.take();
279 self.variadic_type.take();
280 self.generic_parameters.take();
281 for parameter in &mut self.parameters {
282 parameter.remove_type();
283 }
284 if let Some(tokens) = &mut self.tokens {
285 tokens.variable_arguments_colon.take();
286 }
287 }
288
289 pub fn mutate_first_token(&mut self) -> &mut Token {
291 self.set_default_tokens();
292 &mut self.tokens.as_deref_mut().unwrap().local
293 }
294
295 pub fn mutate_last_token(&mut self) -> &mut Token {
298 self.set_default_tokens();
299 &mut self.tokens.as_deref_mut().unwrap().end
300 }
301
302 fn set_default_tokens(&mut self) {
303 if self.tokens.is_none() {
304 self.tokens = Some(
305 LocalFunctionTokens {
306 local: Token::from_content("local"),
307 function_body: FunctionBodyTokens {
308 function: Token::from_content("function"),
309 opening_parenthese: Token::from_content("("),
310 closing_parenthese: Token::from_content(")"),
311 end: Token::from_content("end"),
312 parameter_commas: Vec::new(),
313 variable_arguments: None,
314 variable_arguments_colon: None,
315 return_type_colon: None,
316 },
317 }
318 .into(),
319 );
320 }
321 }
322
323 super::impl_token_fns!(
324 target = [identifier]
325 iter = [parameters, generic_parameters, tokens]
326 );
327}
328
329#[cfg(test)]
330mod test {
331 use super::*;
332
333 #[test]
334 fn has_parameter_is_true_when_single_param_matches() {
335 let func = LocalFunctionStatement::from_name("foo", Block::default()).with_parameter("bar");
336
337 assert!(func.has_parameter("bar"));
338 }
339
340 #[test]
341 fn has_parameter_is_true_when_at_least_one_param_matches() {
342 let func = LocalFunctionStatement::from_name("foo", Block::default())
343 .with_parameter("bar")
344 .with_parameter("baz");
345
346 assert!(func.has_parameter("baz"));
347 }
348
349 #[test]
350 fn has_parameter_is_false_when_none_matches() {
351 let func = LocalFunctionStatement::from_name("foo", Block::default())
352 .with_parameter("bar")
353 .with_parameter("baz");
354
355 assert!(!func.has_parameter("foo"));
356 }
357}