xsd-schema 0.1.0

XML Schema (XSD 1.0/1.1) validator with PSVI and a built-in XPath 2.0 engine
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
use crate::navigator::DomNodeType;

// ── Page-addressing constants ──────────────────────────────────────────

/// Page size exponent: 2^12 = 4096 nodes per page.
pub const PAGE_SHIFT: u32 = 12;

/// Nodes per page (4096). Each page = 4096 × 16 bytes = 64 KB.
pub const PAGE_SIZE: u32 = 1 << PAGE_SHIFT;

/// Bitmask for extracting the slot within a page.
pub const PAGE_MASK: u32 = PAGE_SIZE - 1; // 0xFFF

/// NULL sentinel — never a valid node index.
pub const NULL: u32 = u32::MAX;

// ── Page-addressing helpers ────────────────────────────────────────────

/// Returns the page number for a flat node index.
#[inline]
pub fn page_of(node_ref: u32) -> u32 {
    node_ref >> PAGE_SHIFT
}

/// Returns the slot (offset within page) for a flat node index.
#[inline]
pub fn slot_of(node_ref: u32) -> u32 {
    node_ref & PAGE_MASK
}

/// Constructs a flat node index from a page number and slot.
///
/// # Panics (debug only)
///
/// Panics if `slot >= PAGE_SIZE`, which would alias bits into the page portion.
#[inline]
pub fn node_ref_from(page: u32, slot: u32) -> u32 {
    debug_assert!(slot < PAGE_SIZE, "slot {slot} >= PAGE_SIZE ({PAGE_SIZE})");
    (page << PAGE_SHIFT) | slot
}

// ── NodeType ───────────────────────────────────────────────────────────

/// Discriminant stored in the low 4 bits of [`Node::props_type`].
#[repr(u8)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum NodeType {
    /// Sentinel: marks end of document.
    #[default]
    Nul = 0,
    /// Document root.
    Root = 1,
    /// Element node (value = QNameAtom index).
    Element = 2,
    /// Attribute name node of a two-node pair (value = QNameAtom index).
    Attribute = 3,
    /// String content of a two-node pair (attribute value or PI data).
    ChildValue = 4,
    /// Text node (value = string index).
    Text = 5,
    /// Whitespace-only text node.
    Whitespace = 6,
    /// Schema-significant whitespace.
    SignificantWhitespace = 7,
    /// Comment node (value = string index).
    Comment = 8,
    /// Processing instruction target (value = string index); data in next ChildValue.
    ProcessingInstruction = 9,
}

impl From<NodeType> for DomNodeType {
    fn from(nt: NodeType) -> Self {
        match nt {
            NodeType::Root => DomNodeType::Root,
            NodeType::Element => DomNodeType::Element,
            NodeType::Attribute => DomNodeType::Attribute,
            NodeType::Text => DomNodeType::Text,
            NodeType::Whitespace => DomNodeType::Whitespace,
            NodeType::SignificantWhitespace => DomNodeType::SignificantWhitespace,
            NodeType::Comment => DomNodeType::Comment,
            NodeType::ProcessingInstruction => DomNodeType::ProcessingInstruction,
            NodeType::Nul | NodeType::ChildValue => {
                unreachable!("Nul and ChildValue have no DomNodeType equivalent")
            }
        }
    }
}

impl NodeType {
    /// Decode from the low 4 bits of a `props_type` value.
    #[inline]
    fn from_bits(bits: u32) -> Self {
        match bits & Node::NODE_TYPE_MASK {
            0 => NodeType::Nul,
            1 => NodeType::Root,
            2 => NodeType::Element,
            3 => NodeType::Attribute,
            4 => NodeType::ChildValue,
            5 => NodeType::Text,
            6 => NodeType::Whitespace,
            7 => NodeType::SignificantWhitespace,
            8 => NodeType::Comment,
            9 => NodeType::ProcessingInstruction,
            _ => NodeType::Nul, // reserved values → Nul
        }
    }
}

// ── Node ───────────────────────────────────────────────────────────────

