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
//! The public entry point: [`Schema`] ties `parse`/`simplify`/`validate`
//! together into a single "compile once, validate many documents"
//! workflow, which is almost always what a caller wants — the lower-level
//! [`crate::parse`]/[`crate::simplify`]/[`crate::validate`] functions
//! remain available separately for callers who need the intermediate
//! [`crate::SchemaDocument`]/[`CompiledSchema`] (e.g. to inspect a schema
//! without validating anything against it).
use fmt;
use crate::;
/// A parsed and composed ("simplified", in RELAX NG spec terms) schema,
/// ready to validate any number of documents against.
///
/// Compiling — parsing the schema text and resolving/composing any
/// `include`/`externalRef`/`div`/`combine` it uses — is the relatively
/// expensive step; [`Schema::compile`] does it once, and the resulting
/// `Schema` is then reused for every document validated against it.
///
/// # Example
///
/// ```
/// use relax_ng::{
/// Content, DatatypeRegistry, Element, ExpandedName, ResolveError, Schema, SchemaResolver,
/// SchemaSource, SchemaSyntax,
/// };
///
/// // This schema is self-contained (no `include`/`externalRef`), so the
/// // resolver is never actually called — it only needs to exist to
/// // satisfy `Schema::compile`'s signature.
/// struct NoResolver;
/// impl SchemaResolver for NoResolver {
/// fn resolve(&self, href: &str, _base_uri: &str) -> Result<SchemaSource, ResolveError> {
/// Err(ResolveError::new(format!("no such resource: {href}")))
/// }
/// }
///
/// let source = SchemaSource::new(
/// "element greeting { attribute lang { text }, text }",
/// "memory:/greeting.rnc",
/// SchemaSyntax::Compact,
/// );
/// let schema = Schema::compile(&source, &NoResolver).expect("schema compiles");
///
/// // A minimal `Element` implementation over an in-memory tree — a real
/// // caller implements this over whatever tree type it already has (an
/// // XML DOM, an HTML parser's tree, ...); this crate parses none of
/// // that itself.
/// struct Doc {
/// name: &'static str,
/// attributes: Vec<(&'static str, &'static str)>,
/// text: &'static str,
/// }
/// impl Element for Doc {
/// type Location = ();
///
/// fn name(&self) -> ExpandedName {
/// ExpandedName::new(None::<String>, self.name)
/// }
/// fn attributes(&self) -> impl Iterator<Item = (ExpandedName, String)> {
/// self.attributes
/// .iter()
/// .map(|(name, value)| (ExpandedName::new(None::<String>, *name), value.to_string()))
/// }
/// fn children(&self) -> impl Iterator<Item = Content<Self>> {
/// std::iter::once(Content::Text(self.text.to_owned()))
/// }
/// }
///
/// let document = Doc {
/// name: "greeting",
/// attributes: vec![("lang", "en")],
/// text: "hello",
/// };
///
/// // The built-in `string`/`token` datatypes are all this schema needs.
/// let errors = schema
/// .validate(&DatatypeRegistry::new(), &document)
/// .expect("only built-in/XSD datatypes are used");
/// assert!(errors.is_empty());
/// ```
/// Either step [`Schema::compile`] performs can fail on its own: `source`
/// might not even parse (malformed `.rnc`/`.rng` syntax — [`ParseError`]),
/// or it might parse but fail composition (an undefined `ref`, illegal
/// recursion, a §7 restriction violation, ... — [`SchemaError`]).