xee-interpreter 0.2.0

Interpreter for XPath and XSLT
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
// this is unfortunately a ridiculously verbose module, wiring everything up
// carefully so we don't use performance due to dynamic dispatch on the inside.
// The verbosity is all pretty straightforward though.

// creation.rs contains various functions that create Sequence
// compare.rs contains various comparison functions

use xot::Xot;

use crate::{
    atomic::{self, AtomicCompare},
    context, error, function,
    string::Collation,
    xml,
};

use super::{
    item::Item,
    traits::{BoxedItemIter, SequenceCompare, SequenceCore, SequenceExt, SequenceOrder},
    variant::{Empty, Many, One, Range},
};

// The Sequence that goes onto the stack is the size of an single item, as
// that's the biggest thing in it.
#[derive(Debug, Clone, PartialEq)]
pub enum Sequence {
    Empty(Empty),
    One(One),
    Many(Many),
    Range(Range),
}

impl From<Range> for Sequence {
    fn from(inner: Range) -> Self {
        Self::Range(inner)
    }
}

// a static assertion to ensure that Sequence never grows in size
#[cfg(target_arch = "x86_64")]
static_assertions::assert_eq_size!(Sequence, [u8; 24]);

impl Default for Sequence {
    fn default() -> Self {
        Self::Empty(Empty {})
    }
}

impl Sequence {
    /// Check whether the sequence is empty
    pub fn is_empty(&self) -> bool {
        match self {
            Sequence::Empty(inner) => inner.is_empty(),
            Sequence::One(inner) => inner.is_empty(),
            Sequence::Many(inner) => inner.is_empty(),
            Sequence::Range(inner) => inner.is_empty(),
        }
    }

    /// Get the sequence length
    pub fn len(&self) -> usize {
        match self {
            Sequence::Empty(inner) => inner.len(),
            Sequence::One(inner) => inner.len(),
            Sequence::Many(inner) => inner.len(),
            Sequence::Range(inner) => inner.len(),
        }
    }

    /// Get an item in the index, if it exists
    pub fn get(&self, index: usize) -> Option<Item> {
        match self {
            Sequence::Empty(inner) => inner.get(index),
            Sequence::One(inner) => inner.get(index),
            Sequence::Many(inner) => inner.get(index),
            Sequence::Range(inner) => inner.get(index),
        }
    }

    /// Get a single item from the sequence, if it only contains one item
    ///
    /// Otherwise you get a type error.
    pub fn one(self) -> error::Result<Item> {
        match self {
            Sequence::Empty(inner) => inner.one(),
            Sequence::One(inner) => inner.one(),
            Sequence::Many(inner) => inner.one(),
            Sequence::Range(inner) => inner.one(),
        }
    }

    /// Get a optional item from the sequence
    ///
    /// If it contains more than one item, you get a type error.
    pub fn option(self) -> error::Result<Option<Item>> {
        match self {
            Sequence::Empty(inner) => inner.option(),
            Sequence::One(inner) => inner.option(),
            Sequence::Many(inner) => inner.option(),
            Sequence::Range(inner) => inner.option(),
        }
    }

    /// Get the items from the sequence as an iterator
    pub fn iter(&self) -> BoxedItemIter {
        match self {
            Sequence::Empty(inner) => Box::new(inner.iter()),
            Sequence::One(inner) => Box::new(inner.iter()),
            Sequence::Many(inner) => Box::new(inner.iter()),
            Sequence::Range(inner) => Box::new(inner.iter()),
        }
    }

    /// Effective boolean value
    pub fn effective_boolean_value(&self) -> error::Result<bool> {
        match self {
            Sequence::Empty(inner) => inner.effective_boolean_value(),
            Sequence::One(inner) => inner.effective_boolean_value(),
            Sequence::Many(inner) => inner.effective_boolean_value(),
            Sequence::Range(inner) => inner.effective_boolean_value(),
        }
    }

    /// String value
    pub fn string_value(&self, xot: &xot::Xot) -> error::Result<String> {
        match self {
            Sequence::Empty(inner) => inner.string_value(xot),
            Sequence::One(inner) => inner.string_value(xot),
            Sequence::Many(inner) => inner.string_value(xot),
            Sequence::Range(inner) => inner.string_value(xot),
        }
    }

