zerodds-types 1.0.0-rc.1

OMG XTypes 1.3 type system: TypeIdentifier + TypeObject (Minimal/Complete) + Assignability + DynamicType + TypeLookup. Pure-Rust no_std + alloc.
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! TypeDescriptor + MemberDescriptor (XTypes 1.3 §7.5.1, §7.5.2).
//!
//! Ein `TypeDescriptor` beschreibt einen DynamicType vollstaendig: kind +
//! Name + Bound + Element-Type usw. Er ist der **konstruktive** Eingang
//! zu `DynamicTypeBuilderFactory::create_type` (Spec §7.5.5.1).
//!
//! Ein `MemberDescriptor` beschreibt einen Member innerhalb eines
//! Composite-Types (Struct/Union/Annotation). Spec §7.5.2 listet alle
//! Felder, die hier 1:1 abgebildet sind. Apply-Logik fuer
//! `try_construct` (DISCARD/USE_DEFAULT/TRIM) wird in C4.7 ergaenzt.

use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;

/// XTypes 1.3 TypeKind-Enum (§7.5.1 Table 10).
///
/// Deckt die 24 in der Spec genannten Kinds. `NoType` entspricht
/// `TK_NONE`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TypeKind {
    /// Kein Typ — Sentinel-Wert.
    NoType,
    /// `boolean`.
    Boolean,
    /// `octet` / `byte` (8-bit unsigned).
    Byte,
    /// `int8`.
    Int8,
    /// `uint8`.
    UInt8,
    /// `int16`.
    Int16,
    /// `uint16`.
    UInt16,
    /// `int32`.
    Int32,
    /// `uint32`.
    UInt32,
    /// `int64`.
    Int64,
    /// `uint64`.
    UInt64,
    /// `float32`.
    Float32,
    /// `float64`.
    Float64,
    /// `float128` (long double).
    Float128,
    /// `char` (8-bit).
    Char8,
    /// `wchar` (16-bit).
    Char16,
    /// `string<N>`.
    String8,
    /// `wstring<N>`.
    String16,
    /// Enumeration.
    Enumeration,
    /// Bitmask.
    Bitmask,
    /// Alias / typedef.
    Alias,
    /// Array `T[D1,D2,...]`.
    Array,
    /// `sequence<T,N>`.
    Sequence,
    /// `map<K,V,N>`.
    Map,
    /// `struct`.
    Structure,
    /// `union`.
    Union,
    /// `bitset`.
    Bitset,
    /// `annotation`.
    Annotation,
}

impl TypeKind {
    /// `true` wenn der Kind ein primitiver, atomarer Typ ist (kein
    /// Composite, keine Collection). Spec §7.5.1.
    #[must_use]
    pub const fn is_primitive(self) -> bool {
        matches!(
            self,
            Self::Boolean
                | Self::Byte
                | Self::Int8
                | Self::UInt8
                | Self::Int16
                | Self::UInt16
                | Self::Int32
                | Self::UInt32
                | Self::Int64
                | Self::UInt64
                | Self::Float32
                | Self::Float64
                | Self::Float128
                | Self::Char8
                | Self::Char16
        )
    }

    /// `true` wenn dieser Kind Members tragen kann (Struct/Union/
    /// Annotation/Bitset/Bitmask/Enum).
    #[must_use]
    pub const fn is_aggregable(self) -> bool {
        matches!(
            self,
            Self::Structure
                | Self::Union
                | Self::Annotation
                | Self::Bitset
                | Self::Bitmask
                | Self::Enumeration
        )
    }
}

/// Extensibility-Kind (§7.2.2.4).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExtensibilityKind {
    /// `@final` — Typ ist abgeschlossen.
    Final,
    /// `@appendable` — neue Felder am Ende erlaubt (Default).
    Appendable,
    /// `@mutable` — beliebige Evolution mit `@id`-Bindings.
    Mutable,
}

impl Default for ExtensibilityKind {
    fn default() -> Self {
        Self::Appendable
    }
}

