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
//! The plugin surface: a [`DatatypeLibrary`] trait one `datatypeLibrary` URI
//! implements, and a [`DatatypeRegistry`] mapping URIs to implementations
//! that [`crate::relaxng::validate`] takes as a parameter.
//!
//! [`DatatypeRegistry::new`] always has the RELAX NG built-in (empty-string)
//! library and the XSD library (`http://www.w3.org/2001/XMLSchema-datatypes`)
//! registered — see the upstream project's design notes (Phase 06) for why both are
//! treated as "this crate natively supports them" rather than something a
//! caller must supply. Anything else (e.g. vnu's own
//! `http://whattf.org/datatype-draft`) is the caller's responsibility to
//! implement and [`DatatypeRegistry::register`] — this crate has no
//! knowledge of any non-standard library's types.
use BTreeMap;
use fmt;
use ;
/// The namespace URI identifying the XSD datatype library, as used in
/// `datatypeLibrary="..."` — see the upstream project's design notes.
pub const XSD_DATATYPE_LIBRARY: &str = "http://www.w3.org/2001/XMLSchema-datatypes";
/// The in-scope XML namespace prefix bindings at the point a value occurs
/// in the *document* (not the schema) — needed to resolve `QName`/
/// `NOTATION`-typed values, whose lexical form (`prefix:local`) only
/// denotes a value together with a prefix-to-URI binding. Built once per
/// element from [`crate::relaxng::Element::namespace_bindings`].
/// A `datatypeLibrary` implementation: recognizes a set of type names,
/// each with its own applicable parameters (facets, in XSD's terminology).
///
/// Modeled on the datatype interface the RELAX NG spec itself describes
/// (type validity, parameter validity, value matching, value equality) —
/// independently designed for Rust, not ported from any existing
/// implementation.
///
/// ```
/// use teixml::relaxng::{DatatypeContext, DatatypeLibrary, DatatypeRegistry};
///
/// /// `urn:example:datatypes`: one type, `even`, for even integers.
/// struct Even;
///
/// impl DatatypeLibrary for Even {
/// fn validate_params(&self, type_name: &str, params: &[(&str, &str)]) -> Result<(), String> {
/// match (type_name, params) {
/// ("even", []) => Ok(()),
/// ("even", _) => Err("`even` takes no parameters".into()),
/// _ => Err(format!("unknown type `{type_name}`")),
/// }
/// }
/// fn matches(&self, _: &str, _: &[(&str, &str)], value: &str, _: &DatatypeContext) -> bool {
/// value.trim().parse::<i64>().is_ok_and(|n| n % 2 == 0)
/// }
/// fn values_equal(
/// &self,
/// _: &str,
/// expected: &str,
/// _: &DatatypeContext,
/// actual: &str,
/// _: &DatatypeContext,
/// ) -> bool {
/// expected.trim().parse::<i64>().ok() == actual.trim().parse::<i64>().ok()
/// }
/// fn is_context_dependent(&self, _: &str) -> bool {
/// false
/// }
/// }
///
/// let mut registry = DatatypeRegistry::new();
/// registry.register("urn:example:datatypes", Even);
/// // Pass `registry` to `teixml::relaxng::Schema::validate` for schemas
/// // declaring `datatypes ex = "urn:example:datatypes"` and using `ex:even`.
/// ```
/// The schema uses a `datatypeLibrary`/type/parameter this
/// [`DatatypeRegistry`] can't handle.
/// Maps `datatypeLibrary` URIs to [`DatatypeLibrary`] implementations for
/// one [`crate::relaxng::validate`] call.
///
/// ```
/// use teixml::relaxng::{DatatypeRegistry, Schema, SchemaSource, SchemaSyntax};
/// # use teixml::relaxng::{ResolveError, SchemaResolver};
/// # struct NoIncludes;
/// # impl SchemaResolver for NoIncludes {
/// # fn resolve(&self, href: &str, _: &str) -> Result<SchemaSource, ResolveError> {
/// # Err(ResolveError::new(href))
/// # }
/// # }
/// # struct Leaf(&'static str);
/// # impl teixml::relaxng::Element for Leaf {
/// # type Location = ();
/// # fn name(&self) -> teixml::relaxng::ExpandedName {
/// # teixml::relaxng::ExpandedName::new(None::<String>, "n")
/// # }
/// # fn attributes(&self) -> impl Iterator<Item = (teixml::relaxng::ExpandedName, String)> {
/// # std::iter::empty()
/// # }
/// # fn children(&self) -> impl Iterator<Item = teixml::relaxng::Content<Self>> {
/// # std::iter::once(teixml::relaxng::Content::Text(self.0.to_owned()))
/// # }
/// # }
/// let source = SchemaSource::new("element n { xsd:integer }", "n.rnc", SchemaSyntax::Compact);
/// let schema = Schema::compile(&source, &NoIncludes)?;
///
/// // The default registry knows XSD types...
/// assert!(schema.validate(&DatatypeRegistry::new(), &Leaf("42"))?.is_empty());
/// // ...a built-in-only one refuses to validate rather than guess.
/// assert!(schema.validate(&DatatypeRegistry::builtin_only(), &Leaf("42")).is_err());
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```