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
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
//! Schema composition graph types
//!
//! Tracks the relationships between schema documents created by
//! `xs:include`, `xs:import`, `xs:redefine`, and `xs:override` directives.
//!
//! These types are populated during directive resolution (Step 1) and
//! consumed by later pipeline stages for document-scoped component lookup,
//! provenance tracking, and override processing order.

use std::collections::HashMap;

use crate::ids::*;
use crate::parser::location::SourceRef;

/// The kind of composition relationship between two schema documents.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CompositionEdgeKind {
    Include,
    Import,
    Redefine,
    #[cfg(feature = "xsd11")]
    Override,
}

/// A directed edge in the schema composition graph.
///
/// Records that `source_doc` references `target_doc` via a composition
/// directive of the given `kind`. The `source` field links back to the
/// directive element in the source document for diagnostics.
///
/// `target_doc` is `None` for cycle edges discovered while the target is
/// still mid-parse (in the resolver's `resolving` set). After directive
/// resolution completes, call [`fixup_composition_edges`] to fill in
/// any `None` targets from `loaded_locations`.
#[derive(Debug, Clone)]
pub struct CompositionEdge {
    pub source_doc: DocumentId,
    pub target_doc: Option<DocumentId>,
    /// Resolved URI of the target (for fixup and diagnostics).
    pub resolved_location: String,
    pub kind: CompositionEdgeKind,
    /// Source location of the directive element (for diagnostics).
    pub source: Option<SourceRef>,
    /// Raw `schemaLocation` attribute value (not the resolved URI).
    pub schema_location: String,
}

// ============================================================================
// Forward-looking types for Steps 2–3 (defined now, used later)
// ============================================================================

/// Classification of a top-level schema component.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ComponentKind {
    SimpleType,
    ComplexType,
    Element,
    Attribute,
    ModelGroup,
    AttributeGroup,
    Notation,
    IdentityConstraint,
}

impl ComponentKind {
    /// Human-readable label for diagnostic messages
    /// (e.g. "simple type", "attribute group").
    pub fn display_name(self) -> &'static str {
        match self {
            ComponentKind::SimpleType => "simple type",
            ComponentKind::ComplexType => "complex type",
            ComponentKind::Element => "element",
            ComponentKind::Attribute => "attribute",
            ComponentKind::ModelGroup => "model group",
            ComponentKind::AttributeGroup => "attribute group",
            ComponentKind::Notation => "notation",
            ComponentKind::IdentityConstraint => "identity constraint",
        }
    }
}

/// Unique identity of a schema component (kind + QName).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ComponentIdentity {
    pub kind: ComponentKind,
    pub name: NameId,
    pub namespace: Option<NameId>,
}

/// Tracks which document a component originated from.
///
/// `owner_doc` is `None` when the originating document is unknown (e.g.,
/// pre-loaded schemas without directive resolution).
#[derive(Debug, Clone, Copy)]
pub struct ComponentOrigin {
    pub owner_doc: Option<DocumentId>,
    pub identity: ComponentIdentity,
}

/// A typed arena key for any top-level schema component.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ComponentKey {
    Type(TypeKey),
    Element(ElementKey),
    Attribute(AttributeKey),
    ModelGroup(ModelGroupKey),
    AttributeGroup(AttributeGroupKey),
    Notation(NotationKey),
    IdentityConstraint(IdentityConstraintKey),
}

/// How a component arrived in the effective schema set.
#[derive(Debug, Clone)]
pub enum CompositionAction {
    /// Declared directly in its owning document.
    Declared,
    /// Included from another document (same namespace merge).
    Included { from_doc: DocumentId },
    /// Replaced via `xs:redefine`. `from_doc` is `None` when the redefining
    /// document is unknown (directive without source location).
    Redefined {
        from_doc: Option<DocumentId>,
        replaced: ComponentOrigin,
    },
    /// Replaced via `xs:override` (XSD 1.1). `from_doc` is `None` when the
    /// overriding document is unknown.
    #[cfg(feature = "xsd11")]
    Overridden {
        from_doc: Option<DocumentId>,
        replaced: ComponentOrigin,
    },
}

/// An effective top-level component with its provenance metadata.
///
/// After composition, every visible component in the namespace tables
/// has an associated `EffectiveComponent` that records which document
/// contributed it and how it arrived (declared, included, redefined, etc.).
#[derive(Debug, Clone)]
pub struct EffectiveComponent {
    /// The arena key of the component.
    pub key: ComponentKey,
    /// Where this component originated.
    pub origin: ComponentOrigin,
    /// How this component entered the effective schema.
    pub action: CompositionAction,
}

/// Record a composition provenance entry into the effective components map.
///
/// Used by both `apply_redefine` and `apply_override` to record how a
/// component arrived in the effective schema set.
pub fn record_provenance(
    effective_components: &mut HashMap<ComponentIdentity, EffectiveComponent>,
    key: ComponentKey,
    kind: ComponentKind,
    namespace: Option<NameId>,
    name: NameId,
    acting_doc_id: Option<DocumentId>,
    action: CompositionAction,
) {
    let identity = ComponentIdentity {
        kind,
        name,
        namespace,
    };
    let origin = ComponentOrigin {
        owner_doc: acting_doc_id,
        identity,
    };
    effective_components.insert(
        identity,
        EffectiveComponent {
            key,
            origin,
            action,
        },
    );
}

