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