openbim-dt 0.3.0

Lossless ISO 23387 data-template model and XML codec for Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
//! Owned ISO 23387 type contracts used across standard boundaries.

use crate::{
    AnyUri, Base, Concept, DataTypeName, DateTime, Decimal, Language, MultiLanguageText, Rational,
    Reference, Scale,
};

/// Owned `SubjectType` core shared by object types, groups, and data templates.
///
/// `SubjectType` requires at least one `HasPartRef` or `IsSubtypeOfRef`
/// (a repeating choice with a minimum of one). `new` does not enforce this,
/// so check a built value with its codec's schema validation before relying
/// on it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Subject {
    concept: Concept,
    has_part_refs: Vec<Reference>,
    is_subtype_of_refs: Vec<Reference>,
}

impl Subject {
    #[must_use]
    pub const fn new(concept: Concept) -> Self {
        Self {
            concept,
            has_part_refs: Vec::new(),
            is_subtype_of_refs: Vec::new(),
        }
    }
    #[must_use]
    pub const fn concept(&self) -> &Concept {
        &self.concept
    }
    /// Mutable access to the shared `ConceptType` core.
    pub fn concept_mut(&mut self) -> &mut Concept {
        &mut self.concept
    }
    #[must_use]
    pub fn has_part_refs(&self) -> &[Reference] {
        &self.has_part_refs
    }
    pub fn add_has_part_ref(&mut self, value: Reference) {
        self.has_part_refs.push(value);
    }
    /// `IsSubtypeOfRef` children; the schema permits any number.
    #[must_use]
    pub fn is_subtype_of_refs(&self) -> &[Reference] {
        &self.is_subtype_of_refs
    }
    pub fn add_is_subtype_of_ref(&mut self, value: Reference) {
        self.is_subtype_of_refs.push(value);
    }
    /// The first `IsSubtypeOfRef`, when present.
    #[deprecated(since = "0.3.0", note = "use `is_subtype_of_refs`")]
    #[must_use]
    pub fn is_subtype_of_ref(&self) -> Option<&Reference> {
        self.is_subtype_of_refs.first()
    }
    /// Replaces every `IsSubtypeOfRef` with `value` (none when `None`).
    #[deprecated(since = "0.3.0", note = "use `add_is_subtype_of_ref`")]
    pub fn set_is_subtype_of_ref(&mut self, value: Option<Reference>) {
        self.is_subtype_of_refs.clear();
        self.is_subtype_of_refs.extend(value);
    }
}

/// ISO 23387 `ObjectTypeType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ObjectType(Subject);
impl ObjectType {
    #[must_use]
    pub const fn new(subject: Subject) -> Self {
        Self(subject)
    }
    #[must_use]
    pub const fn subject(&self) -> &Subject {
        &self.0
    }
    /// Mutable access to the shared `SubjectType` core.
    pub fn subject_mut(&mut self) -> &mut Subject {
        &mut self.0
    }
    #[must_use]
    pub fn into_subject(self) -> Subject {
        self.0
    }
}

/// One ordered simple value in ISO 23387 `ValueListType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataValue {
    value: String,
    order: Option<i32>,
}
impl DataValue {
    #[must_use]
    pub fn new(value: impl Into<String>, order: Option<i32>) -> Self {
        Self {
            value: value.into(),
            order,
        }
    }
    #[must_use]
    pub fn value(&self) -> &str {
        &self.value
    }
    #[must_use]
    pub const fn order(&self) -> Option<i32> {
        self.order
    }
}

/// One language-specific ISO 23387 `ValueListType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValueList {
    language: Language,
    values: Vec<DataValue>,
}
impl ValueList {
    #[must_use]
    pub fn new(language: Language, first_value: DataValue) -> Self {
        Self {
            language,
            values: vec![first_value],
        }
    }
    #[must_use]
    pub const fn language(&self) -> &Language {
        &self.language
    }
    #[must_use]
    pub fn values(&self) -> &[DataValue] {
        &self.values
    }
    pub fn add_value(&mut self, value: DataValue) {
        self.values.push(value);
    }
}

/// One constraint carried by `DataTypeType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DataTypeConstraint {
    MinInclusive(String),
    MinExclusive(String),
    MaxInclusive(String),
    MaxExclusive(String),
}

