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
use fmt;
/// Which of RELAX NG's two equivalent syntaxes a [`SchemaSource`]'s text is
/// written in.
/// Schema text plus enough context (a base URI, and which syntax it's
/// written in) to parse it and resolve any `include`/`externalRef` it
/// contains. This crate never reads files or network resources itself —
/// callers supply schema text directly, and a [`SchemaResolver`] for
/// anything the schema references by `href`.
/// Resolves an `include`/`externalRef` `href` (relative to a base URI) to
/// the [`SchemaSource`] it names. This crate has no file-system or
/// network access of its own — implement this over whatever loading
/// mechanism (files, an in-memory map, HTTP, ...) fits the caller, with
/// whatever security boundary (allowed schemes, path restrictions) is
/// appropriate there.
///
/// ```
/// use std::collections::HashMap;
/// use teixml::relaxng::{ResolveError, Schema, SchemaResolver, SchemaSource, SchemaSyntax};
///
/// /// Schemas held in memory, by name.
/// struct InMemory(HashMap<&'static str, &'static str>);
///
/// impl SchemaResolver for InMemory {
/// fn resolve(&self, href: &str, _base_uri: &str) -> Result<SchemaSource, ResolveError> {
/// let text = self.0.get(href).ok_or_else(|| ResolveError::new(format!("no {href}")))?;
/// Ok(SchemaSource::new(*text, href, SchemaSyntax::Compact))
/// }
/// }
///
/// let resolver = InMemory(HashMap::from([("names.rnc", "name = element name { text }")]));
/// let main = SchemaSource::new(
/// "include \"names.rnc\"\nstart = element person { name+ }",
/// "main.rnc",
/// SchemaSyntax::Compact,
/// );
/// let schema = Schema::compile(&main, &resolver)?;
/// # let _ = schema;
///
/// let missing = SchemaSource::new("include \"other.rnc\"", "main.rnc", SchemaSyntax::Compact);
/// assert!(Schema::compile(&missing, &resolver).is_err());
/// # Ok::<(), teixml::relaxng::CompileError>(())
/// ```
/// A [`SchemaResolver`] failed to resolve an `href`.