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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Schema location hint loader.
//!
//! A helper that takes accumulated `xsi:schemaLocation` and
//! `xsi:noNamespaceSchemaLocation` hints from a validation run and attempts
//! to load the referenced schemas into a [`SchemaSetBuilder`] for
//! recompilation.
//!
//! # Design
//!
//! `process_loaded_schemas()` is a whole-set compile pass that is not
//! idempotent. Therefore this helper works with [`SchemaSetBuilder`]
//! (pre-compile), not with an already-compiled [`SchemaSet`]. The caller
//! adds their base schemas, enriches with hints, then compiles once.
//!
//! URI resolution is delegated to the builder's [`SchemaResolver`] so that
//! Windows paths, URL normalization, and other platform-specific handling
//! are applied consistently.
//!
//! # Example
//!
//! ```ignore
//! // 1. Validate and collect hints
//! let sl_hints = runtime.schema_location_hints().to_vec();
//! let nnsl_hints = runtime.no_namespace_schema_location_hints().to_vec();
//!
//! // 2. Build enriched schema set
//! let mut builder = SchemaSetBuilder::new();
//! builder.try_add("base.xsd")?;
//! load_hints_into_builder(&mut builder, &sl_hints, &nnsl_hints);
//! let compiled = builder.compile()?;
//! ```

use super::info::{NoNamespaceSchemaLocationHint, SchemaLocationHint};
use crate::builder::SchemaSetBuilder;
use crate::error::SchemaError;

/// Result of hint-driven schema loading.
#[derive(Debug, Default)]
pub struct HintLoadResult {
    /// Number of schemas freshly loaded from hints.
    pub loaded_count: usize,
    /// Number of hints skipped (already loaded, load failure, etc.).
    pub skipped_count: usize,
    /// Errors encountered during loading (non-fatal — partial success is possible).
    pub errors: Vec<SchemaError>,
}

/// Enrich a [`SchemaSetBuilder`] with schemas discovered from
/// `xsi:schemaLocation` and `xsi:noNamespaceSchemaLocation` hints
/// collected during a validation run.
///
/// Each hint carries its own base URI (from the instance document) so
/// that relative schema locations are resolved correctly. URI resolution
/// is performed by the builder's [`SchemaResolver`], which handles
/// platform-specific paths and URL normalization.
///
/// Schemas that are already loaded in the builder are silently skipped
/// and counted in [`HintLoadResult::skipped_count`].
/// Load/network failures are non-fatal and collected in
/// [`HintLoadResult::errors`].
///
/// The builder must NOT yet be compiled. After calling this, the caller
/// should call `builder.compile()` to produce the final compiled schema set.
pub fn load_hints_into_builder(
    builder: &mut SchemaSetBuilder,
    schema_location_hints: &[SchemaLocationHint],
    no_namespace_hints: &[NoNamespaceSchemaLocationHint],
) -> HintLoadResult {
    let mut result = HintLoadResult::default();

    for hint in schema_location_hints {
        try_load_hint(builder, &hint.location, &hint.base_uri, &mut result);
    }

    for hint in no_namespace_hints {
        try_load_hint(builder, &hint.location, &hint.base_uri, &mut result);
    }

    result
}

fn try_load_hint(
    builder: &mut SchemaSetBuilder,
    location: &str,
    base_uri: &str,
    result: &mut HintLoadResult,
) {
    match builder.try_add_relative(location, base_uri) {
        Ok(true) => {
            result.loaded_count += 1;
        }
        Ok(false) => {
            // Already loaded — dedup skip
            result.skipped_count += 1;
        }
        Err(e) => {
            result.errors.push(e);
            result.skipped_count += 1;
        }
    }
}

/// Outcome of [`enrich_schema_set`].
///
/// `schema_set` is `Some` only when the recompile succeeded. The other
/// fields surface diagnostics that the previous `Option<SchemaSet>` API
/// silently dropped:
///
/// - `hint_errors` — per-hint load failures (network, missing file,
///   relative-URI resolution, ...). Always non-fatal.
/// - `compile_error` — the `SchemaError` from recompiling the enriched
///   builder, if recompilation failed. When this is `Some`, `schema_set`
///   is always `None`.
///
/// The `is_no_op` helper distinguishes "no hints to apply" from
/// "tried but failed".
#[derive(Debug, Default)]
pub struct EnrichmentOutcome {
    /// Compiled enriched schema set, if recompile succeeded.
    pub schema_set: Option<crate::schema::SchemaSet>,
    /// Errors from individual hint loads (non-fatal, partial success
    /// is possible).
    pub hint_errors: Vec<SchemaError>,
    /// Recompile error after the enriched builder was assembled, if any.
    pub compile_error: Option<SchemaError>,
}

