Skip to main content

xsd_schema/
ids.rs

1//! Typed IDs for arena-based storage
2//!
3//! All schema components are stored in arenas and referenced by typed IDs.
4//! This approach avoids reference cycles and provides type safety.
5//!
6//! Uses slotmap for type-safe arena keys with generation tracking.
7
8use slotmap::new_key_type;
9use std::fmt;
10
11/// Document ID for source map indexing
12pub type DocumentId = u32;
13
14/// Interned string identifier for names and namespace URIs
15///
16/// See XML_NAME_TABLE.md for NameTable design.
17/// NameId(0) is reserved for empty string.
18///
19/// Note: NameId is NOT a slotmap key - it's a simple index into the NameTable
20/// which uses a custom chained hash table for string interning.
21#[derive(Clone, Copy, PartialEq, Eq, Hash)]
22pub struct NameId(pub u32);
23
24impl fmt::Debug for NameId {
25    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26        write!(f, "NameId({})", self.0)
27    }
28}
29
30impl fmt::Display for NameId {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "${}", self.0)
33    }
34}
35
36// Define typed keys for slotmap arenas
37// Each key type is unique and cannot be used with other SlotMaps
38
39new_key_type! {
40    /// Simple type definition key
41    pub struct SimpleTypeKey;
42
43    /// Complex type definition key
44    pub struct ComplexTypeKey;
45
46    /// Element declaration key
47    pub struct ElementKey;
48
49    /// Attribute declaration key
50    pub struct AttributeKey;
51
52    /// Attribute group key
53    pub struct AttributeGroupKey;
54
55    /// Model group key (named groups like <xs:group name="...">)
56    pub struct ModelGroupKey;
57
58    /// Notation declaration key
59    pub struct NotationKey;
60
61    /// Identity constraint key (key, unique, keyref)
62    pub struct IdentityConstraintKey;
63}
64
65/// Type definition reference (simple or complex)
66#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
67pub enum TypeKey {
68    Simple(SimpleTypeKey),
69    Complex(ComplexTypeKey),
70}
71
72impl TypeKey {
73    pub fn as_simple(&self) -> Option<SimpleTypeKey> {
74        match self {
75            TypeKey::Simple(key) => Some(*key),
76            _ => None,
77        }
78    }
79
80    pub fn as_complex(&self) -> Option<ComplexTypeKey> {
81        match self {
82            TypeKey::Complex(key) => Some(*key),
83            _ => None,
84        }
85    }
86}
87
88impl From<SimpleTypeKey> for TypeKey {
89    fn from(key: SimpleTypeKey) -> Self {
90        TypeKey::Simple(key)
91    }
92}
93
94impl From<ComplexTypeKey> for TypeKey {
95    fn from(key: ComplexTypeKey) -> Self {
96        TypeKey::Complex(key)
97    }
98}