php_parser_rs/parser/ast/
modifiers.rs

1use schemars::JsonSchema;
2use serde::Deserialize;
3use serde::Serialize;
4
5use crate::lexer::token::Span;
6
7#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
8#[serde(tag = "type")]
9pub enum Visibility {
10    Public,
11    Protected,
12    Private,
13}
14
15#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize, JsonSchema)]
16#[serde(tag = "type", content = "value")]
17pub enum VisibilityModifier {
18    Public(Span),
19    Protected(Span),
20    Private(Span),
21}
22
23#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
24#[serde(tag = "type", content = "value")]
25pub enum PromotedPropertyModifier {
26    Public(Span),
27    Protected(Span),
28    Private(Span),
29    Readonly(Span),
30}
31
32impl PromotedPropertyModifier {
33    pub fn span(&self) -> Span {
34        match self {
35            PromotedPropertyModifier::Public(span) => *span,
36            PromotedPropertyModifier::Protected(span) => *span,
37            PromotedPropertyModifier::Private(span) => *span,
38            PromotedPropertyModifier::Readonly(span) => *span,
39        }
40    }
41}
42
43impl std::fmt::Display for PromotedPropertyModifier {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        match self {
46            PromotedPropertyModifier::Public(_) => write!(f, "public"),
47            PromotedPropertyModifier::Protected(_) => write!(f, "protected"),
48            PromotedPropertyModifier::Private(_) => write!(f, "private"),
49            PromotedPropertyModifier::Readonly(_) => write!(f, "readonly"),
50        }
51    }
52}
53
54#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
55#[repr(transparent)]
56pub struct PromotedPropertyModifierGroup {
57    pub modifiers: Vec<PromotedPropertyModifier>,
58}
59
60impl PromotedPropertyModifierGroup {
61    pub fn is_empty(&self) -> bool {
62        self.modifiers.is_empty()
63    }
64
65    pub fn get_readonly(&self) -> Option<&PromotedPropertyModifier> {
66        self.modifiers
67            .iter()
68            .find(|modifier| matches!(modifier, PromotedPropertyModifier::Readonly { .. }))
69    }
70
71    pub fn has_readonly(&self) -> bool {
72        self.modifiers
73            .iter()
74            .any(|modifier| matches!(modifier, PromotedPropertyModifier::Readonly { .. }))
75    }
76
77    pub fn visibility(&self) -> Visibility {
78        self.modifiers
79            .iter()
80            .find_map(|modifier| match modifier {
81                PromotedPropertyModifier::Protected { .. } => Some(Visibility::Protected),
82                PromotedPropertyModifier::Private { .. } => Some(Visibility::Private),
83                PromotedPropertyModifier::Public { .. } => Some(Visibility::Public),
84                _ => None,
85            })
86            .unwrap_or(Visibility::Public)
87    }
88}
89
90#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
91#[serde(tag = "type", content = "value")]
92pub enum PropertyModifier {
93    Public(Span),
94    Protected(Span),
95    Private(Span),
96    Static(Span),
97    Readonly(Span),
98}
99
100impl PropertyModifier {
101    pub fn span(&self) -> Span {
102        match self {
103            PropertyModifier::Public(span) => *span,
104            PropertyModifier::Protected(span) => *span,
105            PropertyModifier::Private(span) => *span,
106            PropertyModifier::Static(span) => *span,
107            PropertyModifier::Readonly(span) => *span,
108        }
109    }
110}
111
112#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
113#[repr(transparent)]
114pub struct PropertyModifierGroup {
115    pub modifiers: Vec<PropertyModifier>,
116}
117
118impl PropertyModifierGroup {
119    pub fn is_empty(&self) -> bool {
120        self.modifiers.is_empty()
121    }
122
123    pub fn get_readonly(&self) -> Option<&PropertyModifier> {
124        self.modifiers
125            .iter()
126            .find(|modifier| matches!(modifier, PropertyModifier::Readonly { .. }))
127    }
128
129    pub fn get_static(&self) -> Option<&PropertyModifier> {
130        self.modifiers
131            .iter()
132            .find(|modifier| matches!(modifier, PropertyModifier::Static { .. }))
133    }
134
135    pub fn has_readonly(&self) -> bool {
136        self.modifiers
137            .iter()
138            .any(|modifier| matches!(modifier, PropertyModifier::Readonly { .. }))
139    }
140
141    pub fn has_static(&self) -> bool {
142        self.modifiers
143            .iter()
144            .any(|modifier| matches!(modifier, PropertyModifier::Static { .. }))
145    }
146
147    pub fn visibility(&self) -> Visibility {
148        self.modifiers
149            .iter()
150            .find_map(|modifier| match modifier {
151                PropertyModifier::Protected { .. } => Some(Visibility::Protected),
152                PropertyModifier::Private { .. } => Some(Visibility::Private),
153                PropertyModifier::Public { .. } => Some(Visibility::Public),
154                _ => None,
155            })
156            .unwrap_or(Visibility::Public)
157    }
158}
159
160#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
161#[serde(tag = "type", content = "value")]
162pub enum MethodModifier {
163    Final(Span),
164    Static(Span),
165    Abstract(Span),
166    Public(Span),
167    Protected(Span),
168    Private(Span),
169}
170
171impl MethodModifier {
172    pub fn span(&self) -> Span {
173        match self {
174            MethodModifier::Final(span) => *span,
175            MethodModifier::Static(span) => *span,
176            MethodModifier::Abstract(span) => *span,
177            MethodModifier::Public(span) => *span,
178            MethodModifier::Protected(span) => *span,
179            MethodModifier::Private(span) => *span,
180        }
181    }
182}
183
184#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
185#[repr(transparent)]
186pub struct MethodModifierGroup {
187    pub modifiers: Vec<MethodModifier>,
188}
189
190impl MethodModifierGroup {
191    pub fn is_empty(&self) -> bool {
192        self.modifiers.is_empty()
193    }
194
195    pub fn has_final(&self) -> bool {
196        self.modifiers
197            .iter()
198            .any(|modifier| matches!(modifier, MethodModifier::Final { .. }))
199    }
200
201    pub fn has_static(&self) -> bool {
202        self.modifiers
203            .iter()
204            .any(|modifier| matches!(modifier, MethodModifier::Static { .. }))
205    }
206
207    pub fn has_abstract(&self) -> bool {
208        self.modifiers
209            .iter()
210            .any(|modifier| matches!(modifier, MethodModifier::Abstract { .. }))
211    }
212
213    pub fn get_abstract(&self) -> Option<&MethodModifier> {
214        self.modifiers
215            .iter()
216            .find(|modifier| matches!(modifier, MethodModifier::Abstract { .. }))
217    }
218
219    pub fn visibility(&self) -> Visibility {
220        self.modifiers
221            .iter()
222            .find_map(|modifier| match modifier {
223                MethodModifier::Protected { .. } => Some(Visibility::Protected),
224                MethodModifier::Private { .. } => Some(Visibility::Private),
225                MethodModifier::Public { .. } => Some(Visibility::Public),
226                _ => None,
227            })
228            .unwrap_or(Visibility::Public)
229    }
230}
231
232#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
233#[serde(tag = "type", content = "value")]
234pub enum ClassModifier {
235    Final(Span),
236    Abstract(Span),
237    Readonly(Span),
238}
239
240#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
241#[repr(transparent)]
242pub struct ClassModifierGroup {
243    pub modifiers: Vec<ClassModifier>,
244}
245
246impl ClassModifierGroup {
247    pub fn is_empty(&self) -> bool {
248        self.modifiers.is_empty()
249    }
250
251    pub fn has_final(&self) -> bool {
252        self.modifiers
253            .iter()
254            .any(|modifier| matches!(modifier, ClassModifier::Final { .. }))
255    }
256
257    pub fn has_readonly(&self) -> bool {
258        self.modifiers
259            .iter()
260            .any(|modifier| matches!(modifier, ClassModifier::Readonly { .. }))
261    }
262
263    pub fn has_abstract(&self) -> bool {
264        self.modifiers
265            .iter()
266            .any(|modifier| matches!(modifier, ClassModifier::Abstract { .. }))
267    }
268}
269
270#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
271#[serde(tag = "type", content = "value")]
272pub enum ConstantModifier {
273    Final(Span),
274    Public(Span),
275    Protected(Span),
276    Private(Span),
277}
278
279#[derive(Debug, Clone, Eq, PartialEq, Deserialize, Serialize, JsonSchema)]
280#[repr(transparent)]
281pub struct ConstantModifierGroup {
282    pub modifiers: Vec<ConstantModifier>,
283}
284
285impl ConstantModifierGroup {
286    pub fn is_empty(&self) -> bool {
287        self.modifiers.is_empty()
288    }
289
290    pub fn has_final(&self) -> bool {
291        self.modifiers
292            .iter()
293            .any(|modifier| matches!(modifier, ConstantModifier::Final { .. }))
294    }
295
296    pub fn visibility(&self) -> Visibility {
297        self.modifiers
298            .iter()
299            .find_map(|modifier| match modifier {
300                ConstantModifier::Protected { .. } => Some(Visibility::Protected),
301                ConstantModifier::Private { .. } => Some(Visibility::Private),
302                ConstantModifier::Public { .. } => Some(Visibility::Public),
303                _ => None,
304            })
305            .unwrap_or(Visibility::Public)
306    }
307}