/// Build a [`CompositionAction::Redefined`] action.
pub fn redefined_action(
    redefining_doc_id: Option<DocumentId>,
    kind: ComponentKind,
    name: NameId,
    namespace: Option<NameId>,
    target_doc_id: Option<DocumentId>,
) -> CompositionAction {
    let identity = ComponentIdentity {
        kind,
        name,
        namespace,
    };
    CompositionAction::Redefined {
        from_doc: redefining_doc_id,
        replaced: ComponentOrigin {
            owner_doc: target_doc_id,
            identity,
        },
    }
}

/// Build a [`CompositionAction::Overridden`] action (XSD 1.1).
#[cfg(feature = "xsd11")]
pub fn overridden_action(
    overriding_doc_id: Option<DocumentId>,
    kind: ComponentKind,
    name: NameId,
    namespace: Option<NameId>,
    target_doc_id: Option<DocumentId>,
) -> CompositionAction {
    let identity = ComponentIdentity {
        kind,
        name,
        namespace,
    };
    CompositionAction::Overridden {
        from_doc: overriding_doc_id,
        replaced: ComponentOrigin {
            owner_doc: target_doc_id,
            identity,
        },
    }
}

/// Per-document index of top-level components declared by a single schema document.
///
/// Populated during assembly (`register_*` calls) and used for document-scoped
/// lookup in `apply_redefine()` and `apply_override()`.
#[derive(Debug, Default)]
pub struct DocumentComponentIndex {
    index: HashMap<ComponentIdentity, ComponentKey>,
}

impl DocumentComponentIndex {
    pub fn new() -> Self {
        Self::default()
    }

    /// Insert a component into the index.
    pub fn insert(&mut self, identity: ComponentIdentity, key: ComponentKey) {
        self.index.insert(identity, key);
    }

    /// Raw lookup by kind, namespace, and name.
    fn get(
        &self,
        kind: ComponentKind,
        namespace: Option<NameId>,
        name: NameId,
    ) -> Option<&ComponentKey> {
        self.index.get(&ComponentIdentity {
            kind,
            name,
            namespace,
        })
    }

    /// Look up a type by namespace and local name (both simple and complex).
    pub fn lookup_type(&self, namespace: Option<NameId>, name: NameId) -> Option<TypeKey> {
        self.lookup_simple_type(namespace, name)
            .map(TypeKey::Simple)
            .or_else(|| {
                self.lookup_complex_type(namespace, name)
                    .map(TypeKey::Complex)
            })
    }

    /// Look up a simple type by namespace and local name.
    pub fn lookup_simple_type(
        &self,
        namespace: Option<NameId>,
        name: NameId,
    ) -> Option<SimpleTypeKey> {
        match self.get(ComponentKind::SimpleType, namespace, name) {
            Some(&ComponentKey::Type(TypeKey::Simple(key))) => Some(key),
            _ => None,
        }
    }

    /// Look up a complex type by namespace and local name.
    pub fn lookup_complex_type(
        &self,
        namespace: Option<NameId>,
        name: NameId,
    ) -> Option<ComplexTypeKey> {
        match self.get(ComponentKind::ComplexType, namespace, name) {
            Some(&ComponentKey::Type(TypeKey::Complex(key))) => Some(key),
            _ => None,
        }
    }

    /// Look up an element by namespace and local name.
    pub fn lookup_element(&self, namespace: Option<NameId>, name: NameId) -> Option<ElementKey> {
        match self.get(ComponentKind::Element, namespace, name) {
            Some(&ComponentKey::Element(key)) => Some(key),
            _ => None,
        }
    }

    /// Look up an attribute by namespace and local name.
    pub fn lookup_attribute(
        &self,
        namespace: Option<NameId>,
        name: NameId,
    ) -> Option<AttributeKey> {
        match self.get(ComponentKind::Attribute, namespace, name) {
            Some(&ComponentKey::Attribute(key)) => Some(key),
            _ => None,
        }
    }

    /// Look up a model group by namespace and local name.
    pub fn lookup_model_group(
        &self,
        namespace: Option<NameId>,
        name: NameId,
    ) -> Option<ModelGroupKey> {
        match self.get(ComponentKind::ModelGroup, namespace, name) {
            Some(&ComponentKey::ModelGroup(key)) => Some(key),
            _ => None,
        }
    }

    /// Look up an attribute group by namespace and local name.
    pub fn lookup_attribute_group(
        &self,
        namespace: Option<NameId>,
        name: NameId,
    ) -> Option<AttributeGroupKey> {
        match self.get(ComponentKind::AttributeGroup, namespace, name) {
            Some(&ComponentKey::AttributeGroup(key)) => Some(key),
            _ => None,
        }
    }