/// Try-Construct-Strategie (Spec §7.5.2 + §7.6.4).
///
/// Apply-Semantik (was passiert bei Decoder-Fehlschlag) wird in C4.7
/// implementiert — hier ist nur das Enum + Member-Feld.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TryConstructKind {
    /// Werfe das Sample weg.
    Discard,
    /// Setze auf den Default-Wert.
    UseDefault,
    /// Truncate auf Bound (Strings/Sequences).
    Trim,
}

impl Default for TryConstructKind {
    fn default() -> Self {
        Self::Discard
    }
}

/// `MemberId` — konsistent mit XTypes 1.3 §7.3.1.1 (32 bits).
pub type MemberId = u32;

/// XTypes §7.5.1.2 TypeDescriptor.
///
/// Beschreibt einen DynamicType — zur Konstruktion via
/// [`crate::dynamic::DynamicTypeBuilderFactory::create_type`] oder als
/// Read-only-Sicht via [`crate::dynamic::DynamicType::descriptor`].
///
/// Felder, die fuer einen gegebenen Kind irrelevant sind, koennen leer
/// bleiben (z.B. `bound` fuer Struct = `Vec::new()`).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TypeDescriptor {
    /// TypeKind.
    pub kind: TypeKind,
    /// Voll qualifizierter Name, z.B. `"::sensors::Chatter"`.
    pub name: String,
    /// Base-Type fuer Inheritance (Struct/Union).
    pub base_type: Option<Box<TypeDescriptor>>,
    /// Discriminator-Type fuer `kind == Union` (Pflicht).
    pub discriminator_type: Option<Box<TypeDescriptor>>,
    /// Bound — Array-Dimensionen, oder `[max]` fuer Sequence/String/Map.
    /// Leer fuer Composite/Primitive.
    pub bound: Vec<u32>,
    /// Element-Type fuer Array/Sequence/Map.
    pub element_type: Option<Box<TypeDescriptor>>,
    /// Key-Type fuer Map.
    pub key_element_type: Option<Box<TypeDescriptor>>,
    /// Extensibility-Kind (relevant fuer Struct/Union).
    pub extensibility_kind: ExtensibilityKind,
    /// `@nested` — Type ist nicht als Top-Level-Topic gedacht.
    pub is_nested: bool,
}