/// 16-byte flat node in the `BufferDocument` node array.
///
/// Layout of `props_type` (32 bits):
/// - Bits \[3:0\]   — [`NodeType`] discriminant (4 bits)
/// - Bits \[7:4\]   — property flags (`HAS_ATTRIBUTE`, `HAS_CHILDREN`, `IS_COMPLEX_TYPE`, `HAS_NMSP_DECLS`)
/// - Bit  \[8\]     — `IS_NIL` flag (xsi:nil="true")
/// - Bits \[11:9\]  — reserved (must be 0)
/// - Bits \[31:12\] — 20-bit binding index into `BindingRemapTable` (0 = unbound)
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Node {
    /// Index of the next sibling node, or [`NULL`].
    pub next_sibling: u32,
    /// Index of the parent node, or [`NULL`].
    pub parent: u32,
    /// Packed field: node type (4 bits) | flags (4 bits) | type_index (24 bits).
    pub props_type: u32,
    /// Interpretation depends on node type (QNameAtom index, string index, etc.).
    pub value: u32,
}

impl Node {
    // ── Bitmask constants ──────────────────────────────────────────────

    const NODE_TYPE_MASK: u32 = 0x0F; // bits [3:0]
    const BINDING_INDEX_SHIFT: u32 = 12; // bits [31:12]

    /// Element has attribute children.
    pub const HAS_ATTRIBUTE: u32 = 0x10;
    /// Element/Root has content children.
    pub const HAS_CHILDREN: u32 = 0x20;
    /// `binding_index` references a complex type in the remap table.
    pub const IS_COMPLEX_TYPE: u32 = 0x40;
    /// Element declares namespace bindings.
    pub const HAS_NMSP_DECLS: u32 = 0x80;
    /// Element has xsi:nil="true".
    pub const IS_NIL: u32 = 0x100; // bit [8]

    // ── Accessors ──────────────────────────────────────────────────────

    /// Returns the [`NodeType`] stored in the low 4 bits.
    #[inline]
    pub fn node_type(self) -> NodeType {
        NodeType::from_bits(self.props_type)
    }

    /// Returns the raw flag nibble (bits \[7:4\]).
    #[inline]
    pub fn flags(self) -> u32 {
        self.props_type & 0xF0
    }

    /// Returns the 20-bit binding index (bits \[31:12\]).
    #[inline]
    pub fn binding_index(self) -> u32 {
        self.props_type >> Self::BINDING_INDEX_SHIFT
    }

    /// Overwrites the [`NodeType`] in bits \[3:0\], preserving other fields.
    #[inline]
    pub fn set_node_type(&mut self, nt: NodeType) {
        self.props_type = (self.props_type & !Self::NODE_TYPE_MASK) | (nt as u32);
    }

    /// Sets a flag bit (e.g. [`HAS_ATTRIBUTE`](Self::HAS_ATTRIBUTE)).
    #[inline]
    pub fn set_flag(&mut self, flag: u32) {
        self.props_type |= flag;
    }

    /// Clears a flag bit.
    #[inline]
    pub fn clear_flag(&mut self, flag: u32) {
        self.props_type &= !flag;
    }

    /// Tests whether a flag bit is set.
    #[inline]
    pub fn has_flag(self, flag: u32) -> bool {
        self.props_type & flag != 0
    }

    /// Sets the 20-bit binding index in bits \[31:12\].
    ///
    /// Preserves bits \[11:0\] (node type, flags, and IS_NIL).
    ///
    /// # Panics (debug only)
    ///
    /// Panics if `idx` exceeds 20 bits (>= 0x10_0000).
    #[inline]
    pub fn set_binding_index(&mut self, idx: u32) {
        debug_assert!(idx < (1 << 20), "binding_index {idx} exceeds 20-bit range");
        self.props_type = (self.props_type & 0xFFF) | (idx << Self::BINDING_INDEX_SHIFT);
    }
}