    /// Look up a notation by namespace and local name.
    pub fn lookup_notation(&self, namespace: Option<NameId>, name: NameId) -> Option<NotationKey> {
        match self.get(ComponentKind::Notation, namespace, name) {
            Some(&ComponentKey::Notation(key)) => Some(key),
            _ => None,
        }
    }

    /// Returns `true` if the index is empty.
    pub fn is_empty(&self) -> bool {
        self.index.is_empty()
    }

    /// Returns the number of components in the index.
    pub fn len(&self) -> usize {
        self.index.len()
    }

    /// Iterate over all `(identity, key)` pairs in the index.
    pub fn iter(&self) -> impl Iterator<Item = (&ComponentIdentity, &ComponentKey)> {
        self.index.iter()
    }
}

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

    #[test]
    fn test_composition_edge_kind_equality() {
        assert_eq!(CompositionEdgeKind::Include, CompositionEdgeKind::Include);
        assert_ne!(CompositionEdgeKind::Include, CompositionEdgeKind::Import);
        assert_ne!(CompositionEdgeKind::Import, CompositionEdgeKind::Redefine);
    }

    #[test]
    fn test_composition_edge_construction() {
        let edge = CompositionEdge {
            source_doc: 0,
            target_doc: Some(1),
            resolved_location: "/path/to/types.xsd".to_string(),
            kind: CompositionEdgeKind::Include,
            source: None,
            schema_location: "types.xsd".to_string(),
        };
        assert_eq!(edge.source_doc, 0);
        assert_eq!(edge.target_doc, Some(1));
        assert_eq!(edge.kind, CompositionEdgeKind::Include);
        assert!(edge.source.is_none());
        assert_eq!(edge.schema_location, "types.xsd");
    }

    #[test]
    fn test_composition_edge_cycle_none_target() {
        let edge = CompositionEdge {
            source_doc: 1,
            target_doc: None,
            resolved_location: "/path/to/cyclic.xsd".to_string(),
            kind: CompositionEdgeKind::Include,
            source: None,
            schema_location: "cyclic.xsd".to_string(),
        };
        assert!(edge.target_doc.is_none());
        assert_eq!(edge.resolved_location, "/path/to/cyclic.xsd");
    }

    #[test]
    fn test_composition_edge_with_source() {
        use crate::parser::location::{SourceRef, SourceSpan};
        let edge = CompositionEdge {
            source_doc: 0,
            target_doc: Some(1),
            resolved_location: "/path/to/base.xsd".to_string(),
            kind: CompositionEdgeKind::Redefine,
            source: Some(SourceRef::new(0, SourceSpan::new(100, 200))),
            schema_location: "base.xsd".to_string(),
        };
        let src = edge.source.unwrap();
        assert_eq!(src.doc_id, 0);
        assert_eq!(src.span.start, 100);
    }

    #[test]
    fn test_component_kind_variants() {
        let kinds = [
            ComponentKind::SimpleType,
            ComponentKind::ComplexType,
            ComponentKind::Element,
            ComponentKind::Attribute,
            ComponentKind::ModelGroup,
            ComponentKind::AttributeGroup,
            ComponentKind::Notation,
            ComponentKind::IdentityConstraint,
        ];
        // All variants are distinct
        for (i, a) in kinds.iter().enumerate() {
            for (j, b) in kinds.iter().enumerate() {
                if i == j {
                    assert_eq!(a, b);
                } else {
                    assert_ne!(a, b);
                }
            }
        }
    }

    #[test]
    fn test_component_identity_equality_and_copy() {
        let id1 = ComponentIdentity {
            kind: ComponentKind::Element,
            name: NameId(1),
            namespace: Some(NameId(2)),
        };
        // Copy trait: assignment copies value
        let id2 = id1;
        assert_eq!(id1, id2);

        let id3 = ComponentIdentity {
            kind: ComponentKind::Attribute,
            name: NameId(1),
            namespace: Some(NameId(2)),
        };
        assert_ne!(id1, id3);
    }

    #[test]
    fn test_component_origin_construction_and_copy() {
        let origin = ComponentOrigin {
            owner_doc: Some(42),
            identity: ComponentIdentity {
                kind: ComponentKind::SimpleType,
                name: NameId(5),
                namespace: None,
            },
        };
        // Copy trait: assignment copies value
        let origin2 = origin;
        assert_eq!(origin.owner_doc, Some(42));
        assert_eq!(origin.owner_doc, origin2.owner_doc);
        assert_eq!(origin.identity.kind, ComponentKind::SimpleType);
        assert_eq!(origin.identity.name, NameId(5));
        assert_eq!(origin.identity.namespace, None);
    }

    #[cfg(feature = "xsd11")]
    #[test]
    fn test_override_edge_kind() {
        assert_eq!(CompositionEdgeKind::Override, CompositionEdgeKind::Override);
        assert_ne!(CompositionEdgeKind::Override, CompositionEdgeKind::Include);
    }
}