1use crate::{
5 load::ModuleLoader,
6 model::{
7 check::{MaybeIncomplete, Validate},
8 identifiers::{Identifier, IdentifierReference},
9 modules::Module,
10 HasName, HasSourceSpan, References, Span,
11 },
12 store::ModuleStore,
13};
14use sdml_errors::diagnostics::functions::definition_is_incomplete;
15use std::{collections::BTreeSet, fmt::Debug};
16
17#[cfg(feature = "serde")]
18use serde::{Deserialize, Serialize};
19
20pub trait HasMultiMembers {
25 fn has_any_members(&self) -> bool;
26
27 fn contains_any_member(&self, name: &Identifier) -> bool;
28
29 fn all_member_count(&self) -> usize;
30
31 fn all_member_names(&self) -> impl Iterator<Item = &Identifier>;
32}
33
34#[derive(Clone, Debug)]
40#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
41pub enum Definition {
42 Datatype(DatatypeDef),
43 Dimension(DimensionDef),
44 Entity(EntityDef),
45 Enum(EnumDef),
46 Event(EventDef),
47 Property(PropertyDef),
48 Rdf(RdfDef),
49 Structure(StructureDef),
50 TypeClass(TypeClassDef),
51 Union(UnionDef),
52}
53
54impl From<&DatatypeDef> for Definition {
59 fn from(v: &DatatypeDef) -> Self {
60 Self::Datatype(v.clone())
61 }
62}
63
64impl From<DatatypeDef> for Definition {
65 fn from(v: DatatypeDef) -> Self {
66 Self::Datatype(v)
67 }
68}
69impl From<&DimensionDef> for Definition {
70 fn from(v: &DimensionDef) -> Self {
71 Self::Dimension(v.clone())
72 }
73}
74
75impl From<DimensionDef> for Definition {
76 fn from(v: DimensionDef) -> Self {
77 Self::Dimension(v)
78 }
79}
80
81impl From<&EntityDef> for Definition {
82 fn from(v: &EntityDef) -> Self {
83 Self::Entity(v.clone())
84 }
85}
86
87impl From<EntityDef> for Definition {
88 fn from(v: EntityDef) -> Self {
89 Self::Entity(v)
90 }
91}
92
93impl From<&EnumDef> for Definition {
94 fn from(v: &EnumDef) -> Self {
95 Self::Enum(v.clone())
96 }
97}
98
99impl From<EnumDef> for Definition {
100 fn from(v: EnumDef) -> Self {
101 Self::Enum(v)
102 }
103}
104
105impl From<&EventDef> for Definition {
106 fn from(v: &EventDef) -> Self {
107 Self::Event(v.clone())
108 }
109}
110
111impl From<EventDef> for Definition {
112 fn from(v: EventDef) -> Self {
113 Self::Event(v)
114 }
115}
116
117impl From<&PropertyDef> for Definition {
118 fn from(v: &PropertyDef) -> Self {
119 Self::Property(v.clone())
120 }
121}
122
123impl From<PropertyDef> for Definition {
124 fn from(v: PropertyDef) -> Self {
125 Self::Property(v)
126 }
127}
128
129impl From<&RdfDef> for Definition {
130 fn from(v: &RdfDef) -> Self {
131 Self::Rdf(v.clone())
132 }
133}
134
135impl From<RdfDef> for Definition {
136 fn from(v: RdfDef) -> Self {
137 Self::Rdf(v)
138 }
139}
140
141impl From<&StructureDef> for Definition {
142 fn from(v: &StructureDef) -> Self {
143 Self::Structure(v.clone())
144 }
145}
146
147impl From<StructureDef> for Definition {
148 fn from(v: StructureDef) -> Self {
149 Self::Structure(v)
150 }
151}
152
153impl From<&TypeClassDef> for Definition {
154 fn from(v: &TypeClassDef) -> Self {
155 Self::TypeClass(v.clone())
156 }
157}
158
159impl From<TypeClassDef> for Definition {
160 fn from(v: TypeClassDef) -> Self {
161 Self::TypeClass(v)
162 }
163}
164
165impl From<&UnionDef> for Definition {
166 fn from(v: &UnionDef) -> Self {
167 Self::Union(v.clone())
168 }
169}
170
171impl From<UnionDef> for Definition {
172 fn from(v: UnionDef) -> Self {
173 Self::Union(v)
174 }
175}
176
177impl HasName for Definition {
178 fn name(&self) -> &Identifier {
179 match self {
180 Self::Datatype(v) => v.name(),
181 Self::Dimension(v) => v.name(),
182 Self::Entity(v) => v.name(),
183 Self::Enum(v) => v.name(),
184 Self::Event(v) => v.name(),
185 Self::Property(v) => v.name(),
186 Self::Rdf(v) => v.name(),
187 Self::Structure(v) => v.name(),
188 Self::TypeClass(v) => v.name(),
189 Self::Union(v) => v.name(),
190 }
191 }
192
193 fn set_name(&mut self, name: Identifier) {
194 match self {
195 Self::Datatype(v) => v.set_name(name),
196 Self::Dimension(v) => v.set_name(name),
197 Self::Entity(v) => v.set_name(name),
198 Self::Enum(v) => v.set_name(name),
199 Self::Event(v) => v.set_name(name),
200 Self::Property(v) => v.set_name(name),
201 Self::Rdf(v) => v.set_name(name),
202 Self::Structure(v) => v.set_name(name),
203 Self::TypeClass(v) => v.set_name(name),
204 Self::Union(v) => v.set_name(name),
205 }
206 }
207}
208
209impl HasSourceSpan for Definition {
210 #[inline]
211 fn with_source_span(self, span: Span) -> Self {
212 match self {
213 Self::Datatype(v) => Self::Datatype(v.with_source_span(span)),
214 Self::Dimension(v) => Self::Dimension(v.with_source_span(span)),
215 Self::Entity(v) => Self::Entity(v.with_source_span(span)),
216 Self::Enum(v) => Self::Enum(v.with_source_span(span)),
217 Self::Event(v) => Self::Event(v.with_source_span(span)),
218 Self::Property(v) => Self::Property(v.with_source_span(span)),
219 Self::Rdf(v) => Self::Rdf(v.with_source_span(span)),
220 Self::Structure(v) => Self::Structure(v.with_source_span(span)),
221 Self::TypeClass(v) => Self::TypeClass(v.with_source_span(span)),
222 Self::Union(v) => Self::Union(v.with_source_span(span)),
223 }
224 }
225
226 #[inline]
227 fn source_span(&self) -> Option<&Span> {
228 match self {
229 Self::Datatype(v) => v.source_span(),
230 Self::Dimension(v) => v.source_span(),
231 Self::Entity(v) => v.source_span(),
232 Self::Enum(v) => v.source_span(),
233 Self::Event(v) => v.source_span(),
234 Self::Property(v) => v.source_span(),
235 Self::Rdf(v) => v.source_span(),
236 Self::Structure(v) => v.source_span(),
237 Self::TypeClass(v) => v.source_span(),
238 Self::Union(v) => v.source_span(),
239 }
240 }
241
242 #[inline]
243 fn set_source_span(&mut self, span: Span) {
244 match self {
245 Self::Datatype(v) => v.set_source_span(span),
246 Self::Dimension(v) => v.set_source_span(span),
247 Self::Entity(v) => v.set_source_span(span),
248 Self::Enum(v) => v.set_source_span(span),
249 Self::Event(v) => v.set_source_span(span),
250 Self::Property(v) => v.set_source_span(span),
251 Self::Rdf(v) => v.set_source_span(span),
252 Self::Structure(v) => v.set_source_span(span),
253 Self::TypeClass(v) => v.set_source_span(span),
254 Self::Union(v) => v.set_source_span(span),
255 }
256 }
257
258 #[inline]
259 fn unset_source_span(&mut self) {
260 match self {
261 Self::Datatype(v) => v.unset_source_span(),
262 Self::Dimension(v) => v.unset_source_span(),
263 Self::Entity(v) => v.unset_source_span(),
264 Self::Enum(v) => v.unset_source_span(),
265 Self::Event(v) => v.unset_source_span(),
266 Self::Property(v) => v.unset_source_span(),
267 Self::Rdf(v) => v.unset_source_span(),
268 Self::Structure(v) => v.unset_source_span(),
269 Self::TypeClass(v) => v.unset_source_span(),
270 Self::Union(v) => v.unset_source_span(),
271 }
272 }
273}
274
275impl References for Definition {
276 fn referenced_annotations<'a>(&'a self, names: &mut BTreeSet<&'a IdentifierReference>) {
277 match self {
278 Self::Datatype(v) => v.referenced_annotations(names),
279 Self::Dimension(v) => v.referenced_annotations(names),
280 Self::Entity(v) => v.referenced_annotations(names),
281 Self::Enum(v) => v.referenced_annotations(names),
282 Self::Event(v) => v.referenced_annotations(names),
283 Self::Property(v) => v.referenced_annotations(names),
284 Self::Rdf(v) => v.referenced_annotations(names),
285 Self::Structure(v) => v.referenced_annotations(names),
286 Self::TypeClass(v) => v.referenced_annotations(names),
287 Self::Union(v) => v.referenced_annotations(names),
288 }
289 }
290
291 fn referenced_types<'a>(&'a self, names: &mut BTreeSet<&'a IdentifierReference>) {
292 match self {
293 Self::Datatype(v) => v.referenced_types(names),
294 Self::Dimension(v) => v.referenced_types(names),
295 Self::Entity(v) => v.referenced_types(names),
296 Self::Enum(v) => v.referenced_types(names),
297 Self::Event(v) => v.referenced_types(names),
298 Self::Property(v) => v.referenced_types(names),
299 Self::Rdf(v) => v.referenced_types(names),
300 Self::Structure(v) => v.referenced_types(names),
301 Self::TypeClass(v) => v.referenced_types(names),
302 Self::Union(v) => v.referenced_types(names),
303 }
304 }
305}
306
307impl MaybeIncomplete for Definition {
308 fn is_incomplete(&self, top: &Module, cache: &impl ModuleStore) -> bool {
309 match self {
310 Self::Datatype(v) => v.is_incomplete(top, cache),
311 Self::Dimension(v) => v.is_incomplete(top, cache),
312 Self::Entity(v) => v.is_incomplete(top, cache),
313 Self::Enum(v) => v.is_incomplete(top, cache),
314 Self::Event(v) => v.is_incomplete(top, cache),
315 Self::Property(v) => v.is_incomplete(top, cache),
316 Self::Rdf(v) => v.is_incomplete(top, cache),
317 Self::Structure(v) => v.is_incomplete(top, cache),
318 Self::TypeClass(v) => v.is_incomplete(top, cache),
319 Self::Union(v) => v.is_incomplete(top, cache),
320 }
321 }
322}
323
324impl Validate for Definition {
325 fn validate(
326 &self,
327 top: &Module,
328 cache: &impl ModuleStore,
329 loader: &impl ModuleLoader,
330 check_constraints: bool,
331 ) {
332 match self {
333 Definition::Datatype(v) => v.validate(top, cache, loader, check_constraints),
334 Definition::Dimension(v) => v.validate(top, cache, loader, check_constraints),
335 Definition::Entity(v) => v.validate(top, cache, loader, check_constraints),
336 Definition::Enum(v) => v.validate(top, cache, loader, check_constraints),
337 Definition::Event(v) => v.validate(top, cache, loader, check_constraints),
338 Definition::Property(v) => v.validate(top, cache, loader, check_constraints),
339 Definition::Rdf(v) => v.validate(top, cache, loader, check_constraints),
340 Definition::Structure(v) => v.validate(top, cache, loader, check_constraints),
341 Definition::TypeClass(v) => v.validate(top, cache, loader, check_constraints),
342 Definition::Union(v) => v.validate(top, cache, loader, check_constraints),
343 }
344 if self.is_incomplete(top, cache) {
345 loader
346 .report(&definition_is_incomplete(
347 top.file_id().copied().unwrap_or_default(),
348 self.source_span().map(|span| span.byte_range()),
349 top.name(),
350 ))
351 .unwrap()
352 }
353 }
354}
355
356impl Definition {
357 pub const fn is_datatype(&self) -> bool {
362 matches!(self, Self::Datatype(_))
363 }
364
365 pub const fn as_datatype(&self) -> Option<&DatatypeDef> {
366 match self {
367 Self::Datatype(v) => Some(v),
368 _ => None,
369 }
370 }
371
372 pub const fn is_dimension(&self) -> bool {
373 matches!(self, Self::Dimension(_))
374 }
375
376 pub const fn as_dimension(&self) -> Option<&DimensionDef> {
377 match self {
378 Self::Dimension(v) => Some(v),
379 _ => None,
380 }
381 }
382
383 pub const fn is_entity(&self) -> bool {
384 matches!(self, Self::Entity(_))
385 }
386
387 pub const fn as_entity(&self) -> Option<&EntityDef> {
388 match self {
389 Self::Entity(v) => Some(v),
390 _ => None,
391 }
392 }
393
394 pub const fn is_enum(&self) -> bool {
395 matches!(self, Self::Enum(_))
396 }
397
398 pub const fn as_enum(&self) -> Option<&EnumDef> {
399 match self {
400 Self::Enum(v) => Some(v),
401 _ => None,
402 }
403 }
404
405 pub const fn is_event(&self) -> bool {
406 matches!(self, Self::Event(_))
407 }
408
409 pub const fn as_event(&self) -> Option<&EventDef> {
410 match self {
411 Self::Event(v) => Some(v),
412 _ => None,
413 }
414 }
415
416 pub const fn is_property(&self) -> bool {
417 matches!(self, Self::Property(_))
418 }
419
420 pub const fn as_property(&self) -> Option<&PropertyDef> {
421 match self {
422 Self::Property(v) => Some(v),
423 _ => None,
424 }
425 }
426
427 pub const fn is_rdf(&self) -> bool {
428 matches!(self, Self::Rdf(_))
429 }
430
431 pub const fn as_rdf(&self) -> Option<&RdfDef> {
432 match self {
433 Self::Rdf(v) => Some(v),
434 _ => None,
435 }
436 }
437
438 pub const fn is_structure(&self) -> bool {
439 matches!(self, Self::Structure(_))
440 }
441
442 pub const fn as_structure(&self) -> Option<&StructureDef> {
443 match self {
444 Self::Structure(v) => Some(v),
445 _ => None,
446 }
447 }
448
449 pub const fn is_type_class(&self) -> bool {
450 matches!(self, Self::TypeClass(_))
451 }
452
453 pub const fn as_type_class(&self) -> Option<&TypeClassDef> {
454 match self {
455 Self::TypeClass(v) => Some(v),
456 _ => None,
457 }
458 }
459
460 pub const fn is_union(&self) -> bool {
461 matches!(self, Self::Union(_))
462 }
463
464 pub const fn as_union(&self) -> Option<&UnionDef> {
465 match self {
466 Self::Union(v) => Some(v),
467 _ => None,
468 }
469 }
470
471 #[inline(always)]
476 pub fn is_structured_type(&self) -> bool {
477 matches!(
478 self,
479 Self::Entity(_) | Self::Enum(_) | Self::Event(_) | Self::Structure(_) | Self::Union(_)
480 )
481 }
482
483 #[inline(always)]
484 pub fn is_type(&self) -> bool {
485 matches!(
486 self,
487 Self::Datatype(_)
488 | Self::Entity(_)
489 | Self::Enum(_)
490 | Self::Event(_)
491 | Self::Structure(_)
492 | Self::Union(_)
493 )
494 }
495
496 #[inline(always)]
497 pub fn is_library_definition(&self) -> bool {
498 matches!(self, Self::Rdf(_) | Self::TypeClass(_))
499 }
500}
501
502mod classes;
507pub use classes::{
508 MethodDef, TypeClassArgument, TypeClassBody, TypeClassDef, TypeClassReference, TypeVariable,
509};
510
511mod datatypes;
512pub use datatypes::DatatypeDef;
513
514mod dimensions;
515pub use dimensions::{
516 DimensionBody, DimensionDef, DimensionIdentity, DimensionParent, SourceEntity,
517};
518
519mod entities;
520pub use entities::{EntityBody, EntityDef};
521
522mod enums;
523pub use enums::{EnumBody, EnumDef, ValueVariant};
524
525mod events;
526pub use events::{EventBody, EventDef};
527
528mod properties;
529pub use properties::PropertyDef;
530
531mod structures;
532pub use structures::{StructureBody, StructureDef};
533
534mod unions;
535pub use unions::{TypeVariant, UnionBody, UnionDef};
536
537mod rdf;
538pub use rdf::RdfDef;