darklua_core/nodes/
arguments.rs

1use std::iter;
2
3use crate::nodes::{Expression, StringExpression, TableExpression, Token};
4
5#[derive(Clone, Debug, PartialEq, Eq)]
6pub struct TupleArgumentsTokens {
7    pub opening_parenthese: Token,
8    pub closing_parenthese: Token,
9    pub commas: Vec<Token>,
10}
11
12impl TupleArgumentsTokens {
13    super::impl_token_fns!(
14        target = [opening_parenthese, closing_parenthese]
15        iter = [commas]
16    );
17}
18
19#[derive(Clone, Debug, Default, PartialEq, Eq)]
20pub struct TupleArguments {
21    values: Vec<Expression>,
22    tokens: Option<TupleArgumentsTokens>,
23}
24
25impl TupleArguments {
26    pub fn new(values: Vec<Expression>) -> Self {
27        Self {
28            values,
29            tokens: None,
30        }
31    }
32
33    #[inline]
34    pub fn to_expressions(self) -> Vec<Expression> {
35        self.values
36    }
37
38    pub fn with_tokens(mut self, tokens: TupleArgumentsTokens) -> Self {
39        self.tokens = Some(tokens);
40        self
41    }
42
43    #[inline]
44    pub fn set_tokens(&mut self, tokens: TupleArgumentsTokens) {
45        self.tokens = Some(tokens);
46    }
47
48    #[inline]
49    pub fn get_tokens(&self) -> Option<&TupleArgumentsTokens> {
50        self.tokens.as_ref()
51    }
52
53    pub fn with_argument<T: Into<Expression>>(mut self, argument: T) -> Self {
54        self.values.push(argument.into());
55        self
56    }
57
58    #[inline]
59    pub fn len(&self) -> usize {
60        self.values.len()
61    }
62
63    #[inline]
64    pub fn is_empty(&self) -> bool {
65        self.values.is_empty()
66    }
67
68    #[inline]
69    pub fn iter_values(&self) -> impl Iterator<Item = &Expression> {
70        self.values.iter()
71    }
72
73    #[inline]
74    pub fn iter_mut_values(&mut self) -> impl Iterator<Item = &mut Expression> {
75        self.values.iter_mut()
76    }
77
78    super::impl_token_fns!(iter = [tokens]);
79}
80
81impl From<Arguments> for TupleArguments {
82    fn from(arguments: Arguments) -> Self {
83        match arguments {
84            Arguments::Tuple(tuple) => tuple,
85            Arguments::String(string) => TupleArguments::default().with_argument(string),
86            Arguments::Table(table) => TupleArguments::default().with_argument(table),
87        }
88    }
89}
90
91impl iter::FromIterator<Expression> for TupleArguments {
92    fn from_iter<T: IntoIterator<Item = Expression>>(iter: T) -> Self {
93        Self {
94            values: iter.into_iter().collect(),
95            tokens: None,
96        }
97    }
98}
99
100#[derive(Clone, Debug, PartialEq, Eq)]
101pub enum Arguments {
102    Tuple(TupleArguments),
103    String(StringExpression),
104    Table(TableExpression),
105}
106
107impl Arguments {
108    pub fn to_expressions(self) -> Vec<Expression> {
109        match self {
110            Self::Tuple(expressions) => expressions.to_expressions(),
111            Self::String(string) => vec![string.into()],
112            Self::Table(table) => vec![table.into()],
113        }
114    }
115
116    pub fn with_argument<T: Into<Expression>>(self, argument: T) -> Self {
117        TupleArguments::from(self).with_argument(argument).into()
118    }
119
120    pub fn clear_comments(&mut self) {
121        match self {
122            Arguments::Tuple(tuple) => tuple.clear_comments(),
123            Arguments::String(_) | Arguments::Table(_) => {}
124        }
125    }
126
127    pub fn clear_whitespaces(&mut self) {
128        match self {
129            Arguments::Tuple(tuple) => tuple.clear_whitespaces(),
130            Arguments::String(_) | Arguments::Table(_) => {}
131        }
132    }
133
134    pub(crate) fn replace_referenced_tokens(&mut self, code: &str) {
135        match self {
136            Arguments::Tuple(tuple) => tuple.replace_referenced_tokens(code),
137            Arguments::String(_) | Arguments::Table(_) => {}
138        }
139    }
140
141    pub(crate) fn shift_token_line(&mut self, amount: usize) {
142        match self {
143            Arguments::Tuple(tuple) => tuple.shift_token_line(amount),
144            Arguments::String(_) | Arguments::Table(_) => {}
145        }
146    }
147
148    pub(crate) fn filter_comments(&mut self, filter: impl Fn(&super::Trivia) -> bool) {
149        match self {
150            Arguments::Tuple(tuple) => tuple.filter_comments(filter),
151            Arguments::String(_) | Arguments::Table(_) => {}
152        }
153    }
154}
155
156impl Default for Arguments {
157    fn default() -> Self {
158        Self::Tuple(TupleArguments::default())
159    }
160}
161
162impl From<TupleArguments> for Arguments {
163    fn from(tuple: TupleArguments) -> Self {
164        Self::Tuple(tuple)
165    }
166}
167
168impl From<TableExpression> for Arguments {
169    fn from(table: TableExpression) -> Self {
170        Self::Table(table)
171    }
172}
173
174impl From<StringExpression> for Arguments {
175    fn from(string: StringExpression) -> Self {
176        Self::String(string)
177    }
178}