impl TypeDescriptor {
    /// Konstruiert einen primitiven Descriptor.
    #[must_use]
    pub fn primitive(kind: TypeKind, name: impl Into<String>) -> Self {
        Self {
            kind,
            name: name.into(),
            base_type: None,
            discriminator_type: None,
            bound: Vec::new(),
            element_type: None,
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen Struct-Descriptor.
    #[must_use]
    pub fn structure(name: impl Into<String>) -> Self {
        Self {
            kind: TypeKind::Structure,
            name: name.into(),
            base_type: None,
            discriminator_type: None,
            bound: Vec::new(),
            element_type: None,
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen Union-Descriptor.
    #[must_use]
    pub fn union(name: impl Into<String>, discriminator: TypeDescriptor) -> Self {
        Self {
            kind: TypeKind::Union,
            name: name.into(),
            base_type: None,
            discriminator_type: Some(Box::new(discriminator)),
            bound: Vec::new(),
            element_type: None,
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen Sequence-Descriptor.
    #[must_use]
    pub fn sequence(name: impl Into<String>, element: TypeDescriptor, max: u32) -> Self {
        Self {
            kind: TypeKind::Sequence,
            name: name.into(),
            base_type: None,
            discriminator_type: None,
            bound: alloc::vec![max],
            element_type: Some(Box::new(element)),
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen Array-Descriptor.
    #[must_use]
    pub fn array(name: impl Into<String>, element: TypeDescriptor, dims: Vec<u32>) -> Self {
        Self {
            kind: TypeKind::Array,
            name: name.into(),
            base_type: None,
            discriminator_type: None,
            bound: dims,
            element_type: Some(Box::new(element)),
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen Map-Descriptor.
    #[must_use]
    pub fn map(
        name: impl Into<String>,
        key: TypeDescriptor,
        element: TypeDescriptor,
        max: u32,
    ) -> Self {
        Self {
            kind: TypeKind::Map,
            name: name.into(),
            base_type: None,
            discriminator_type: None,
            bound: alloc::vec![max],
            element_type: Some(Box::new(element)),
            key_element_type: Some(Box::new(key)),
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen String-Descriptor (`string<bound>`).
    #[must_use]
    pub fn string8(bound: u32) -> Self {
        Self {
            kind: TypeKind::String8,
            name: alloc::format!("string<{bound}>"),
            base_type: None,
            discriminator_type: None,
            bound: alloc::vec![bound],
            element_type: None,
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen WString-Descriptor.
    #[must_use]
    pub fn string16(bound: u32) -> Self {
        Self {
            kind: TypeKind::String16,
            name: alloc::format!("wstring<{bound}>"),
            base_type: None,
            discriminator_type: None,
            bound: alloc::vec![bound],
            element_type: None,
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Konstruiert einen Enum-Descriptor.
    #[must_use]
    pub fn enumeration(name: impl Into<String>) -> Self {
        Self {
            kind: TypeKind::Enumeration,
            name: name.into(),
            base_type: None,
            discriminator_type: None,
            bound: Vec::new(),
            element_type: None,
            key_element_type: None,
            extensibility_kind: ExtensibilityKind::default(),
            is_nested: false,
        }
    }

    /// Validierung — Spec §7.5.1.4 `is_consistent()`.
    ///
    /// Prueft die in der Spec festgelegten Constraints fuer ein
    /// Descriptor-Object: Discriminator-Pflicht bei Union, Bound-Pflicht
    /// bei Array/Sequence/String/Map etc.
    ///
    /// # Errors
    /// `String` mit menschen-lesbarer Fehlerbeschreibung.
    pub fn is_consistent(&self) -> Result<(), String> {
        // Cycle-Check: ein Descriptor darf sich nicht selbst als
        // base_type oder element_type referenzieren (durch struktur-
        // gleichheit erkannt — robusten Cycle-Check macht der Builder).
        if self.name.is_empty() && self.kind != TypeKind::NoType {
            return Err(String::from("descriptor without name"));
        }
        match self.kind {
            TypeKind::Union => {
                let Some(d) = &self.discriminator_type else {
                    return Err(String::from("union without discriminator_type"));
                };
                if !is_valid_discriminator(d.kind) {
                    return Err(alloc::format!(
                        "union discriminator must be int/enum/bool, got {:?}",
                        d.kind
                    ));
                }
            }
            TypeKind::Array => {
                if self.bound.is_empty() {
                    return Err(String::from("array without dimensions"));
                }
                if self.bound.contains(&0) {
                    return Err(String::from("array dimension must be > 0"));
                }
                if self.element_type.is_none() {
                    return Err(String::from("array without element_type"));
                }
            }
            TypeKind::Sequence | TypeKind::String8 | TypeKind::String16 => {
                if self.bound.len() != 1 {
                    return Err(String::from("sequence/string needs exactly 1 bound"));
                }
                if matches!(self.kind, TypeKind::Sequence) && self.element_type.is_none() {
                    return Err(String::from("sequence without element_type"));
                }
            }
            TypeKind::Map => {
                if self.bound.len() != 1 {
                    return Err(String::from("map needs exactly 1 bound"));
                }
                if self.element_type.is_none() {
                    return Err(String::from("map without value element_type"));
                }
                if self.key_element_type.is_none() {
                    return Err(String::from("map without key_element_type"));
                }
            }
            _ => {}
        }
        // Inheritance-Cycle-Check (1 Ebene; tiefere Ebenen werden im
        // Builder via `build()` gegen die finale DynamicType gecheckt).
        if let Some(b) = &self.base_type {
            if b.name == self.name && !self.name.is_empty() {
                return Err(String::from("inheritance cycle: base_type == self"));
            }
        }
        Ok(())
    }
}

/// XTypes §7.5.2.2 MemberDescriptor.
///
/// Beschreibt einen Member innerhalb eines Composite-Types (Struct,
/// Union, Annotation, Bitset, Bitmask). Fuer Bitmask repraesentiert
/// `member_type` typisch `Boolean`, `id` ist die Bit-Position.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemberDescriptor {
    /// Member-Name (case-sensitive, eindeutig im Composite).
    pub name: String,
    /// Member-Id (eindeutig im Composite, fuer XCDR2).
    pub id: MemberId,
    /// Type des Members.
    pub member_type: Box<TypeDescriptor>,
    /// Default-Wert in canonical IDL-Literal-Form.
    pub default_value: Option<String>,
    /// Reihenfolgen-Index (0-basiert) — fuer `member_by_index`.
    pub index: u32,
    /// Union-Case-Labels. Spec §7.5.2 — nur fuer Union belegt.
    pub label: Vec<i64>,
    /// Try-Construct-Strategie (Apply in C4.7).
    pub try_construct: TryConstructKind,
    /// `@key` — Member ist Teil des Topic-Keys.
    pub is_key: bool,
    /// `@optional`.
    pub is_optional: bool,
    /// `@must_understand`.
    pub is_must_understand: bool,
    /// `@external` — indirect storage (shared_ptr in C++).
    pub is_shared: bool,
    /// `default:` Branch fuer Union.
    pub is_default_label: bool,
}

impl MemberDescriptor {
    /// Erstellt einen MemberDescriptor mit den verbreitetsten Defaults
    /// fuer Struct-Members.
    #[must_use]
    pub fn new(name: impl Into<String>, id: MemberId, ty: TypeDescriptor) -> Self {
        Self {
            name: name.into(),
            id,
            member_type: Box::new(ty),
            default_value: None,
            index: 0,
            label: Vec::new(),
            try_construct: TryConstructKind::default(),
            is_key: false,
            is_optional: false,
            is_must_understand: false,
            is_shared: false,
            is_default_label: false,
        }
    }

    /// Validierung — Spec §7.5.2.4 `is_consistent()`.
    ///
    /// # Errors
    /// `String` mit Fehlertext.
    pub fn is_consistent(&self) -> Result<(), String> {
        if self.name.is_empty() {
            return Err(String::from("member without name"));
        }
        self.member_type.is_consistent()?;
        if self.is_default_label && !self.label.is_empty() {
            return Err(String::from(
                "member with is_default_label must not have explicit labels",
            ));
        }
        Ok(())
    }
}

/// True, wenn der TypeKind ein gueltiger Union-Discriminator ist
/// (Spec §7.4.1.4.4: integral oder enum oder boolean oder char).
const fn is_valid_discriminator(kind: TypeKind) -> bool {
    matches!(
        kind,
        TypeKind::Boolean
            | TypeKind::Byte
            | TypeKind::Int8
            | TypeKind::UInt8
            | TypeKind::Int16
            | TypeKind::UInt16
            | TypeKind::Int32
            | TypeKind::UInt32
            | TypeKind::Int64
            | TypeKind::UInt64
            | TypeKind::Char8
            | TypeKind::Char16
            | TypeKind::Enumeration
    )
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;

    #[test]
    fn type_kind_primitive_set_matches_spec_table_10() {
        for k in [
            TypeKind::Boolean,
            TypeKind::Byte,
            TypeKind::Int8,
            TypeKind::UInt8,
            TypeKind::Int16,
            TypeKind::UInt16,
            TypeKind::Int32,
            TypeKind::UInt32,
            TypeKind::Int64,
            TypeKind::UInt64,
            TypeKind::Float32,
            TypeKind::Float64,
            TypeKind::Float128,
            TypeKind::Char8,
            TypeKind::Char16,
        ] {
            assert!(k.is_primitive(), "{k:?} should be primitive");
        }
        for k in [
            TypeKind::Structure,
            TypeKind::Union,
            TypeKind::Sequence,
            TypeKind::Array,
            TypeKind::Map,
            TypeKind::String8,
            TypeKind::String16,
            TypeKind::Alias,
        ] {
            assert!(!k.is_primitive(), "{k:?} should not be primitive");
        }
    }

    #[test]
    fn type_kind_aggregable_set() {
        assert!(TypeKind::Structure.is_aggregable());
        assert!(TypeKind::Union.is_aggregable());
        assert!(TypeKind::Annotation.is_aggregable());
        assert!(TypeKind::Bitset.is_aggregable());
        assert!(TypeKind::Bitmask.is_aggregable());
        assert!(TypeKind::Enumeration.is_aggregable());
        assert!(!TypeKind::Int32.is_aggregable());
        assert!(!TypeKind::Sequence.is_aggregable());
    }

    #[test]
    fn descriptor_struct_passes_consistency() {
        let s = TypeDescriptor::structure("::Foo");
        assert!(s.is_consistent().is_ok());
    }

    #[test]
    fn descriptor_union_without_discriminator_fails() {
        let mut u = TypeDescriptor::structure("::U");
        u.kind = TypeKind::Union;
        let err = u.is_consistent().unwrap_err();
        assert!(err.contains("discriminator"));
    }

    #[test]
    fn descriptor_union_with_invalid_discriminator_fails() {
        let bad_disc = TypeDescriptor::structure("::S");
        let u = TypeDescriptor::union("::U", bad_disc);
        let err = u.is_consistent().unwrap_err();
        assert!(err.contains("discriminator"));
    }

    #[test]
    fn descriptor_array_without_dims_fails() {
        let mut a = TypeDescriptor::array(
            "::A",
            TypeDescriptor::primitive(TypeKind::Int32, "int32"),
            alloc::vec![3, 3],
        );
        a.bound.clear();
        let err = a.is_consistent().unwrap_err();
        assert!(err.contains("dimensions"));
    }

    #[test]
    fn descriptor_array_with_zero_dim_fails() {
        let a = TypeDescriptor::array(
            "::A",
            TypeDescriptor::primitive(TypeKind::Int32, "int32"),
            alloc::vec![3, 0, 4],
        );
        let err = a.is_consistent().unwrap_err();
        assert!(err.contains("> 0"));
    }

    #[test]
    fn descriptor_sequence_with_element_passes() {
        let s = TypeDescriptor::sequence(
            "::S",
            TypeDescriptor::primitive(TypeKind::Int32, "int32"),
            100,
        );
        assert!(s.is_consistent().is_ok());
    }

    #[test]
    fn descriptor_map_requires_both_key_and_value() {
        let mut m = TypeDescriptor::map(
            "::M",
            TypeDescriptor::string8(64),
            TypeDescriptor::primitive(TypeKind::Int64, "int64"),
            500,
        );
        assert!(m.is_consistent().is_ok());
        m.key_element_type = None;
        assert!(m.is_consistent().is_err());
    }

    #[test]
    fn descriptor_inheritance_cycle_self_reference_rejected() {
        let mut s = TypeDescriptor::structure("::Foo");
        let cycle = TypeDescriptor::structure("::Foo");
        s.base_type = Some(Box::new(cycle));
        let err = s.is_consistent().unwrap_err();
        assert!(err.contains("cycle"));
    }

    #[test]
    fn member_descriptor_default_label_with_labels_rejected() {
        let mut m =
            MemberDescriptor::new("x", 1, TypeDescriptor::primitive(TypeKind::Int32, "int32"));
        m.is_default_label = true;
        m.label = alloc::vec![0];
        let err = m.is_consistent().unwrap_err();
        assert!(err.contains("default_label"));
    }

    #[test]
    fn member_descriptor_empty_name_rejected() {
        let m = MemberDescriptor::new("", 1, TypeDescriptor::primitive(TypeKind::Int32, "int32"));
        assert!(m.is_consistent().is_err());
    }

    #[test]
    fn try_construct_default_is_discard() {
        assert_eq!(TryConstructKind::default(), TryConstructKind::Discard);
    }

    #[test]
    fn extensibility_default_is_appendable() {
        assert_eq!(ExtensibilityKind::default(), ExtensibilityKind::Appendable);
    }
}