/// ISO 23387 `DataTypeType`.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct DataType {
    name: Option<DataTypeName>,
    constraints: Vec<DataTypeConstraint>,
    data_formats: Vec<String>,
    possible_values: Vec<ValueList>,
}
impl DataType {
    #[must_use]
    pub fn new(name: Option<DataTypeName>) -> Self {
        Self {
            name,
            ..Self::default()
        }
    }
    #[must_use]
    pub const fn name(&self) -> Option<&DataTypeName> {
        self.name.as_ref()
    }
    #[must_use]
    pub fn constraints(&self) -> &[DataTypeConstraint] {
        &self.constraints
    }
    #[must_use]
    pub fn data_formats(&self) -> &[String] {
        &self.data_formats
    }
    #[must_use]
    pub fn possible_values(&self) -> &[ValueList] {
        &self.possible_values
    }
    pub fn add_constraint(&mut self, value: DataTypeConstraint) {
        self.constraints.push(value);
    }
    pub fn add_data_format(&mut self, value: impl Into<String>) {
        self.data_formats.push(value.into());
    }
    pub fn add_possible_values(&mut self, value: ValueList) {
        self.possible_values.push(value);
    }
}

/// ISO 23387 `PropertyType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Property {
    concept: Concept,
    data_type: DataType,
    symbols: Vec<String>,
    dimension_refs: Vec<Reference>,
    unit_refs: Vec<Reference>,
    quantity_kind_refs: Vec<Reference>,
    dependency_refs: Vec<Reference>,
    specialization_ref: Option<Reference>,
}
impl Property {
    #[must_use]
    pub const fn new(concept: Concept, data_type: DataType) -> Self {
        Self {
            concept,
            data_type,
            symbols: Vec::new(),
            dimension_refs: Vec::new(),
            unit_refs: Vec::new(),
            quantity_kind_refs: Vec::new(),
            dependency_refs: Vec::new(),
            specialization_ref: None,
        }
    }
    #[must_use]
    pub const fn concept(&self) -> &Concept {
        &self.concept
    }
    /// Mutable access to the shared `ConceptType` core.
    pub fn concept_mut(&mut self) -> &mut Concept {
        &mut self.concept
    }

    #[must_use]
    pub const fn data_type(&self) -> &DataType {
        &self.data_type
    }
    #[must_use]
    pub fn symbols(&self) -> &[String] {
        &self.symbols
    }
    /// `DimensionRef` children; the schema permits any number.
    #[must_use]
    pub fn dimension_refs(&self) -> &[Reference] {
        &self.dimension_refs
    }
    pub fn add_dimension_ref(&mut self, value: Reference) {
        self.dimension_refs.push(value);
    }
    /// The first `DimensionRef`, when present.
    #[deprecated(since = "0.3.0", note = "use `dimension_refs`")]
    #[must_use]
    pub fn dimension_ref(&self) -> Option<&Reference> {
        self.dimension_refs.first()
    }
    /// Replaces every `DimensionRef` with `value` (none when `None`).
    #[deprecated(since = "0.3.0", note = "use `add_dimension_ref`")]
    pub fn set_dimension_ref(&mut self, value: Option<Reference>) {
        self.dimension_refs.clear();
        self.dimension_refs.extend(value);
    }
    #[must_use]
    pub fn unit_refs(&self) -> &[Reference] {
        &self.unit_refs
    }
    #[must_use]
    pub fn quantity_kind_refs(&self) -> &[Reference] {
        &self.quantity_kind_refs
    }
    #[must_use]
    pub fn dependency_refs(&self) -> &[Reference] {
        &self.dependency_refs
    }
    #[must_use]
    pub const fn specialization_ref(&self) -> Option<&Reference> {
        self.specialization_ref.as_ref()
    }
    pub fn add_symbol(&mut self, value: impl Into<String>) {
        self.symbols.push(value.into());
    }
    pub fn add_unit_ref(&mut self, value: Reference) {
        self.unit_refs.push(value);
    }
    pub fn add_quantity_kind_ref(&mut self, value: Reference) {
        self.quantity_kind_refs.push(value);
    }
    pub fn add_dependency_ref(&mut self, value: Reference) {
        self.dependency_refs.push(value);
    }
    pub fn set_specialization_ref(&mut self, value: Option<Reference>) {
        self.specialization_ref = value;
    }
}

