Skip to main content

openbim_dt/
domain.rs

1//! Owned ISO 23387 type contracts used across standard boundaries.
2
3use crate::{
4    AnyUri, Base, Concept, DataTypeName, DateTime, Decimal, Language, MultiLanguageText, Rational,
5    Reference, Scale,
6};
7
8/// Owned `SubjectType` core shared by object types, groups, and data templates.
9///
10/// `SubjectType` requires at least one `HasPartRef` or `IsSubtypeOfRef`
11/// (a repeating choice with a minimum of one). `new` does not enforce this,
12/// so check a built value with its codec's schema validation before relying
13/// on it.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct Subject {
16    concept: Concept,
17    has_part_refs: Vec<Reference>,
18    is_subtype_of_refs: Vec<Reference>,
19}
20
21impl Subject {
22    #[must_use]
23    pub const fn new(concept: Concept) -> Self {
24        Self {
25            concept,
26            has_part_refs: Vec::new(),
27            is_subtype_of_refs: Vec::new(),
28        }
29    }
30    #[must_use]
31    pub const fn concept(&self) -> &Concept {
32        &self.concept
33    }
34    /// Mutable access to the shared `ConceptType` core.
35    pub fn concept_mut(&mut self) -> &mut Concept {
36        &mut self.concept
37    }
38    #[must_use]
39    pub fn has_part_refs(&self) -> &[Reference] {
40        &self.has_part_refs
41    }
42    pub fn add_has_part_ref(&mut self, value: Reference) {
43        self.has_part_refs.push(value);
44    }
45    /// `IsSubtypeOfRef` children; the schema permits any number.
46    #[must_use]
47    pub fn is_subtype_of_refs(&self) -> &[Reference] {
48        &self.is_subtype_of_refs
49    }
50    pub fn add_is_subtype_of_ref(&mut self, value: Reference) {
51        self.is_subtype_of_refs.push(value);
52    }
53    /// The first `IsSubtypeOfRef`, when present.
54    #[deprecated(since = "0.3.0", note = "use `is_subtype_of_refs`")]
55    #[must_use]
56    pub fn is_subtype_of_ref(&self) -> Option<&Reference> {
57        self.is_subtype_of_refs.first()
58    }
59    /// Replaces every `IsSubtypeOfRef` with `value` (none when `None`).
60    #[deprecated(since = "0.3.0", note = "use `add_is_subtype_of_ref`")]
61    pub fn set_is_subtype_of_ref(&mut self, value: Option<Reference>) {
62        self.is_subtype_of_refs.clear();
63        self.is_subtype_of_refs.extend(value);
64    }
65}
66
67/// ISO 23387 `ObjectTypeType`.
68#[derive(Debug, Clone, PartialEq, Eq)]
69pub struct ObjectType(Subject);
70impl ObjectType {
71    #[must_use]
72    pub const fn new(subject: Subject) -> Self {
73        Self(subject)
74    }
75    #[must_use]
76    pub const fn subject(&self) -> &Subject {
77        &self.0
78    }
79    /// Mutable access to the shared `SubjectType` core.
80    pub fn subject_mut(&mut self) -> &mut Subject {
81        &mut self.0
82    }
83    #[must_use]
84    pub fn into_subject(self) -> Subject {
85        self.0
86    }
87}
88
89/// One ordered simple value in ISO 23387 `ValueListType`.
90#[derive(Debug, Clone, PartialEq, Eq)]
91pub struct DataValue {
92    value: String,
93    order: Option<i32>,
94}
95impl DataValue {
96    #[must_use]
97    pub fn new(value: impl Into<String>, order: Option<i32>) -> Self {
98        Self {
99            value: value.into(),
100            order,
101        }
102    }
103    #[must_use]
104    pub fn value(&self) -> &str {
105        &self.value
106    }
107    #[must_use]
108    pub const fn order(&self) -> Option<i32> {
109        self.order
110    }
111}
112
113/// One language-specific ISO 23387 `ValueListType`.
114#[derive(Debug, Clone, PartialEq, Eq)]
115pub struct ValueList {
116    language: Language,
117    values: Vec<DataValue>,
118}
119impl ValueList {
120    #[must_use]
121    pub fn new(language: Language, first_value: DataValue) -> Self {
122        Self {
123            language,
124            values: vec![first_value],
125        }
126    }
127    #[must_use]
128    pub const fn language(&self) -> &Language {
129        &self.language
130    }
131    #[must_use]
132    pub fn values(&self) -> &[DataValue] {
133        &self.values
134    }
135    pub fn add_value(&mut self, value: DataValue) {
136        self.values.push(value);
137    }
138}
139
140/// One constraint carried by `DataTypeType`.
141#[derive(Debug, Clone, PartialEq, Eq)]
142pub enum DataTypeConstraint {
143    MinInclusive(String),
144    MinExclusive(String),
145    MaxInclusive(String),
146    MaxExclusive(String),
147}
148
149/// ISO 23387 `DataTypeType`.
150#[derive(Debug, Clone, PartialEq, Eq, Default)]
151pub struct DataType {
152    name: Option<DataTypeName>,
153    constraints: Vec<DataTypeConstraint>,
154    data_formats: Vec<String>,
155    possible_values: Vec<ValueList>,
156}
157impl DataType {
158    #[must_use]
159    pub fn new(name: Option<DataTypeName>) -> Self {
160        Self {
161            name,
162            ..Self::default()
163        }
164    }
165    #[must_use]
166    pub const fn name(&self) -> Option<&DataTypeName> {
167        self.name.as_ref()
168    }
169    #[must_use]
170    pub fn constraints(&self) -> &[DataTypeConstraint] {
171        &self.constraints
172    }
173    #[must_use]
174    pub fn data_formats(&self) -> &[String] {
175        &self.data_formats
176    }
177    #[must_use]
178    pub fn possible_values(&self) -> &[ValueList] {
179        &self.possible_values
180    }
181    pub fn add_constraint(&mut self, value: DataTypeConstraint) {
182        self.constraints.push(value);
183    }
184    pub fn add_data_format(&mut self, value: impl Into<String>) {
185        self.data_formats.push(value.into());
186    }
187    pub fn add_possible_values(&mut self, value: ValueList) {
188        self.possible_values.push(value);
189    }
190}
191
192/// ISO 23387 `PropertyType`.
193#[derive(Debug, Clone, PartialEq, Eq)]
194pub struct Property {
195    concept: Concept,
196    data_type: DataType,
197    symbols: Vec<String>,
198    dimension_refs: Vec<Reference>,
199    unit_refs: Vec<Reference>,
200    quantity_kind_refs: Vec<Reference>,
201    dependency_refs: Vec<Reference>,
202    specialization_ref: Option<Reference>,
203}
204impl Property {
205    #[must_use]
206    pub const fn new(concept: Concept, data_type: DataType) -> Self {
207        Self {
208            concept,
209            data_type,
210            symbols: Vec::new(),
211            dimension_refs: Vec::new(),
212            unit_refs: Vec::new(),
213            quantity_kind_refs: Vec::new(),
214            dependency_refs: Vec::new(),
215            specialization_ref: None,
216        }
217    }
218    #[must_use]
219    pub const fn concept(&self) -> &Concept {
220        &self.concept
221    }
222    /// Mutable access to the shared `ConceptType` core.
223    pub fn concept_mut(&mut self) -> &mut Concept {
224        &mut self.concept
225    }
226
227    #[must_use]
228    pub const fn data_type(&self) -> &DataType {
229        &self.data_type
230    }
231    #[must_use]
232    pub fn symbols(&self) -> &[String] {
233        &self.symbols
234    }
235    /// `DimensionRef` children; the schema permits any number.
236    #[must_use]
237    pub fn dimension_refs(&self) -> &[Reference] {
238        &self.dimension_refs
239    }
240    pub fn add_dimension_ref(&mut self, value: Reference) {
241        self.dimension_refs.push(value);
242    }
243    /// The first `DimensionRef`, when present.
244    #[deprecated(since = "0.3.0", note = "use `dimension_refs`")]
245    #[must_use]
246    pub fn dimension_ref(&self) -> Option<&Reference> {
247        self.dimension_refs.first()
248    }
249    /// Replaces every `DimensionRef` with `value` (none when `None`).
250    #[deprecated(since = "0.3.0", note = "use `add_dimension_ref`")]
251    pub fn set_dimension_ref(&mut self, value: Option<Reference>) {
252        self.dimension_refs.clear();
253        self.dimension_refs.extend(value);
254    }
255    #[must_use]
256    pub fn unit_refs(&self) -> &[Reference] {
257        &self.unit_refs
258    }
259    #[must_use]
260    pub fn quantity_kind_refs(&self) -> &[Reference] {
261        &self.quantity_kind_refs
262    }
263    #[must_use]
264    pub fn dependency_refs(&self) -> &[Reference] {
265        &self.dependency_refs
266    }
267    #[must_use]
268    pub const fn specialization_ref(&self) -> Option<&Reference> {
269        self.specialization_ref.as_ref()
270    }
271    pub fn add_symbol(&mut self, value: impl Into<String>) {
272        self.symbols.push(value.into());
273    }
274    pub fn add_unit_ref(&mut self, value: Reference) {
275        self.unit_refs.push(value);
276    }
277    pub fn add_quantity_kind_ref(&mut self, value: Reference) {
278        self.quantity_kind_refs.push(value);
279    }
280    pub fn add_dependency_ref(&mut self, value: Reference) {
281        self.dependency_refs.push(value);
282    }
283    pub fn set_specialization_ref(&mut self, value: Option<Reference>) {
284        self.specialization_ref = value;
285    }
286}
287
288/// ISO 23387 `QuantityKindType`.
289#[derive(Debug, Clone, PartialEq, Eq)]
290pub struct QuantityKind {
291    concept: Concept,
292    dimension_ref: Reference,
293}
294impl QuantityKind {
295    #[must_use]
296    pub const fn new(concept: Concept, dimension_ref: Reference) -> Self {
297        Self {
298            concept,
299            dimension_ref,
300        }
301    }
302    #[must_use]
303    pub const fn concept(&self) -> &Concept {
304        &self.concept
305    }
306    /// Mutable access to the shared `ConceptType` core.
307    pub fn concept_mut(&mut self) -> &mut Concept {
308        &mut self.concept
309    }
310    #[must_use]
311    pub const fn dimension_ref(&self) -> &Reference {
312        &self.dimension_ref
313    }
314}
315
316/// ISO 23387 `GroupOfPropertiesType`; the constructor enforces its non-empty property-reference sequence.
317#[derive(Debug, Clone, PartialEq, Eq)]
318pub struct GroupOfProperties {
319    subject: Subject,
320    property_refs: Vec<Reference>,
321}
322impl GroupOfProperties {
323    #[must_use]
324    pub fn new(subject: Subject, first_property_ref: Reference) -> Self {
325        Self {
326            subject,
327            property_refs: vec![first_property_ref],
328        }
329    }
330    #[must_use]
331    pub const fn subject(&self) -> &Subject {
332        &self.subject
333    }
334    /// Mutable access to the shared `SubjectType` core.
335    pub fn subject_mut(&mut self) -> &mut Subject {
336        &mut self.subject
337    }
338    #[must_use]
339    pub fn property_refs(&self) -> &[Reference] {
340        &self.property_refs
341    }
342    pub fn add_property_ref(&mut self, value: Reference) {
343        self.property_refs.push(value);
344    }
345}
346
347/// ISO 23387 `ReferenceDocumentType`.
348#[derive(Debug, Clone, PartialEq, Eq)]
349pub struct ReferenceDocument {
350    concept: Concept,
351    languages: Vec<Language>,
352    date_of_publication: Option<DateTime>,
353    author: Option<String>,
354    isbn: Option<String>,
355    publisher: Option<String>,
356    uri: Option<AnyUri>,
357}
358impl ReferenceDocument {
359    pub fn new(concept: Concept, first_language: Language) -> Self {
360        Self {
361            concept,
362            languages: vec![first_language],
363            date_of_publication: None,
364            author: None,
365            isbn: None,
366            publisher: None,
367            uri: None,
368        }
369    }
370    #[must_use]
371    pub const fn concept(&self) -> &Concept {
372        &self.concept
373    }
374    /// Mutable access to the shared `ConceptType` core.
375    pub fn concept_mut(&mut self) -> &mut Concept {
376        &mut self.concept
377    }
378    #[must_use]
379    pub fn languages(&self) -> &[Language] {
380        &self.languages
381    }
382    pub fn add_language(&mut self, value: Language) {
383        self.languages.push(value);
384    }
385    #[must_use]
386    pub const fn date_of_publication(&self) -> Option<&DateTime> {
387        self.date_of_publication.as_ref()
388    }
389    pub fn set_date_of_publication(&mut self, value: Option<DateTime>) {
390        self.date_of_publication = value;
391    }
392    #[must_use]
393    pub fn author(&self) -> Option<&str> {
394        self.author.as_deref()
395    }
396    pub fn set_author(&mut self, value: Option<String>) {
397        self.author = value;
398    }
399    #[must_use]
400    pub fn isbn(&self) -> Option<&str> {
401        self.isbn.as_deref()
402    }
403    pub fn set_isbn(&mut self, value: Option<String>) {
404        self.isbn = value;
405    }
406    #[must_use]
407    pub fn publisher(&self) -> Option<&str> {
408        self.publisher.as_deref()
409    }
410    pub fn set_publisher(&mut self, value: Option<String>) {
411        self.publisher = value;
412    }
413    #[must_use]
414    pub fn uri(&self) -> Option<&str> {
415        self.uri.as_ref().map(AnyUri::as_str)
416    }
417    pub fn set_uri(&mut self, value: Option<AnyUri>) {
418        self.uri = value;
419    }
420}
421
422/// Seven SI base-dimension exponents in Annex E declaration order.
423#[derive(Debug, Clone, PartialEq, Eq)]
424pub struct Dimension {
425    concept: Concept,
426    exponents: [Decimal; 7],
427}
428impl Dimension {
429    #[must_use]
430    pub const fn new(concept: Concept, exponents: [Decimal; 7]) -> Self {
431        Self { concept, exponents }
432    }
433    #[must_use]
434    pub const fn concept(&self) -> &Concept {
435        &self.concept
436    }
437    /// Mutable access to the shared `ConceptType` core.
438    pub fn concept_mut(&mut self) -> &mut Concept {
439        &mut self.concept
440    }
441    #[must_use]
442    pub const fn exponents(&self) -> &[Decimal; 7] {
443        &self.exponents
444    }
445}
446
447/// ISO 23387 `UnitType`, with all required scalar/reference fields present.
448#[derive(Debug, Clone, PartialEq, Eq)]
449pub struct Unit {
450    concept: Concept,
451    symbols: Vec<MultiLanguageText>,
452    dimension_ref: Reference,
453    scale: Scale,
454    base: Base,
455    coefficient: Rational,
456    offset: Rational,
457}
458impl Unit {
459    #[must_use]
460    pub const fn new(
461        concept: Concept,
462        dimension_ref: Reference,
463        scale: Scale,
464        base: Base,
465        coefficient: Rational,
466        offset: Rational,
467    ) -> Self {
468        Self {
469            concept,
470            symbols: Vec::new(),
471            dimension_ref,
472            scale,
473            base,
474            coefficient,
475            offset,
476        }
477    }
478    #[must_use]
479    pub const fn concept(&self) -> &Concept {
480        &self.concept
481    }
482    /// Mutable access to the shared `ConceptType` core.
483    pub fn concept_mut(&mut self) -> &mut Concept {
484        &mut self.concept
485    }
486    #[must_use]
487    pub fn symbols(&self) -> &[MultiLanguageText] {
488        &self.symbols
489    }
490    pub fn add_symbol(&mut self, value: MultiLanguageText) {
491        self.symbols.push(value);
492    }
493    #[must_use]
494    pub const fn dimension_ref(&self) -> &Reference {
495        &self.dimension_ref
496    }
497    #[must_use]
498    pub const fn scale(&self) -> &Scale {
499        &self.scale
500    }
501    #[must_use]
502    pub const fn base(&self) -> &Base {
503        &self.base
504    }
505    #[must_use]
506    pub const fn coefficient(&self) -> &Rational {
507        &self.coefficient
508    }
509    #[must_use]
510    pub const fn offset(&self) -> &Rational {
511        &self.offset
512    }
513}
514
515/// ISO 23387 `DataTemplateType`.
516///
517/// The schema requires at least one of `HasObjectTypeRef`, `HasPropertyRef`
518/// or `HasGroupOfPropertiesRef`, on top of the `SubjectType` requirement.
519#[derive(Debug, Clone, PartialEq, Eq)]
520pub struct DataTemplate {
521    subject: Subject,
522    object_type_refs: Vec<Reference>,
523    property_refs: Vec<Reference>,
524    group_of_properties_refs: Vec<Reference>,
525}
526impl DataTemplate {
527    #[must_use]
528    pub const fn new(subject: Subject) -> Self {
529        Self {
530            subject,
531            object_type_refs: Vec::new(),
532            property_refs: Vec::new(),
533            group_of_properties_refs: Vec::new(),
534        }
535    }
536    #[must_use]
537    pub const fn subject(&self) -> &Subject {
538        &self.subject
539    }
540    /// Mutable access to the shared `SubjectType` core.
541    pub fn subject_mut(&mut self) -> &mut Subject {
542        &mut self.subject
543    }
544    /// `HasObjectTypeRef` children; the schema permits any number.
545    #[must_use]
546    pub fn object_type_refs(&self) -> &[Reference] {
547        &self.object_type_refs
548    }
549    pub fn add_object_type_ref(&mut self, value: Reference) {
550        self.object_type_refs.push(value);
551    }
552    /// The first `HasObjectTypeRef`, when present.
553    #[deprecated(since = "0.3.0", note = "use `object_type_refs`")]
554    #[must_use]
555    pub fn object_type_ref(&self) -> Option<&Reference> {
556        self.object_type_refs.first()
557    }
558    #[must_use]
559    pub fn property_refs(&self) -> &[Reference] {
560        &self.property_refs
561    }
562    pub fn add_property_ref(&mut self, value: Reference) {
563        self.property_refs.push(value);
564    }
565    #[must_use]
566    pub fn group_of_properties_refs(&self) -> &[Reference] {
567        &self.group_of_properties_refs
568    }
569    pub fn add_group_of_properties_ref(&mut self, value: Reference) {
570        self.group_of_properties_refs.push(value);
571    }
572    /// Replaces every `HasObjectTypeRef` with `value` (none when `None`).
573    #[deprecated(since = "0.3.0", note = "use `add_object_type_ref`")]
574    pub fn set_object_type_ref(&mut self, value: Option<Reference>) {
575        self.object_type_refs.clear();
576        self.object_type_refs.extend(value);
577    }
578}