// ── Tests ──────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use std::mem;

    #[test]
    fn node_size_is_16_bytes() {
        assert_eq!(mem::size_of::<Node>(), 16);
    }

    #[test]
    fn node_is_copy_clone_default() {
        let a = Node::default();
        let b = a; // Copy
        #[allow(clippy::clone_on_copy)]
        let c = a.clone(); // Clone — intentionally testing the Clone impl
        assert_eq!(a, b);
        assert_eq!(a, c);
        // Default zeroes
        assert_eq!(a.next_sibling, 0);
        assert_eq!(a.parent, 0);
        assert_eq!(a.props_type, 0);
        assert_eq!(a.value, 0);
    }

    #[test]
    fn node_type_round_trip() {
        let variants = [
            NodeType::Nul,
            NodeType::Root,
            NodeType::Element,
            NodeType::Attribute,
            NodeType::ChildValue,
            NodeType::Text,
            NodeType::Whitespace,
            NodeType::SignificantWhitespace,
            NodeType::Comment,
            NodeType::ProcessingInstruction,
        ];
        for nt in variants {
            let mut node = Node::default();
            node.set_node_type(nt);
            assert_eq!(node.node_type(), nt, "round-trip failed for {nt:?}");
        }
    }

    #[test]
    fn node_type_preserves_other_bits() {
        let mut node = Node::default();
        node.set_flag(Node::HAS_CHILDREN);
        node.set_binding_index(42);
        node.set_node_type(NodeType::Element);
        assert_eq!(node.node_type(), NodeType::Element);
        assert!(node.has_flag(Node::HAS_CHILDREN));
        assert_eq!(node.binding_index(), 42);
    }

    #[test]
    fn flags_set_clear_has() {
        let mut node = Node::default();
        assert!(!node.has_flag(Node::HAS_ATTRIBUTE));
        assert!(!node.has_flag(Node::HAS_CHILDREN));
        assert!(!node.has_flag(Node::IS_COMPLEX_TYPE));
        assert!(!node.has_flag(Node::HAS_NMSP_DECLS));

        node.set_flag(Node::HAS_ATTRIBUTE);
        assert!(node.has_flag(Node::HAS_ATTRIBUTE));

        node.set_flag(Node::HAS_CHILDREN);
        assert!(node.has_flag(Node::HAS_CHILDREN));
        assert!(node.has_flag(Node::HAS_ATTRIBUTE)); // still set

        node.clear_flag(Node::HAS_ATTRIBUTE);
        assert!(!node.has_flag(Node::HAS_ATTRIBUTE));
        assert!(node.has_flag(Node::HAS_CHILDREN)); // unchanged
    }

    #[test]
    fn flags_nibble() {
        let mut node = Node::default();
        node.set_flag(Node::HAS_ATTRIBUTE | Node::HAS_NMSP_DECLS);
        assert_eq!(node.flags(), Node::HAS_ATTRIBUTE | Node::HAS_NMSP_DECLS);
    }

    #[test]
    fn binding_index_set_get() {
        let mut node = Node::default();
        node.set_node_type(NodeType::Element);
        node.set_flag(Node::HAS_CHILDREN);

        node.set_binding_index(0);
        assert_eq!(node.binding_index(), 0);

        node.set_binding_index(1);
        assert_eq!(node.binding_index(), 1);

        node.set_binding_index(0xF_FFFF); // max 20-bit
        assert_eq!(node.binding_index(), 0xF_FFFF);

        // Verify node_type and flags are preserved
        assert_eq!(node.node_type(), NodeType::Element);
        assert!(node.has_flag(Node::HAS_CHILDREN));
    }

    #[test]
    #[cfg(debug_assertions)]
    #[should_panic(expected = "binding_index")]
    fn binding_index_overflow_panics_in_debug() {
        let mut node = Node::default();
        node.set_binding_index(0x10_0000); // 21 bits — too large
    }

    #[test]
    fn is_nil_flag_set_clear() {
        let mut node = Node::default();
        node.set_node_type(NodeType::Element);
        assert!(!node.has_flag(Node::IS_NIL));

        node.set_flag(Node::IS_NIL);
        assert!(node.has_flag(Node::IS_NIL));
        assert_eq!(node.node_type(), NodeType::Element);

        node.clear_flag(Node::IS_NIL);
        assert!(!node.has_flag(Node::IS_NIL));
    }

    #[test]
    fn is_nil_and_binding_index_independent() {
        let mut node = Node::default();
        node.set_node_type(NodeType::Element);
        node.set_flag(Node::IS_NIL);
        node.set_binding_index(123);

        assert!(node.has_flag(Node::IS_NIL));
        assert_eq!(node.binding_index(), 123);
        assert_eq!(node.node_type(), NodeType::Element);
    }

    #[test]
    fn set_binding_index_preserves_is_nil() {
        let mut node = Node::default();
        node.set_node_type(NodeType::Element);
        node.set_flag(Node::HAS_CHILDREN | Node::IS_NIL);

        node.set_binding_index(42);
        assert!(node.has_flag(Node::IS_NIL));
        assert!(node.has_flag(Node::HAS_CHILDREN));
        assert_eq!(node.binding_index(), 42);
        assert_eq!(node.node_type(), NodeType::Element);
    }

    #[test]
    fn page_addressing_round_trip() {
        // First node on first page
        assert_eq!(page_of(0), 0);
        assert_eq!(slot_of(0), 0);
        assert_eq!(node_ref_from(0, 0), 0);

        // Last slot on first page
        assert_eq!(page_of(PAGE_SIZE - 1), 0);
        assert_eq!(slot_of(PAGE_SIZE - 1), PAGE_SIZE - 1);
        assert_eq!(node_ref_from(0, PAGE_SIZE - 1), PAGE_SIZE - 1);

        // First slot on second page
        assert_eq!(page_of(PAGE_SIZE), 1);
        assert_eq!(slot_of(PAGE_SIZE), 0);
        assert_eq!(node_ref_from(1, 0), PAGE_SIZE);

        // Arbitrary position
        let page = 7u32;
        let slot = 123u32;
        let r = node_ref_from(page, slot);
        assert_eq!(page_of(r), page);
        assert_eq!(slot_of(r), slot);
    }

    #[test]
    fn null_sentinel() {
        assert_eq!(NULL, u32::MAX);
        // NULL should decode to a very large page, not page 0
        assert_ne!(page_of(NULL), 0);
    }

    #[test]
    fn dom_node_type_conversion() {
        assert_eq!(DomNodeType::from(NodeType::Root), DomNodeType::Root);
        assert_eq!(DomNodeType::from(NodeType::Element), DomNodeType::Element);
        assert_eq!(
            DomNodeType::from(NodeType::Attribute),
            DomNodeType::Attribute
        );
        assert_eq!(DomNodeType::from(NodeType::Text), DomNodeType::Text);
        assert_eq!(
            DomNodeType::from(NodeType::Whitespace),
            DomNodeType::Whitespace
        );
        assert_eq!(
            DomNodeType::from(NodeType::SignificantWhitespace),
            DomNodeType::SignificantWhitespace
        );
        assert_eq!(DomNodeType::from(NodeType::Comment), DomNodeType::Comment);
        assert_eq!(
            DomNodeType::from(NodeType::ProcessingInstruction),
            DomNodeType::ProcessingInstruction
        );
    }

    #[test]
    #[should_panic(expected = "Nul and ChildValue")]
    fn dom_node_type_nul_panics() {
        let _ = DomNodeType::from(NodeType::Nul);
    }

    #[test]
    #[should_panic(expected = "Nul and ChildValue")]
    fn dom_node_type_child_value_panics() {
        let _ = DomNodeType::from(NodeType::ChildValue);
    }

    #[test]
    fn node_type_default_is_nul() {
        assert_eq!(NodeType::default(), NodeType::Nul);
    }

    #[test]
    fn page_constants() {
        assert_eq!(PAGE_SHIFT, 12);
        assert_eq!(PAGE_SIZE, 4096);
        assert_eq!(PAGE_MASK, 0xFFF);
        assert_eq!(1u32 << PAGE_SHIFT, PAGE_SIZE);
        assert_eq!(PAGE_SIZE - 1, PAGE_MASK);
    }
}