/// ISO 23387 `QuantityKindType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct QuantityKind {
    concept: Concept,
    dimension_ref: Reference,
}
impl QuantityKind {
    #[must_use]
    pub const fn new(concept: Concept, dimension_ref: Reference) -> Self {
        Self {
            concept,
            dimension_ref,
        }
    }
    #[must_use]
    pub const fn concept(&self) -> &Concept {
        &self.concept
    }
    /// Mutable access to the shared `ConceptType` core.
    pub fn concept_mut(&mut self) -> &mut Concept {
        &mut self.concept
    }
    #[must_use]
    pub const fn dimension_ref(&self) -> &Reference {
        &self.dimension_ref
    }
}

/// ISO 23387 `GroupOfPropertiesType`; the constructor enforces its non-empty property-reference sequence.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GroupOfProperties {
    subject: Subject,
    property_refs: Vec<Reference>,
}
impl GroupOfProperties {
    #[must_use]
    pub fn new(subject: Subject, first_property_ref: Reference) -> Self {
        Self {
            subject,
            property_refs: vec![first_property_ref],
        }
    }
    #[must_use]
    pub const fn subject(&self) -> &Subject {
        &self.subject
    }
    /// Mutable access to the shared `SubjectType` core.
    pub fn subject_mut(&mut self) -> &mut Subject {
        &mut self.subject
    }
    #[must_use]
    pub fn property_refs(&self) -> &[Reference] {
        &self.property_refs
    }
    pub fn add_property_ref(&mut self, value: Reference) {
        self.property_refs.push(value);
    }
}

/// ISO 23387 `ReferenceDocumentType`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ReferenceDocument {
    concept: Concept,
    languages: Vec<Language>,
    date_of_publication: Option<DateTime>,
    author: Option<String>,
    isbn: Option<String>,
    publisher: Option<String>,
    uri: Option<AnyUri>,
}
impl ReferenceDocument {
    pub fn new(concept: Concept, first_language: Language) -> Self {
        Self {
            concept,
            languages: vec![first_language],
            date_of_publication: None,
            author: None,
            isbn: None,
            publisher: None,
            uri: None,
        }
    }
    #[must_use]
    pub const fn concept(&self) -> &Concept {
        &self.concept
    }
    /// Mutable access to the shared `ConceptType` core.
    pub fn concept_mut(&mut self) -> &mut Concept {
        &mut self.concept
    }
    #[must_use]
    pub fn languages(&self) -> &[Language] {
        &self.languages
    }
    pub fn add_language(&mut self, value: Language) {
        self.languages.push(value);
    }
    #[must_use]
    pub const fn date_of_publication(&self) -> Option<&DateTime> {
        self.date_of_publication.as_ref()
    }
    pub fn set_date_of_publication(&mut self, value: Option<DateTime>) {
        self.date_of_publication = value;
    }
    #[must_use]
    pub fn author(&self) -> Option<&str> {
        self.author.as_deref()
    }
    pub fn set_author(&mut self, value: Option<String>) {
        self.author = value;
    }
    #[must_use]
    pub fn isbn(&self) -> Option<&str> {
        self.isbn.as_deref()
    }
    pub fn set_isbn(&mut self, value: Option<String>) {
        self.isbn = value;
    }
    #[must_use]
    pub fn publisher(&self) -> Option<&str> {
        self.publisher.as_deref()
    }
    pub fn set_publisher(&mut self, value: Option<String>) {
        self.publisher = value;
    }
    #[must_use]
    pub fn uri(&self) -> Option<&str> {
        self.uri.as_ref().map(AnyUri::as_str)
    }
    pub fn set_uri(&mut self, value: Option<AnyUri>) {
        self.uri = value;
    }
}

/// Seven SI base-dimension exponents in Annex E declaration order.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dimension {
    concept: Concept,
    exponents: [Decimal; 7],
}
impl Dimension {
    #[must_use]
    pub const fn new(concept: Concept, exponents: [Decimal; 7]) -> Self {
        Self { concept, exponents }
    }
    #[must_use]
    pub const fn concept(&self) -> &Concept {
        &self.concept
    }
    /// Mutable access to the shared `ConceptType` core.
    pub fn concept_mut(&mut self) -> &mut Concept {
        &mut self.concept
    }
    #[must_use]
    pub const fn exponents(&self) -> &[Decimal; 7] {
        &self.exponents
    }
}

