sdml_core/model/definitions/
mod.rs

1/*!
2Provide the Rust types that implement *definition*-related components of the SDML Grammar.
3*/
4use crate::{
5    load::ModuleLoader,
6    model::{check::MaybeIncomplete, members::Member, HasName, HasSourceSpan},
7    store::ModuleStore,
8};
9use sdml_errors::diagnostics::functions::definition_is_incomplete;
10use std::fmt::Debug;
11
12#[cfg(feature = "serde")]
13use serde::{Deserialize, Serialize};
14
15// ------------------------------------------------------------------------------------------------
16// Public Types ❱ Traits
17// ------------------------------------------------------------------------------------------------
18
19pub trait HasMembers {
20    fn has_members(&self) -> bool;
21
22    fn members_len(&self) -> usize;
23
24    fn members(&self) -> impl Iterator<Item = &Member>;
25
26    fn members_mut(&mut self) -> impl Iterator<Item = &mut Member>;
27
28    fn add_to_members(&mut self, value: Member);
29
30    fn extend_members<I>(&mut self, extension: I)
31    where
32        I: IntoIterator<Item = Member>;
33}
34
35pub trait HasVariants {
36    type Variant;
37
38    fn has_variants(&self) -> bool;
39
40    fn variants_len(&self) -> usize;
41
42    fn variants(&self) -> impl Iterator<Item = &Self::Variant>;
43
44    fn variants_mut(&mut self) -> impl Iterator<Item = &mut Self::Variant>;
45
46    fn add_to_variants(&mut self, value: Self::Variant);
47
48    fn extend_variants<I>(&mut self, extension: I)
49    where
50        I: IntoIterator<Item = Self::Variant>;
51}
52
53// ------------------------------------------------------------------------------------------------
54// Public Types ❱ Type Definitions
55// ------------------------------------------------------------------------------------------------
56
57/// Corresponds to the grammar rule `type_def`.
58#[derive(Clone, Debug)]
59#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
60pub enum Definition {
61    Datatype(DatatypeDef),
62    Entity(EntityDef),
63    Enum(EnumDef),
64    Event(EventDef),
65    Property(PropertyDef),
66    Rdf(RdfDef),
67    Structure(StructureDef),
68    TypeClass(TypeClassDef),
69    Union(UnionDef),
70}
71
72// ------------------------------------------------------------------------------------------------
73// Implementations ❱ Type Definitions
74// ------------------------------------------------------------------------------------------------
75
76impl_from_for_variant!(Definition, Datatype, DatatypeDef);
77
78impl_from_for_variant!(Definition, Entity, EntityDef);
79
80impl_from_for_variant!(Definition, Enum, EnumDef);
81
82impl_from_for_variant!(Definition, Event, EventDef);
83
84impl_from_for_variant!(Definition, Property, PropertyDef);
85
86impl_from_for_variant!(Definition, Rdf, RdfDef);
87
88impl_from_for_variant!(Definition, Structure, StructureDef);
89
90impl_from_for_variant!(Definition, TypeClass, TypeClassDef);
91
92impl_from_for_variant!(Definition, Union, UnionDef);
93
94impl_has_name_for!(Definition => variants Datatype, Entity, Enum, Event, Property, Rdf, Structure, TypeClass, Union);
95
96impl_has_source_span_for!(Definition => variants Datatype, Entity, Enum, Event, Property, Rdf, Structure, TypeClass, Union);
97
98impl_references_for!(Definition => variants Datatype, Entity, Enum, Event, Property, Rdf, Structure, TypeClass, Union);
99
100impl_maybe_incomplete_for!(Definition; variants Datatype, Entity, Enum, Event, Property, Rdf, Structure, TypeClass, Union);
101
102impl Validate for Definition {
103    fn validate(
104        &self,
105        top: &Module,
106        cache: &impl ModuleStore,
107        loader: &impl ModuleLoader,
108        check_constraints: bool,
109    ) {
110        match self {
111            Definition::Datatype(v) => v.validate(top, cache, loader, check_constraints),
112            Definition::Entity(v) => v.validate(top, cache, loader, check_constraints),
113            Definition::Enum(v) => v.validate(top, cache, loader, check_constraints),
114            Definition::Event(v) => v.validate(top, cache, loader, check_constraints),
115            Definition::Property(v) => v.validate(top, cache, loader, check_constraints),
116            Definition::Rdf(v) => v.validate(top, cache, loader, check_constraints),
117            Definition::Structure(v) => v.validate(top, cache, loader, check_constraints),
118            Definition::TypeClass(v) => v.validate(top, cache, loader, check_constraints),
119            Definition::Union(v) => v.validate(top, cache, loader, check_constraints),
120        }
121        if self.is_incomplete(top, cache) {
122            loader
123                .report(&definition_is_incomplete(
124                    top.file_id().copied().unwrap_or_default(),
125                    self.source_span().map(|span| span.byte_range()),
126                    top.name(),
127                ))
128                .unwrap()
129        }
130    }
131}
132
133impl Definition {
134    #[inline(always)]
135    pub fn is_datatype(&self) -> bool {
136        matches!(self, Self::Datatype(_))
137    }
138
139    #[inline(always)]
140    pub fn is_structured_type(&self) -> bool {
141        matches!(
142            self,
143            Self::Entity(_) | Self::Enum(_) | Self::Event(_) | Self::Structure(_) | Self::Union(_)
144        )
145    }
146
147    #[inline(always)]
148    pub fn is_type(&self) -> bool {
149        matches!(
150            self,
151            Self::Datatype(_)
152                | Self::Entity(_)
153                | Self::Enum(_)
154                | Self::Event(_)
155                | Self::Structure(_)
156                | Self::Union(_)
157        )
158    }
159
160    #[inline(always)]
161    pub fn is_library_definition(&self) -> bool {
162        matches!(self, Self::Rdf(_) | Self::TypeClass(_))
163    }
164}
165
166// ------------------------------------------------------------------------------------------------
167// Modules
168// ------------------------------------------------------------------------------------------------
169
170mod classes;
171pub use classes::{
172    MethodDef, TypeClassArgument, TypeClassBody, TypeClassDef, TypeClassReference, TypeVariable,
173};
174
175mod datatypes;
176pub use datatypes::DatatypeDef;
177
178mod entities;
179pub use entities::{EntityBody, EntityDef};
180
181mod enums;
182pub use enums::{EnumBody, EnumDef, ValueVariant};
183
184mod events;
185pub use events::EventDef;
186
187mod properties;
188pub use properties::PropertyDef;
189
190mod structures;
191pub use structures::{StructureBody, StructureDef};
192
193mod unions;
194pub use unions::{TypeVariant, UnionBody, UnionDef};
195
196mod rdf;
197pub use rdf::RdfDef;
198
199use super::{check::Validate, modules::Module};