impl EnrichmentOutcome {
    /// Returns `true` when no hints were provided — enrichment was a
    /// no-op rather than a failure.
    pub fn is_no_op(&self) -> bool {
        self.schema_set.is_none()
            && self.hint_errors.is_empty()
            && self.compile_error.is_none()
    }

    /// Returns the enriched [`SchemaSet`] if available, otherwise the
    /// original. Convenient when you want to "use enriched if it
    /// worked, fall back to the original".
    pub fn schema_set_or<'a>(
        &'a self,
        original: &'a crate::schema::SchemaSet,
    ) -> &'a crate::schema::SchemaSet {
        self.schema_set.as_ref().unwrap_or(original)
    }
}

/// Build an enriched [`SchemaSet`] by re-loading the original schemas and
/// adding any `xsi:schemaLocation` / `xsi:noNamespaceSchemaLocation` hints
/// collected during a validation run.
///
/// Returns an [`EnrichmentOutcome`] describing the result. The compiled
/// `schema_set` is populated only when at least one hint was supplied
/// **and** the recompile succeeded; in every other case
/// `schema_set` is `None` and `hint_errors` / `compile_error` describe
/// what happened.
///
/// This is the recommended way to handle schema-location hints without
/// manually tracking original schema file paths:
///
/// ```rust,ignore
/// // After first validation pass:
/// let sl = runtime.schema_location_hints().to_vec();
/// let nnsl = runtime.no_namespace_schema_location_hints().to_vec();
///
/// let outcome = enrich_schema_set(&schema_set, &sl, &nnsl);
/// if let Some(enriched) = outcome.schema_set.as_ref() {
///     // Re-validate with enriched schema set
/// } else if let Some(err) = outcome.compile_error.as_ref() {
///     eprintln!("hint enrichment recompile failed: {err}");
/// }
/// ```
pub fn enrich_schema_set(
    original: &crate::schema::SchemaSet,
    schema_location_hints: &[SchemaLocationHint],
    no_namespace_hints: &[NoNamespaceSchemaLocationHint],
) -> EnrichmentOutcome {
    if schema_location_hints.is_empty() && no_namespace_hints.is_empty() {
        return EnrichmentOutcome::default();
    }

    let mut builder = if original.xsd_version == crate::schema::model::XsdVersion::V1_1 {
        SchemaSetBuilder::xsd11()
    } else {
        SchemaSetBuilder::new()
    };

    builder.add_from(original);
    let hint_result =
        load_hints_into_builder(&mut builder, schema_location_hints, no_namespace_hints);

    match builder.compile() {
        Ok(compiled) => EnrichmentOutcome {
            schema_set: Some(compiled.into_schema_set()),
            hint_errors: hint_result.errors,
            compile_error: None,
        },
        Err(e) => EnrichmentOutcome {
            schema_set: None,
            hint_errors: hint_result.errors,
            compile_error: Some(e),
        },
    }
}


#[cfg(test)]
mod tests {
    use super::*;
    use crate::builder::SchemaSetBuilder;
    use crate::validation::info::{NoNamespaceSchemaLocationHint, SchemaLocationHint};

    #[test]
    fn test_load_hints_empty() {
        let mut builder = SchemaSetBuilder::new();
        let result = load_hints_into_builder(&mut builder, &[], &[]);
        assert_eq!(result.loaded_count, 0);
        assert_eq!(result.skipped_count, 0);
        assert!(result.errors.is_empty());
    }

    #[test]
    fn test_load_hints_nonexistent_file_is_nonfatal() {
        let mut builder = SchemaSetBuilder::new();
        let hints = vec![SchemaLocationHint {
            namespace: "urn:test".to_string(),
            location: "nonexistent_schema_abc123.xsd".to_string(),
            base_uri: String::new(),
        }];
        let result = load_hints_into_builder(&mut builder, &hints, &[]);
        assert_eq!(result.loaded_count, 0);
        assert_eq!(result.skipped_count, 1);
        assert_eq!(result.errors.len(), 1);
    }

