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)]
7pub struct LocalFunctionTokens {
8 pub local: Token,
9 pub function_body: FunctionBodyTokens,
10}
11
12impl LocalFunctionTokens {
13 super::impl_token_fns!(target = [local, function_body]);
14}
15
16impl std::ops::Deref for LocalFunctionTokens {
17 type Target = FunctionBodyTokens;
18
19 fn deref(&self) -> &Self::Target {
20 &self.function_body
21 }
22}
23
24impl std::ops::DerefMut for LocalFunctionTokens {
25 fn deref_mut(&mut self) -> &mut Self::Target {
26 &mut self.function_body
27 }
28}
29
30#[derive(Clone, Debug, PartialEq, Eq)]
31pub struct LocalFunctionStatement {
32 identifier: Identifier,
33 block: Block,
34 parameters: Vec<TypedIdentifier>,
35 is_variadic: bool,
36 variadic_type: Option<FunctionVariadicType>,
37 return_type: Option<FunctionReturnType>,
38 generic_parameters: Option<GenericParameters>,
39 tokens: Option<Box<LocalFunctionTokens>>,
40}
41
42impl LocalFunctionStatement {
43 pub fn new(
44 identifier: impl Into<Identifier>,
45 block: Block,
46 parameters: Vec<TypedIdentifier>,
47 is_variadic: bool,
48 ) -> Self {
49 Self {
50 identifier: identifier.into(),
51 block,
52 parameters,
53 is_variadic,
54 variadic_type: None,
55 return_type: None,
56 generic_parameters: None,
57 tokens: None,
58 }
59 }
60
61 pub fn from_name(identifier: impl Into<Identifier>, block: impl Into<Block>) -> Self {
62 Self {
63 identifier: identifier.into(),
64 block: block.into(),
65 parameters: Vec::new(),
66 is_variadic: false,
67 variadic_type: None,
68 return_type: None,
69 generic_parameters: None,
70 tokens: None,
71 }
72 }
73
74 pub fn with_tokens(mut self, tokens: LocalFunctionTokens) -> Self {
75 self.tokens = Some(tokens.into());
76 self
77 }
78
79 #[inline]
80 pub fn set_tokens(&mut self, tokens: LocalFunctionTokens) {
81 self.tokens = Some(tokens.into());
82 }
83
84 #[inline]
85 pub fn get_tokens(&self) -> Option<&LocalFunctionTokens> {
86 self.tokens.as_deref()
87 }
88
89 #[inline]
90 pub fn mutate_tokens(&mut self) -> Option<&mut LocalFunctionTokens> {
91 self.tokens.as_deref_mut()
92 }
93
94 pub fn with_parameter(mut self, parameter: impl Into<TypedIdentifier>) -> Self {
95 self.parameters.push(parameter.into());
96 self
97 }
98
99 pub fn variadic(mut self) -> Self {
100 self.is_variadic = true;
101 self
102 }
103
104 pub fn with_variadic_type(mut self, r#type: impl Into<FunctionVariadicType>) -> Self {
105 self.is_variadic = true;
106 self.variadic_type = Some(r#type.into());
107 self
108 }
109
110 pub fn set_variadic_type(&mut self, r#type: impl Into<FunctionVariadicType>) {
111 self.is_variadic = true;
112 self.variadic_type = Some(r#type.into());
113 }
114
115 #[inline]
116 pub fn get_variadic_type(&self) -> Option<&FunctionVariadicType> {
117 self.variadic_type.as_ref()
118 }
119
120 #[inline]
121 pub fn has_variadic_type(&self) -> bool {
122 self.variadic_type.is_some()
123 }
124
125 #[inline]
126 pub fn mutate_variadic_type(&mut self) -> Option<&mut FunctionVariadicType> {
127 self.variadic_type.as_mut()
128 }
129
130 pub fn with_return_type(mut self, return_type: impl Into<FunctionReturnType>) -> Self {
131 self.return_type = Some(return_type.into());
132 self
133 }
134
135 pub fn set_return_type(&mut self, return_type: impl Into<FunctionReturnType>) {
136 self.return_type = Some(return_type.into());
137 }
138
139 #[inline]
140 pub fn get_return_type(&self) -> Option<&FunctionReturnType> {
141 self.return_type.as_ref()
142 }
143
144 #[inline]
145 pub fn has_return_type(&self) -> bool {
146 self.return_type.is_some()
147 }
148
149 #[inline]
150 pub fn mutate_return_type(&mut self) -> Option<&mut FunctionReturnType> {
151 self.return_type.as_mut()
152 }
153
154 pub fn with_generic_parameters(mut self, generic_parameters: GenericParameters) -> Self {
155 self.generic_parameters = Some(generic_parameters);
156 self
157 }
158
159 #[inline]
160 pub fn set_generic_parameters(&mut self, generic_parameters: GenericParameters) {
161 self.generic_parameters = Some(generic_parameters);
162 }
163
164 #[inline]
165 pub fn get_generic_parameters(&self) -> Option<&GenericParameters> {
166 self.generic_parameters.as_ref()
167 }
168
169 #[inline]
170 pub fn mutate_parameters(&mut self) -> &mut Vec<TypedIdentifier> {
171 &mut self.parameters
172 }
173
174 #[inline]
175 pub fn mutate_block(&mut self) -> &mut Block {
176 &mut self.block
177 }
178
179 #[inline]
180 pub fn mutate_identifier(&mut self) -> &mut Identifier {
181 &mut self.identifier
182 }
183
184 #[inline]
185 pub fn get_block(&self) -> &Block {
186 &self.block
187 }
188
189 #[inline]
190 pub fn get_parameters(&self) -> &Vec<TypedIdentifier> {
191 &self.parameters
192 }
193
194 #[inline]
195 pub fn iter_parameters(&self) -> impl Iterator<Item = &TypedIdentifier> {
196 self.parameters.iter()
197 }
198
199 #[inline]
200 pub fn iter_mut_parameters(&mut self) -> impl Iterator<Item = &mut TypedIdentifier> {
201 self.parameters.iter_mut()
202 }
203
204 #[inline]
205 pub fn get_identifier(&self) -> &Identifier {
206 &self.identifier
207 }
208
209 #[inline]
210 pub fn get_name(&self) -> &str {
211 self.identifier.get_name()
212 }
213
214 #[inline]
215 pub fn has_parameter(&self, name: &str) -> bool {
216 self.parameters
217 .iter()
218 .any(|parameter| parameter.get_name() == name)
219 }
220
221 #[inline]
222 pub fn has_parameters(&self) -> bool {
223 !self.parameters.is_empty()
224 }
225
226 #[inline]
227 pub fn is_variadic(&self) -> bool {
228 self.is_variadic
229 }
230
231 #[inline]
232 pub fn parameters_count(&self) -> usize {
233 self.parameters.len()
234 }
235
236 pub fn clear_types(&mut self) {
237 self.return_type.take();
238 self.variadic_type.take();
239 self.generic_parameters.take();
240 for parameter in &mut self.parameters {
241 parameter.remove_type();
242 }
243 if let Some(tokens) = &mut self.tokens {
244 tokens.variable_arguments_colon.take();
245 }
246 }
247
248 super::impl_token_fns!(
249 target = [identifier]
250 iter = [parameters, generic_parameters, tokens]
251 );
252}
253
254#[cfg(test)]
255mod test {
256 use super::*;
257
258 #[test]
259 fn has_parameter_is_true_when_single_param_matches() {
260 let func = LocalFunctionStatement::from_name("foo", Block::default()).with_parameter("bar");
261
262 assert!(func.has_parameter("bar"));
263 }
264
265 #[test]
266 fn has_parameter_is_true_when_at_least_one_param_matches() {
267 let func = LocalFunctionStatement::from_name("foo", Block::default())
268 .with_parameter("bar")
269 .with_parameter("baz");
270
271 assert!(func.has_parameter("baz"));
272 }
273
274 #[test]
275 fn has_parameter_is_false_when_none_matches() {
276 let func = LocalFunctionStatement::from_name("foo", Block::default())
277 .with_parameter("bar")
278 .with_parameter("baz");
279
280 assert!(!func.has_parameter("foo"));
281 }
282}