/// ISO 23387 `UnitType`, with all required scalar/reference fields present.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Unit {
    concept: Concept,
    symbols: Vec<MultiLanguageText>,
    dimension_ref: Reference,
    scale: Scale,
    base: Base,
    coefficient: Rational,
    offset: Rational,
}
impl Unit {
    #[must_use]
    pub const fn new(
        concept: Concept,
        dimension_ref: Reference,
        scale: Scale,
        base: Base,
        coefficient: Rational,
        offset: Rational,
    ) -> Self {
        Self {
            concept,
            symbols: Vec::new(),
            dimension_ref,
            scale,
            base,
            coefficient,
            offset,
        }
    }
    #[must_use]
    pub const fn concept(&self) -> &Concept {
        &self.concept
    }
    /// Mutable access to the shared `ConceptType` core.
    pub fn concept_mut(&mut self) -> &mut Concept {
        &mut self.concept
    }
    #[must_use]
    pub fn symbols(&self) -> &[MultiLanguageText] {
        &self.symbols
    }
    pub fn add_symbol(&mut self, value: MultiLanguageText) {
        self.symbols.push(value);
    }
    #[must_use]
    pub const fn dimension_ref(&self) -> &Reference {
        &self.dimension_ref
    }
    #[must_use]
    pub const fn scale(&self) -> &Scale {
        &self.scale
    }
    #[must_use]
    pub const fn base(&self) -> &Base {
        &self.base
    }
    #[must_use]
    pub const fn coefficient(&self) -> &Rational {
        &self.coefficient
    }
    #[must_use]
    pub const fn offset(&self) -> &Rational {
        &self.offset
    }
}

/// ISO 23387 `DataTemplateType`.
///
/// The schema requires at least one of `HasObjectTypeRef`, `HasPropertyRef`
/// or `HasGroupOfPropertiesRef`, on top of the `SubjectType` requirement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataTemplate {
    subject: Subject,
    object_type_refs: Vec<Reference>,
    property_refs: Vec<Reference>,
    group_of_properties_refs: Vec<Reference>,
}
impl DataTemplate {
    #[must_use]
    pub const fn new(subject: Subject) -> Self {
        Self {
            subject,
            object_type_refs: Vec::new(),
            property_refs: Vec::new(),
            group_of_properties_refs: Vec::new(),
        }
    }
    #[must_use]
    pub const fn subject(&self) -> &Subject {
        &self.subject
    }
    /// Mutable access to the shared `SubjectType` core.
    pub fn subject_mut(&mut self) -> &mut Subject {
        &mut self.subject
    }
    /// `HasObjectTypeRef` children; the schema permits any number.
    #[must_use]
    pub fn object_type_refs(&self) -> &[Reference] {
        &self.object_type_refs
    }
    pub fn add_object_type_ref(&mut self, value: Reference) {
        self.object_type_refs.push(value);
    }
    /// The first `HasObjectTypeRef`, when present.
    #[deprecated(since = "0.3.0", note = "use `object_type_refs`")]
    #[must_use]
    pub fn object_type_ref(&self) -> Option<&Reference> {
        self.object_type_refs.first()
    }
    #[must_use]
    pub fn property_refs(&self) -> &[Reference] {
        &self.property_refs
    }
    pub fn add_property_ref(&mut self, value: Reference) {
        self.property_refs.push(value);
    }
    #[must_use]
    pub fn group_of_properties_refs(&self) -> &[Reference] {
        &self.group_of_properties_refs
    }
    pub fn add_group_of_properties_ref(&mut self, value: Reference) {
        self.group_of_properties_refs.push(value);
    }
    /// Replaces every `HasObjectTypeRef` with `value` (none when `None`).
    #[deprecated(since = "0.3.0", note = "use `add_object_type_ref`")]
    pub fn set_object_type_ref(&mut self, value: Option<Reference>) {
        self.object_type_refs.clear();
        self.object_type_refs.extend(value);
    }
}