    #[test]
    fn test_load_no_namespace_hints_nonexistent_is_nonfatal() {
        let mut builder = SchemaSetBuilder::new();
        let hints = vec![NoNamespaceSchemaLocationHint {
            location: "nonexistent_schema_abc123.xsd".to_string(),
            base_uri: String::new(),
        }];
        let result = load_hints_into_builder(&mut builder, &[], &hints);
        assert_eq!(result.loaded_count, 0);
        assert_eq!(result.skipped_count, 1);
        assert_eq!(result.errors.len(), 1);
    }

    #[test]
    fn test_duplicate_hints_counted_as_skipped() {
        // Load a real schema, then try to load it again via a duplicate hint.
        let xsd = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#;
        let mut builder = SchemaSetBuilder::new()
            .add_source(xsd, "http://example.com/dedup.xsd")
            .unwrap();

        // Hint pointing to the same location should be skipped, not loaded again.
        // try_add normalizes, and add_source records the exact base_uri —
        // use the same absolute URI so normalization matches.
        let hints = vec![SchemaLocationHint {
            namespace: "".to_string(),
            location: "http://example.com/dedup.xsd".to_string(),
            base_uri: String::new(),
        }];
        let result = load_hints_into_builder(&mut builder, &hints, &[]);
        assert_eq!(result.loaded_count, 0, "duplicate should not be loaded");
        assert_eq!(result.skipped_count, 1, "duplicate should be skipped");
        // The hint loader may produce an error if the resolver can't
        // re-fetch the URL, but the is_loaded check should prevent that.
        // The key assertion: it was not double-loaded.
    }

    #[test]
    fn test_add_source_normalizes_for_dedup() {
        // add_source with a relative path should normalize to the same
        // absolute load key that a later hint resolves to.
        let xsd = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#;
        let cwd = std::env::current_dir().unwrap();
        let mut builder = SchemaSetBuilder::new()
            .add_source(xsd, "schemas/test.xsd")
            .unwrap();

        let instance_base = cwd
            .join("schemas")
            .join("instance.xml")
            .to_string_lossy()
            .into_owned();
        let hints = vec![SchemaLocationHint {
            namespace: "".to_string(),
            location: "test.xsd".to_string(),
            base_uri: instance_base,
        }];
        let result = load_hints_into_builder(&mut builder, &hints, &[]);
        assert_eq!(
            result.loaded_count, 0,
            "hint resolving to already-loaded URI should not reload"
        );
        assert_eq!(result.skipped_count, 1);
    }

    #[test]
    fn test_enrich_schema_set_no_hints_is_no_op() {
        let xsd = r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#;
        let compiled = SchemaSetBuilder::new()
            .add_source(xsd, "test.xsd")
            .unwrap()
            .compile()
            .unwrap();

        let outcome = enrich_schema_set(compiled.schema_set(), &[], &[]);
        assert!(
            outcome.is_no_op(),
            "should be a no-op when no hints are provided"
        );
        assert!(outcome.schema_set.is_none());
        assert!(outcome.compile_error.is_none());
        assert!(outcome.hint_errors.is_empty());
    }

