sdml_core/model/definitions/
events.rs1use crate::{
2 load::ModuleLoader,
3 model::{
4 annotations::{Annotation, AnnotationBuilder, AnnotationProperty, HasAnnotations},
5 check::{validate_multiple_method_duplicates, MaybeIncomplete, Validate},
6 definitions::SourceEntity,
7 identifiers::{Identifier, IdentifierReference},
8 members::Member,
9 modules::Module,
10 values::Value,
11 HasName, HasOptionalBody, HasSourceSpan, References, Span,
12 },
13 store::ModuleStore,
14};
15use sdml_errors::diagnostics::functions::IdentifierCaseConvention;
16use std::{
17 collections::{BTreeMap, BTreeSet},
18 fmt::Debug,
19};
20
21#[cfg(feature = "serde")]
22use serde::{Deserialize, Serialize};
23
24use super::HasMultiMembers;
25
26#[derive(Clone, Debug)]
32#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
33pub struct EventDef {
34 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
35 span: Option<Span>,
36 name: Identifier,
37 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
38 body: Option<EventBody>,
39}
40
41#[derive(Clone, Debug)]
43#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
44pub struct EventBody {
45 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
46 span: Option<Span>,
47 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Vec::is_empty"))]
48 annotations: Vec<Annotation>,
49 source_entity: SourceEntity,
50 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "BTreeMap::is_empty"))]
51 members: BTreeMap<Identifier, Member>,
52}
53
54impl HasName for EventDef {
59 fn name(&self) -> &Identifier {
60 &self.name
61 }
62
63 fn set_name(&mut self, name: Identifier) {
64 self.name = name;
65 }
66}
67
68impl HasOptionalBody for EventDef {
69 type Body = EventBody;
70
71 fn body(&self) -> Option<&Self::Body> {
72 self.body.as_ref()
73 }
74
75 fn body_mut(&mut self) -> Option<&mut Self::Body> {
76 self.body.as_mut()
77 }
78
79 fn set_body(&mut self, body: Self::Body) {
80 self.body = Some(body);
81 }
82
83 fn unset_body(&mut self) {
84 self.body = None;
85 }
86}
87
88impl References for EventDef {
89 fn referenced_annotations<'a>(&'a self, names: &mut BTreeSet<&'a IdentifierReference>) {
90 if let Some(inner) = &self.body {
91 inner.referenced_annotations(names);
92 }
93 }
94
95 fn referenced_types<'a>(&'a self, names: &mut BTreeSet<&'a IdentifierReference>) {
96 if let Some(inner) = &self.body {
97 inner.referenced_types(names);
98 }
99 }
100}
101
102impl HasSourceSpan for EventDef {
103 fn with_source_span(self, span: Span) -> Self {
104 let mut self_mut = self;
105 self_mut.span = Some(span);
106 self_mut
107 }
108
109 fn source_span(&self) -> Option<&Span> {
110 self.span.as_ref()
111 }
112
113 fn set_source_span(&mut self, span: Span) {
114 self.span = Some(span);
115 }
116
117 fn unset_source_span(&mut self) {
118 self.span = None;
119 }
120}
121
122impl MaybeIncomplete for EventDef {
123 fn is_incomplete(&self, top: &Module, cache: &impl ModuleStore) -> bool {
124 if let Some(body) = &self.body {
125 body.is_incomplete(top, cache)
126 } else {
127 true
128 }
129 }
130}
131
132impl AnnotationBuilder for EventDef {
133 fn with_predicate<I, V>(self, predicate: I, value: V) -> Self
134 where
135 Self: Sized,
136 I: Into<IdentifierReference>,
137 V: Into<Value>,
138 {
139 let mut self_mut = self;
140 if let Some(ref mut inner) = self_mut.body {
141 inner.add_to_annotations(AnnotationProperty::new(predicate.into(), value.into()));
142 }
143 self_mut
144 }
145}
146
147impl Validate for EventDef {
148 fn validate(
149 &self,
150 top: &crate::model::modules::Module,
151 cache: &impl crate::store::ModuleStore,
152 loader: &impl crate::load::ModuleLoader,
153 check_constraints: bool,
154 ) {
155 self.name()
156 .validate(top, loader, Some(IdentifierCaseConvention::TypeDefinition));
157 if let Some(body) = &self.body {
158 body.validate(top, cache, loader, check_constraints);
159 }
160 }
161}
162
163impl EventDef {
164 pub fn new(name: Identifier) -> Self {
169 Self {
170 span: Default::default(),
171 name,
172 body: Default::default(),
173 }
174 }
175
176 pub fn with_body(self, body: EventBody) -> Self {
177 let mut self_mut = self;
178 self_mut.body = Some(body);
179 self_mut
180 }
181}
182
183impl HasAnnotations for EventBody {
188 fn has_annotations(&self) -> bool {
189 !self.annotations.is_empty()
190 }
191
192 fn annotation_count(&self) -> usize {
193 self.annotations.len()
194 }
195
196 fn annotations(&self) -> impl Iterator<Item = &Annotation> {
197 self.annotations.iter()
198 }
199
200 fn annotations_mut(&mut self) -> impl Iterator<Item = &mut Annotation> {
201 self.annotations.iter_mut()
202 }
203
204 fn add_to_annotations<I>(&mut self, value: I)
205 where
206 I: Into<Annotation>,
207 {
208 self.annotations.push(value.into())
209 }
210
211 fn extend_annotations<I>(&mut self, extension: I)
212 where
213 I: IntoIterator<Item = Annotation>,
214 {
215 self.annotations.extend(extension.into_iter())
216 }
217}
218
219impl HasSourceSpan for EventBody {
220 fn with_source_span(self, span: Span) -> Self {
221 let mut self_mut = self;
222 self_mut.span = Some(span);
223 self_mut
224 }
225
226 fn source_span(&self) -> Option<&Span> {
227 self.span.as_ref()
228 }
229
230 fn set_source_span(&mut self, span: Span) {
231 self.span = Some(span);
232 }
233
234 fn unset_source_span(&mut self) {
235 self.span = None;
236 }
237}
238
239impl MaybeIncomplete for EventBody {
240 fn is_incomplete(&self, top: &Module, cache: &impl ModuleStore) -> bool {
241 self.members().any(|elem| elem.is_incomplete(top, cache))
242 }
243}
244
245impl Validate for EventBody {
246 fn validate(
247 &self,
248 top: &Module,
249 cache: &impl ModuleStore,
250 loader: &impl ModuleLoader,
251 check_constraints: bool,
252 ) {
253 validate_multiple_method_duplicates(self, top, cache, loader);
254
255 self.source_entity()
256 .validate(top, cache, loader, check_constraints);
257 self.annotations()
258 .for_each(|a| a.validate(top, cache, loader, check_constraints));
259 self.members()
260 .for_each(|m| m.validate(top, cache, loader, check_constraints));
261 }
262}
263
264impl References for EventBody {
265 fn referenced_types<'a>(&'a self, names: &mut BTreeSet<&'a IdentifierReference>) {
266 self.source_entity().referenced_types(names);
267 self.members().for_each(|m| m.referenced_types(names));
268 }
269
270 fn referenced_annotations<'a>(&'a self, names: &mut BTreeSet<&'a IdentifierReference>) {
271 self.source_entity().referenced_annotations(names);
272 self.members().for_each(|m| m.referenced_annotations(names));
273 }
274}
275
276impl HasMultiMembers for EventBody {
277 fn has_any_members(&self) -> bool {
278 !(self.has_source_members() || self.has_members())
279 }
280
281 fn contains_any_member(&self, name: &Identifier) -> bool {
282 self.contains_source_member(name) || self.contains_member(name)
283 }
284
285 fn all_member_count(&self) -> usize {
286 self.source_member_count() + self.members.len()
287 }
288
289 fn all_member_names(&self) -> impl Iterator<Item = &Identifier> {
290 self.source_member_names().chain(self.member_names())
291 }
292}
293
294impl EventBody {
295 pub fn new(source_entity: SourceEntity) -> Self {
301 Self {
302 span: Default::default(),
303 annotations: Default::default(),
304 source_entity,
305 members: Default::default(),
306 }
307 }
308
309 pub fn with_members<I>(self, members: I) -> Self
310 where
311 I: IntoIterator<Item = Member>,
312 {
313 let mut self_mut = self;
314 self_mut.extend_members(members);
315 self_mut
316 }
317
318 pub const fn source_entity(&self) -> &SourceEntity {
324 &self.source_entity
325 }
326
327 pub fn set_source_entity(&mut self, source_entity: SourceEntity) {
329 self.source_entity = source_entity;
330 }
331
332 fn has_source_members(&self) -> bool {
334 self.source_entity.has_members()
335 }
336
337 fn source_member_count(&self) -> usize {
339 self.source_entity.member_count()
340 }
341
342 fn source_member_names(&self) -> impl Iterator<Item = &Identifier> {
343 self.source_entity.members()
344 }
345
346 fn contains_source_member(&self, name: &Identifier) -> bool {
347 self.source_entity.contains_member(name)
348 }
349
350 pub fn has_members(&self) -> bool {
355 !self.members.is_empty()
356 }
357
358 pub fn member_count(&self) -> usize {
359 self.members.len()
360 }
361
362 pub fn contains_member(&self, name: &Identifier) -> bool {
363 self.members.contains_key(name)
364 }
365
366 pub fn member(&self, name: &Identifier) -> Option<&Member> {
367 self.members.get(name)
368 }
369
370 pub fn member_mut(&mut self, name: &Identifier) -> Option<&mut Member> {
371 self.members.get_mut(name)
372 }
373
374 pub fn members(&self) -> impl Iterator<Item = &Member> {
375 self.members.values()
376 }
377
378 pub fn members_mut(&mut self) -> impl Iterator<Item = &mut Member> {
379 self.members.values_mut()
380 }
381
382 pub fn member_names(&self) -> impl Iterator<Item = &Identifier> {
383 self.members.keys()
384 }
385
386 pub fn add_to_members(&mut self, value: Member) -> Option<Member> {
387 self.members.insert(value.name().clone(), value)
388 }
389
390 pub fn extend_members<I>(&mut self, extension: I)
391 where
392 I: IntoIterator<Item = Member>,
393 {
394 self.members.extend(
395 extension
396 .into_iter()
397 .map(|elem| (elem.name().clone(), elem)),
398 )
399 }
400}