Skip to main content

xsd_schema/
lib.rs

1//! XSD 1.0 Schema Parser and Validator
2//!
3//! This crate provides a complete XSD 1.0 schema parser with structural validation,
4//! namespace management, and W3C conformance testing. It follows the design specifications
5//! in the XSD_*.md documentation files.
6//!
7//! # Entry Points
8//!
9//! ## Single Schema (Recommended)
10//!
11//! Use [`load_and_process_schema`] for complete processing of a single schema.
12//! XSD version is set on `SchemaSet` — the parser derives it automatically.
13//!
14//! ```
15//! use xsd_schema::{SchemaSet, load_and_process_schema};
16//!
17//! // Use SchemaSet::new() for XSD 1.0, SchemaSet::xsd11() for XSD 1.1
18//! let mut schema_set = SchemaSet::new();
19//! let xml = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
20//!     <xs:element name="root" type="xs:string"/>
21//! </xs:schema>"#;
22//!
23//! let stats = load_and_process_schema(xml.as_bytes(), "schema.xsd", &mut schema_set, None)
24//!     .expect("failed to load schema");
25//! assert_eq!(stats.doc_id, 0);
26//! ```
27//!
28//! ## Multiple Related Schemas
29//!
30//! For loading multiple schema files, use the two-phase approach:
31//!
32//! ```
33//! use xsd_schema::{SchemaSet, parse_schema_only, process_loaded_schemas};
34//!
35//! let mut schema_set = SchemaSet::new();
36//!
37//! let schemas = [
38//!     (r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
39//!                   targetNamespace="urn:schema1">
40//!         <xs:element name="item1" type="xs:string"/>
41//!     </xs:schema>"#, "schema1.xsd"),
42//!     (r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
43//!                   targetNamespace="urn:schema2">
44//!         <xs:element name="item2" type="xs:int"/>
45//!     </xs:schema>"#, "schema2.xsd"),
46//! ];
47//!
48//! // Phase 1: Parse all schemas
49//! for (xml, uri) in schemas {
50//!     parse_schema_only(xml.as_bytes(), uri, &mut schema_set).expect("parse failed");
51//! }
52//!
53//! // Phase 2: Process all schemas together
54//! // (redefine/override application, inline assembly, reference resolution)
55//! // Note: all participating schemas — including redefine/override targets —
56//! // must be parsed before calling this function.
57//! let (inline_stats, resolution_stats) = process_loaded_schemas(&mut schema_set)
58//!     .expect("processing failed");
59//! ```
60//!
61//! ## Advanced: Low-Level Parser
62//!
63//! For custom pipelines, the low-level parser is available at [`parser::parse_schema`].
64//! This only performs Phase 1 (parsing + assembly) - subsequent phases must be run manually.
65//!
66//! # Architecture
67//!
68//! The parser uses a state machine approach with typed parser frames for each XSD element type.
69//! All schema components are stored in arenas with typed IDs to avoid reference cycles.
70//!
71//! ## Core Modules
72//!
73//! - `parser` - XSD document parsing with location tracking
74//! - `namespace` - String interning and namespace management
75//! - `schema` - Schema component model (elements, types, groups)
76//! - `types` - Type definitions and facets
77//!
78//! # Example
79//!
80//! ```rust
81//! use xsd_schema::SchemaSet;
82//!
83//! // Create an empty schema set
84//! let mut schema_set = SchemaSet::new();
85//!
86//! // Schema parser will be added in later implementation
87//! // For now, the schema set can be populated programmatically
88//! ```
89
90// Core type definitions
91pub mod arenas;
92pub mod error;
93pub mod ids;
94
95// Parser infrastructure
96pub mod parser;
97
98// Namespace management
99pub mod namespace;
100
101// Schema component model
102pub mod schema;
103pub mod types;
104
105// DOM navigation (always available)
106pub mod navigator;
107
108// XPath 2.0 engine (only with xsd11 feature)
109#[cfg(feature = "xsd11")]
110pub mod xpath;
111
112// Page-based XML document buffer (only with xsd11 feature)
113#[cfg(feature = "xsd11")]
114pub mod document;
115
116// Pipeline orchestration
117pub mod pipeline;
118
119// Embedded assets
120pub mod embedded;
121
122// Builder pattern API
123pub mod builder;
124
125// Regex pattern conversion (shared between XSD and XPath)
126pub mod regex_convert;
127pub(crate) mod regex_xsd_unicode;
128
129// NFA compiler for content models
130pub mod compiler;
131
132// Instance validation
133pub mod validation;
134
135// Re-export primary types
136pub use error::{FacetError, FacetResult, SchemaError, SchemaResult};
137pub use ids::*;
138pub use schema::{SchemaDocument, SchemaSet};
139
140// Re-export resolution and inline assembly
141pub use schema::{
142    assemble_inline_types, resolve_all_references, InlineAssemblyStats, ResolutionStats,
143};
144
145// Re-export XSD version
146pub use schema::model::XsdVersion;
147
148// Re-export regex compatibility mode
149pub use schema::model::RegexCompat;
150
151// Re-export type system enums
152pub use types::{BuiltinTypes, PrimitiveTypeCode, ValueKind, XmlTypeCode};
153
154// Re-export facet types
155pub use types::{
156    facet_applicable, facet_applicable_for_type, normalize_whitespace, FacetApplicability,
157    FacetFixed, FacetKind, FacetSet, WhitespaceMode,
158};
159
160// Re-export navigator types (always available)
161pub use navigator::{
162    DomNavigator, DomNodeType, NamespaceAxisScope, NavigatorError, RoXmlNavigator, XmlNodeOrder,
163};
164
165// Re-export XPath types (only with xsd11 feature)
166#[cfg(feature = "xsd11")]
167pub use xpath::{
168    BufferedNodeIterator, EmptyIterator, EvalValue, ExternalVar, RangeIterator, TreeComparer,
169    TypedEvaluator, VecNodeIterator, XPathContext, XPathEvaluator, XPathExpr, XmlItem, XmlItemRef,
170    XmlNodeIterator,
171};
172
173// Re-export pipeline functions
174pub use pipeline::{
175    load_and_process_schema, load_schema, parse_schema_only, process_loaded_schemas,
176    DirectiveStats, PipelineConfig, PipelineStats,
177};
178
179// Re-export async pipeline functions
180#[cfg(feature = "async")]
181pub use pipeline::{load_and_process_schema_async, load_schema_async};
182
183// Re-export builder types
184pub use builder::{CompilationStats, CompiledSchemaSet, SchemaSetBuilder};
185
186// Re-export resolver types
187pub use parser::resolver::{
188    decode_xml_bytes, decode_xml_to_utf8_bytes, EmbeddedLoader, FileSystemLoader, LoaderChain,
189    ResolverConfig, SchemaCatalog, SchemaLoader, SchemaResolver,
190};
191
192// Re-export async loader trait
193#[cfg(feature = "async")]
194pub use parser::resolver::AsyncSchemaLoader;
195
196// Re-export embedded assets
197pub use embedded::{
198    get_embedded_schema, has_embedded_schema, XLINK_NAMESPACE, XLINK_XSD, XML_NAMESPACE, XML_XSD,
199};
200
201// Re-export compiler types
202pub use compiler::{
203    compile_model_group, compile_particle, fragment_to_table, CompileContext, FragmentBuilder,
204    NfaCompileError, NfaCompileResult, NfaFragment, NfaState, NfaTable, NfaTerm, NfaTransition,
205    StateId, TransitionKind,
206};
207
208// Re-export instance validation types
209// Note: ValidationError here is distinct from types::validators::ValidationError
210pub use validation::{
211    error as validation_error, error_with_path as validation_error_with_path,
212    facet_constraint_code, from_facet_error, from_value_error, value_error_constraint_code,
213    ValidationError as InstanceValidationError, ValidationResult as InstanceValidationResult,
214};
215
216// Re-export hint-driven schema loading
217pub use validation::hint_loader::{
218    enrich_schema_set, load_hints_into_builder, EnrichmentOutcome, HintLoadResult,
219};
220pub use validation::info::{NoNamespaceSchemaLocationHint, SchemaLocationHint};