mago_syntax/ast/ast/class_like/
member.rs1use serde::Serialize;
2use strum::Display;
3
4use mago_span::HasSpan;
5use mago_span::Span;
6
7use crate::ast::Sequence;
8use crate::ast::ast::class_like::constant::ClassLikeConstant;
9use crate::ast::ast::class_like::enum_case::EnumCase;
10use crate::ast::ast::class_like::method::Method;
11use crate::ast::ast::class_like::property::Property;
12use crate::ast::ast::class_like::trait_use::TraitUse;
13use crate::ast::ast::expression::Expression;
14use crate::ast::ast::identifier::LocalIdentifier;
15use crate::ast::ast::variable::Variable;
16
17#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord, Display)]
18#[serde(tag = "type", content = "value")]
19#[repr(u8)]
20pub enum ClassLikeMember<'arena> {
21 TraitUse(TraitUse<'arena>),
22 Constant(ClassLikeConstant<'arena>),
23 Property(Property<'arena>),
24 EnumCase(EnumCase<'arena>),
25 Method(Method<'arena>),
26}
27
28#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord, Display)]
29#[serde(tag = "type", content = "value")]
30#[repr(u8)]
31pub enum ClassLikeMemberSelector<'arena> {
32 Identifier(LocalIdentifier<'arena>),
33 Variable(Variable<'arena>),
34 Expression(ClassLikeMemberExpressionSelector<'arena>),
35}
36
37#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord, Display)]
38#[serde(tag = "type", content = "value")]
39#[repr(u8)]
40pub enum ClassLikeConstantSelector<'arena> {
41 Identifier(LocalIdentifier<'arena>),
42 Expression(ClassLikeMemberExpressionSelector<'arena>),
43}
44
45#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, PartialOrd, Ord)]
46pub struct ClassLikeMemberExpressionSelector<'arena> {
47 pub left_brace: Span,
48 pub expression: &'arena Expression<'arena>,
49 pub right_brace: Span,
50}
51
52impl ClassLikeMember<'_> {
53 #[inline]
54 #[must_use]
55 pub const fn is_trait_use(&self) -> bool {
56 matches!(self, ClassLikeMember::TraitUse(_))
57 }
58
59 #[inline]
60 #[must_use]
61 pub const fn is_constant(&self) -> bool {
62 matches!(self, ClassLikeMember::Constant(_))
63 }
64
65 #[inline]
66 #[must_use]
67 pub const fn is_property(&self) -> bool {
68 matches!(self, ClassLikeMember::Property(_))
69 }
70
71 #[inline]
72 #[must_use]
73 pub const fn is_enum_case(&self) -> bool {
74 matches!(self, ClassLikeMember::EnumCase(_))
75 }
76
77 #[inline]
78 #[must_use]
79 pub const fn is_method(&self) -> bool {
80 matches!(self, ClassLikeMember::Method(_))
81 }
82}
83
84impl ClassLikeMemberSelector<'_> {
85 #[inline]
86 #[must_use]
87 pub const fn is_identifier(&self) -> bool {
88 matches!(self, ClassLikeMemberSelector::Identifier(_))
89 }
90
91 #[inline]
92 #[must_use]
93 pub const fn is_variable(&self) -> bool {
94 matches!(self, ClassLikeMemberSelector::Variable(_))
95 }
96
97 #[inline]
98 #[must_use]
99 pub const fn is_expression(&self) -> bool {
100 matches!(self, ClassLikeMemberSelector::Expression(_))
101 }
102}
103
104impl<'arena> Sequence<'arena, ClassLikeMember<'arena>> {
105 #[must_use]
106 pub fn contains_trait_uses(&self) -> bool {
107 self.iter().any(|member| matches!(member, ClassLikeMember::TraitUse(_)))
108 }
109
110 #[must_use]
111 pub fn contains_constants(&self) -> bool {
112 self.iter().any(|member| matches!(member, ClassLikeMember::Constant(_)))
113 }
114
115 #[must_use]
116 pub fn contains_properties(&self) -> bool {
117 self.iter().any(|member| matches!(member, ClassLikeMember::Property(_)))
118 }
119
120 #[must_use]
121 pub fn contains_enum_cases(&self) -> bool {
122 self.iter().any(|member| matches!(member, ClassLikeMember::EnumCase(_)))
123 }
124
125 #[must_use]
126 pub fn contains_methods(&self) -> bool {
127 self.iter().any(|member| matches!(member, ClassLikeMember::Method(_)))
128 }
129}
130
131impl HasSpan for ClassLikeMember<'_> {
132 fn span(&self) -> Span {
133 match self {
134 ClassLikeMember::TraitUse(trait_use) => trait_use.span(),
135 ClassLikeMember::Constant(constant) => constant.span(),
136 ClassLikeMember::Property(property) => property.span(),
137 ClassLikeMember::EnumCase(enum_case) => enum_case.span(),
138 ClassLikeMember::Method(method) => method.span(),
139 }
140 }
141}
142
143impl HasSpan for ClassLikeMemberSelector<'_> {
144 fn span(&self) -> Span {
145 match self {
146 ClassLikeMemberSelector::Identifier(i) => i.span(),
147 ClassLikeMemberSelector::Variable(v) => v.span(),
148 ClassLikeMemberSelector::Expression(e) => e.span(),
149 }
150 }
151}
152
153impl HasSpan for ClassLikeConstantSelector<'_> {
154 fn span(&self) -> Span {
155 match self {
156 ClassLikeConstantSelector::Identifier(i) => i.span(),
157 ClassLikeConstantSelector::Expression(e) => e.span(),
158 }
159 }
160}
161
162impl HasSpan for ClassLikeMemberExpressionSelector<'_> {
163 fn span(&self) -> Span {
164 self.left_brace.join(self.right_brace)
165 }
166}