    /// Iterator over the nodes in the sequence
    ///
    /// An error is returned for items that are not a node.
    pub fn nodes<'a>(&'a self) -> Box<dyn Iterator<Item = error::Result<xot::Node>> + 'a> {
        match self {
            Sequence::Empty(inner) => Box::new(inner.nodes()),
            Sequence::One(inner) => Box::new(inner.nodes()),
            Sequence::Many(inner) => Box::new(inner.nodes()),
            Sequence::Range(inner) => Box::new(inner.nodes()),
        }
    }

    /// Iterator for the atomized values in the sequence
    pub fn atomized<'a>(
        &'a self,
        xot: &'a xot::Xot,
    ) -> Box<dyn Iterator<Item = error::Result<atomic::Atomic>> + 'a> {
        match self {
            Sequence::Empty(inner) => Box::new(inner.atomized(xot)),
            Sequence::One(inner) => Box::new(inner.atomized(xot)),
            Sequence::Many(inner) => Box::new(inner.atomized(xot)),
            Sequence::Range(inner) => Box::new(inner.atomized(xot)),
        }
    }

    /// Get just one atomized value from the sequence
    ///
    /// If there are less or more, you get a type error.
    pub fn atomized_one(&self, xot: &xot::Xot) -> error::Result<atomic::Atomic> {
        match self {
            Sequence::Empty(inner) => inner.atomized_one(xot),
            Sequence::One(inner) => inner.atomized_one(xot),
            Sequence::Many(inner) => inner.atomized_one(xot),
            Sequence::Range(inner) => inner.atomized_one(xot),
        }
    }

    /// Get an optional atomized value from the sequence
    ///
    /// If there are more than one, you get a type error.
    pub fn atomized_option(&self, xot: &xot::Xot) -> error::Result<Option<atomic::Atomic>> {
        match self {
            Sequence::Empty(inner) => inner.atomized_option(xot),
            Sequence::One(inner) => inner.atomized_option(xot),
            Sequence::Many(inner) => inner.atomized_option(xot),
            Sequence::Range(inner) => inner.atomized_option(xot),
        }
    }

    /// Is used internally by the library macro.
    pub(crate) fn unboxed_atomized<'a, T: 'a>(
        &'a self,
        xot: &'a xot::Xot,
        extract: impl Fn(atomic::Atomic) -> error::Result<T> + 'a,
    ) -> Box<dyn Iterator<Item = error::Result<T>> + 'a> {
        match self {
            Sequence::Empty(inner) => Box::new(inner.unboxed_atomized(xot, extract)),
            Sequence::One(inner) => Box::new(inner.unboxed_atomized(xot, extract)),
            Sequence::Many(inner) => Box::new(inner.unboxed_atomized(xot, extract)),
            Sequence::Range(inner) => Box::new(inner.unboxed_atomized(xot, extract)),
        }
    }

    /// Iterator over the XPath maps in the sequence
    ///
    /// An error is returned for items that are not a map.
    pub fn map_iter<'a>(&'a self) -> Box<dyn Iterator<Item = error::Result<function::Map>> + 'a> {
        match self {
            Sequence::Empty(inner) => Box::new(inner.map_iter()),
            Sequence::One(inner) => Box::new(inner.map_iter()),
            Sequence::Many(inner) => Box::new(inner.map_iter()),
            Sequence::Range(inner) => Box::new(inner.map_iter()),
        }
    }

    /// Iterator over the XPath arrays in the sequence
    ///
    /// An error is returned for items that are not an array.
    pub fn array_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = error::Result<function::Array>> + 'a> {
        match self {
            Sequence::Empty(inner) => Box::new(inner.array_iter()),
            Sequence::One(inner) => Box::new(inner.array_iter()),
            Sequence::Many(inner) => Box::new(inner.array_iter()),
            Sequence::Range(inner) => Box::new(inner.array_iter()),
        }
    }

    /// Oterator over elements nodes in the sequence
    ///
    /// An error is returned for items that are not an element.
    pub fn elements<'a>(
        &'a self,
        xot: &'a xot::Xot,
    ) -> error::Result<Box<dyn Iterator<Item = error::Result<xot::Node>> + 'a>> {
        match self {
            Sequence::Empty(inner) => Ok(Box::new(inner.elements(xot)?)),
            Sequence::One(inner) => Ok(Box::new(inner.elements(xot)?)),
            Sequence::Many(inner) => Ok(Box::new(inner.elements(xot)?)),
            Sequence::Range(inner) => Ok(Box::new(inner.elements(xot)?)),
        }
    }

    /// Create an XPath array from this sequence.
    pub fn to_array(&self) -> error::Result<function::Array> {
        match self {
            Sequence::Empty(inner) => inner.to_array(),
            Sequence::One(inner) => inner.to_array(),
            Sequence::Many(inner) => inner.to_array(),
            Sequence::Range(inner) => inner.to_array(),
        }
    }

    pub(crate) fn general_comparison<O>(
        &self,
        other: &Self,
        op: O,
        context: &context::DynamicContext,
        xot: &xot::Xot,
    ) -> error::Result<bool>
    where
        O: AtomicCompare,
    {
        match (self, other) {
            (Sequence::Empty(_a), Sequence::Empty(_b)) => Ok(false),
            (Sequence::Empty(_a), Sequence::One(_b)) => Ok(false),
            (Sequence::Empty(_a), Sequence::Many(_b)) => Ok(false),
            (Sequence::Empty(_a), Sequence::Range(_b)) => Ok(false),
            (Sequence::One(_a), Sequence::Empty(_b)) => Ok(false),
            (Sequence::One(a), Sequence::One(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::One(a), Sequence::Many(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::One(a), Sequence::Range(b)) => {
                if let Item::Atomic(atomic::Atomic::Integer(_, i)) = a.item() {
                    Ok(b.general_comparison_integer(i, O::value()))
                } else {
                    a.general_comparison(b, op, context, xot)
                }
            }
            (Sequence::Many(_a), Sequence::Empty(_b)) => Ok(false),
            (Sequence::Many(a), Sequence::One(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::Many(a), Sequence::Many(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::Many(a), Sequence::Range(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::Range(_a), Sequence::Empty(_b)) => Ok(false),
            (Sequence::Range(a), Sequence::One(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::Range(a), Sequence::Many(b)) => a.general_comparison(b, op, context, xot),
            (Sequence::Range(a), Sequence::Range(b)) => a.general_comparison(b, op, context, xot),
        }
    }

    pub(crate) fn value_compare<O>(
        &self,
        other: &Self,
        op: O,
        collation: &Collation,
        timezone: chrono::FixedOffset,
        xot: &Xot,
    ) -> error::Result<bool>
    where
        O: AtomicCompare,
    {
        match (self, other) {
            (Sequence::Empty(a), Sequence::Empty(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Empty(a), Sequence::One(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Empty(a), Sequence::Many(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Empty(a), Sequence::Range(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::One(a), Sequence::Empty(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::One(a), Sequence::One(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::One(a), Sequence::Many(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::One(a), Sequence::Range(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Many(a), Sequence::Empty(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Many(a), Sequence::One(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Many(a), Sequence::Many(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Many(a), Sequence::Range(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Range(a), Sequence::Empty(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Range(a), Sequence::One(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Range(a), Sequence::Many(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
            (Sequence::Range(a), Sequence::Range(b)) => {
                a.value_compare(b, op, collation, timezone, xot)
            }
        }
    }

    pub(crate) fn one_node(&self) -> error::Result<xot::Node> {
        match self {
            Sequence::Empty(inner) => inner.one_node(),
            Sequence::One(inner) => inner.one_node(),
            Sequence::Many(inner) => inner.one_node(),
            Sequence::Range(inner) => inner.one_node(),
        }
    }

    pub(crate) fn is(&self, other: &Self) -> error::Result<bool> {
        match (self, other) {
            (Sequence::Empty(a), Sequence::Empty(b)) => a.is(b),
            (Sequence::Empty(a), Sequence::One(b)) => a.is(b),
            (Sequence::Empty(a), Sequence::Many(b)) => a.is(b),
            (Sequence::Empty(a), Sequence::Range(b)) => a.is(b),
            (Sequence::One(a), Sequence::Empty(b)) => a.is(b),
            (Sequence::One(a), Sequence::One(b)) => a.is(b),
            (Sequence::One(a), Sequence::Many(b)) => a.is(b),
            (Sequence::One(a), Sequence::Range(b)) => a.is(b),
            (Sequence::Many(a), Sequence::Empty(b)) => a.is(b),
            (Sequence::Many(a), Sequence::One(b)) => a.is(b),
            (Sequence::Many(a), Sequence::Many(b)) => a.is(b),
            (Sequence::Many(a), Sequence::Range(b)) => a.is(b),
            (Sequence::Range(a), Sequence::Empty(b)) => a.is(b),
            (Sequence::Range(a), Sequence::One(b)) => a.is(b),
            (Sequence::Range(a), Sequence::Many(b)) => a.is(b),
            (Sequence::Range(a), Sequence::Range(b)) => a.is(b),
        }
    }

    pub(crate) fn precedes(
        &self,
        other: &Self,
        annotations: xml::DocumentOrderAccess,
    ) -> error::Result<bool> {
        match (self, other) {
            (Sequence::Empty(a), Sequence::Empty(b)) => a.precedes(b, annotations),
            (Sequence::Empty(a), Sequence::One(b)) => a.precedes(b, annotations),
            (Sequence::Empty(a), Sequence::Many(b)) => a.precedes(b, annotations),
            (Sequence::Empty(a), Sequence::Range(b)) => a.precedes(b, annotations),
            (Sequence::One(a), Sequence::Empty(b)) => a.precedes(b, annotations),
            (Sequence::One(a), Sequence::One(b)) => a.precedes(b, annotations),
            (Sequence::One(a), Sequence::Many(b)) => a.precedes(b, annotations),
            (Sequence::One(a), Sequence::Range(b)) => a.precedes(b, annotations),
            (Sequence::Many(a), Sequence::Empty(b)) => a.precedes(b, annotations),
            (Sequence::Many(a), Sequence::One(b)) => a.precedes(b, annotations),
            (Sequence::Many(a), Sequence::Many(b)) => a.precedes(b, annotations),
            (Sequence::Many(a), Sequence::Range(b)) => a.precedes(b, annotations),
            (Sequence::Range(a), Sequence::Empty(b)) => a.precedes(b, annotations),
            (Sequence::Range(a), Sequence::One(b)) => a.precedes(b, annotations),
            (Sequence::Range(a), Sequence::Many(b)) => a.precedes(b, annotations),
            (Sequence::Range(a), Sequence::Range(b)) => a.precedes(b, annotations),
        }
    }

    pub(crate) fn follows(
        &self,
        other: &Self,
        annotations: xml::DocumentOrderAccess,
    ) -> error::Result<bool> {
        match (self, other) {
            (Sequence::Empty(a), Sequence::Empty(b)) => a.follows(b, annotations),
            (Sequence::Empty(a), Sequence::One(b)) => a.follows(b, annotations),
            (Sequence::Empty(a), Sequence::Many(b)) => a.follows(b, annotations),
            (Sequence::Empty(a), Sequence::Range(b)) => a.follows(b, annotations),
            (Sequence::One(a), Sequence::Empty(b)) => a.follows(b, annotations),
            (Sequence::One(a), Sequence::One(b)) => a.follows(b, annotations),
            (Sequence::One(a), Sequence::Many(b)) => a.follows(b, annotations),
            (Sequence::One(a), Sequence::Range(b)) => a.follows(b, annotations),
            (Sequence::Many(a), Sequence::Empty(b)) => a.follows(b, annotations),
            (Sequence::Many(a), Sequence::One(b)) => a.follows(b, annotations),
            (Sequence::Many(a), Sequence::Many(b)) => a.follows(b, annotations),
            (Sequence::Many(a), Sequence::Range(b)) => a.follows(b, annotations),
            (Sequence::Range(a), Sequence::Empty(b)) => a.follows(b, annotations),
            (Sequence::Range(a), Sequence::One(b)) => a.follows(b, annotations),
            (Sequence::Range(a), Sequence::Many(b)) => a.follows(b, annotations),
            (Sequence::Range(a), Sequence::Range(b)) => a.follows(b, annotations),
        }
    }
}