    #[test]
    fn test_enrich_schema_set_preserves_original_elements() {
        // Write a temp schema file so add_from can re-load from disk.
        let dir = std::env::temp_dir().join("xsd_hint_test_enrich");
        let _ = std::fs::create_dir_all(&dir);
        let schema_path = dir.join("base.xsd");
        std::fs::write(
            &schema_path,
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#,
        )
        .unwrap();

        let compiled = SchemaSetBuilder::new()
            .add("", &schema_path.to_string_lossy())
            .unwrap()
            .compile()
            .unwrap();
        let original = compiled.schema_set();

        // Provide a hint that fails to load — enrichment should still
        // succeed because add_from re-loaded the original schema, and
        // the hint failure is surfaced in `hint_errors`.
        let hints = vec![SchemaLocationHint {
            namespace: "urn:test".to_string(),
            location: "nonexistent_42.xsd".to_string(),
            base_uri: String::new(),
        }];

        let outcome = enrich_schema_set(original, &hints, &[]);
        assert!(
            outcome.schema_set.is_some(),
            "should return Some even if hint fails"
        );
        assert!(
            !outcome.hint_errors.is_empty(),
            "hint load failure must be surfaced in hint_errors"
        );
        assert!(
            outcome.compile_error.is_none(),
            "recompile of the original schemas should still succeed"
        );

        let enriched = outcome.schema_set.unwrap();
        let name = enriched.name_table.add("root");
        assert!(
            enriched.lookup_element(None, name).is_some(),
            "original element 'root' should still be present after enrichment"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_enrich_schema_set_preserves_xsd_version() {
        let dir = std::env::temp_dir().join("xsd_hint_test_version");
        let _ = std::fs::create_dir_all(&dir);
        let schema_path = dir.join("test.xsd");
        std::fs::write(
            &schema_path,
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#,
        )
        .unwrap();

        let compiled = SchemaSetBuilder::xsd11()
            .add("", &schema_path.to_string_lossy())
            .unwrap()
            .compile()
            .unwrap();
        let original = compiled.schema_set();
        assert_eq!(original.xsd_version, crate::schema::model::XsdVersion::V1_1);

        let hints = vec![SchemaLocationHint {
            namespace: "urn:test".to_string(),
            location: "nonexistent_42.xsd".to_string(),
            base_uri: String::new(),
        }];
        let enriched = enrich_schema_set(original, &hints, &[])
            .schema_set
            .unwrap();
        assert_eq!(
            enriched.xsd_version,
            crate::schema::model::XsdVersion::V1_1,
            "enriched set should preserve XSD 1.1 version"
        );

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_enrich_schema_set_surfaces_compile_error() {
        // Build an enriched set that cannot recompile: the hint adds a
        // schema whose targetNamespace already exists in the original
        // with a conflicting global element of the same name.
        let dir = std::env::temp_dir().join("xsd_hint_test_compile_err");
        let _ = std::fs::create_dir_all(&dir);
        let primary = dir.join("primary.xsd");
        std::fs::write(
            &primary,
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                          targetNamespace="urn:test">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#,
        )
        .unwrap();
        let conflict = dir.join("conflict.xsd");
        std::fs::write(
            &conflict,
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
                          targetNamespace="urn:test">
            <xs:element name="root" type="xs:int"/>
        </xs:schema>"#,
        )
        .unwrap();

        let compiled = SchemaSetBuilder::new()
            .add("urn:test", &primary.to_string_lossy())
            .unwrap()
            .compile()
            .unwrap();
        let original = compiled.schema_set();

        let hints = vec![SchemaLocationHint {
            namespace: "urn:test".to_string(),
            location: conflict.to_string_lossy().into_owned(),
            base_uri: String::new(),
        }];
        let outcome = enrich_schema_set(original, &hints, &[]);

        // Either the recompile rejects the conflict (compile_error set),
        // or the hint loader/dedup spots it (schema_set still None).
        // What we are asserting: the failure is **not** silently swallowed.
        assert!(
            outcome.schema_set.is_none() || outcome.compile_error.is_none(),
            "outcome must be internally consistent: {outcome:?}"
        );
        if outcome.schema_set.is_none() {
            assert!(
                outcome.compile_error.is_some() || !outcome.hint_errors.is_empty(),
                "if no enriched set is produced, the failure reason must be \
                 surfaced via compile_error or hint_errors, got: {outcome:?}"
            );
        }

        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_add_from_seeds_builder_with_loaded_locations() {
        let dir = std::env::temp_dir().join("xsd_hint_test_add_from");
        let _ = std::fs::create_dir_all(&dir);
        let schema_path = dir.join("original.xsd");
        std::fs::write(
            &schema_path,
            r#"<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
            <xs:element name="root" type="xs:string"/>
        </xs:schema>"#,
        )
        .unwrap();

        let compiled = SchemaSetBuilder::new()
            .add("", &schema_path.to_string_lossy())
            .unwrap()
            .compile()
            .unwrap();

        let mut builder = SchemaSetBuilder::new();
        builder.add_from(compiled.schema_set());

        // Verify the builder loaded the schema (has at least one document)
        assert!(builder.schema_count() > 0, "add_from should load schemas");

        let _ = std::fs::remove_dir_all(&dir);
    }
}