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
427
428
429
430
431
432
433
use chrono::{Offset, TimeZone};

use crate::{atomic::Atomic, error};

pub(crate) trait EqWithDefaultOffset: ToDateTimeStamp {
    fn eq_with_default_offset(&self, other: &Self, default_offset: chrono::FixedOffset) -> bool {
        let self_date_time_stamp = self.to_date_time_stamp(default_offset);
        let other_date_time_stamp = other.to_date_time_stamp(default_offset);
        self_date_time_stamp == other_date_time_stamp
    }
}

pub(crate) trait OrdWithDefaultOffset: ToDateTimeStamp {
    fn cmp_with_default_offset(
        &self,
        other: &Self,
        default_offset: chrono::FixedOffset,
    ) -> std::cmp::Ordering {
        let self_date_time_stamp = self.to_date_time_stamp(default_offset);
        let other_date_time_stamp = other.to_date_time_stamp(default_offset);
        self_date_time_stamp.cmp(&other_date_time_stamp)
    }
}

pub(crate) trait ToDateTimeStamp {
    fn to_date_time_stamp(
        &self,
        default_offset: chrono::FixedOffset,
    ) -> chrono::DateTime<chrono::FixedOffset>;

    fn to_naive_date_time(&self, default_offset: chrono::FixedOffset) -> chrono::NaiveDateTime {
        self.to_date_time_stamp(default_offset).naive_utc()
    }
}

impl<T> EqWithDefaultOffset for T where T: ToDateTimeStamp {}
impl<T> OrdWithDefaultOffset for T where T: ToDateTimeStamp {}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct YearMonthDuration {
    pub(crate) months: i64,
}

impl YearMonthDuration {
    pub(crate) fn new(months: i64) -> Self {
        Self { months }
    }

    pub(crate) fn years(&self) -> i64 {
        self.months / 12
    }

    pub(crate) fn months(&self) -> i64 {
        self.months % 12
    }
}

impl From<YearMonthDuration> for Atomic {
    fn from(year_month_duration: YearMonthDuration) -> Self {
        Atomic::YearMonthDuration(year_month_duration)
    }
}

/// A Duration is a combination of a [`YearMonthDuration`]` and
/// [`chrono::Duration`].
///
/// It represents `xs:duration`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Duration {
    pub(crate) year_month: YearMonthDuration,
    pub(crate) day_time: chrono::Duration,
}

impl Duration {
    pub(crate) fn new(months: i64, day_time: chrono::Duration) -> Self {
        Self {
            year_month: YearMonthDuration { months },
            day_time,
        }
    }

    pub(crate) fn from_year_month(year_month_duration: YearMonthDuration) -> Self {
        Self {
            year_month: year_month_duration,
            day_time: chrono::Duration::zero(),
        }
    }

    pub(crate) fn from_day_time(duration: chrono::Duration) -> Self {
        Self {
            year_month: YearMonthDuration { months: 0 },
            day_time: duration,
        }
    }
}

impl From<Duration> for Atomic {
    fn from(duration: Duration) -> Self {
        Atomic::Duration(duration.into())
    }
}

impl TryFrom<Atomic> for Duration {
    type Error = error::Error;

    fn try_from(a: Atomic) -> Result<Self, Self::Error> {
        match a {
            Atomic::Duration(d) => Ok(d.as_ref().clone()),
            Atomic::YearMonthDuration(d) => Ok(Duration::from_year_month(d)),
            Atomic::DayTimeDuration(d) => Ok(Duration::from_day_time(*d)),
            _ => Err(error::Error::XPTY0004),
        }
    }
}

impl TryFrom<Atomic> for chrono::Duration {
    type Error = error::Error;

    fn try_from(a: Atomic) -> Result<Self, Self::Error> {
        match a {
            Atomic::DayTimeDuration(d) => Ok(*d.as_ref()),
            _ => Err(error::Error::XPTY0004),
        }
    }
}

/// A `NaiveDateTimeWithOffset` is a combination of a [`chrono::NaiveDateTime`] and
/// an optional [`chrono::FixedOffset`].
///
/// It represents `xs:dateTime`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NaiveDateTimeWithOffset {
    pub(crate) date_time: chrono::NaiveDateTime,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl From<NaiveDateTimeWithOffset> for chrono::DateTime<chrono::FixedOffset> {
    fn from(naive_date_time_with_offset: NaiveDateTimeWithOffset) -> Self {
        let offset = naive_date_time_with_offset
            .offset
            .unwrap_or_else(|| chrono::offset::Utc.fix());
        chrono::DateTime::from_naive_utc_and_offset(naive_date_time_with_offset.date_time, offset)
    }
}

impl From<chrono::DateTime<chrono::FixedOffset>> for NaiveDateTimeWithOffset {
    fn from(date_time: chrono::DateTime<chrono::FixedOffset>) -> Self {
        NaiveDateTimeWithOffset::new(date_time.naive_local(), Some(*date_time.offset()))
    }
}

impl From<NaiveDateTimeWithOffset> for Atomic {
    fn from(date_time: NaiveDateTimeWithOffset) -> Self {
        Atomic::DateTime(date_time.into())
    }
}

impl TryFrom<Atomic> for NaiveDateTimeWithOffset {
    type Error = error::Error;

    fn try_from(a: Atomic) -> Result<Self, Self::Error> {
        match a {
            Atomic::DateTime(d) => Ok(d.as_ref().clone()),
            Atomic::DateTimeStamp(d) => Ok((*d.as_ref()).into()),
            _ => Err(error::Error::XPTY0004),
        }
    }
}

impl ToDateTimeStamp for NaiveDateTimeWithOffset {
    fn to_date_time_stamp(
        &self,
        default_offset: chrono::FixedOffset,
    ) -> chrono::DateTime<chrono::FixedOffset> {
        let offset = self.offset.unwrap_or(default_offset);
        offset.from_local_datetime(&self.date_time).unwrap()
    }
}

impl NaiveDateTimeWithOffset {
    pub(crate) fn new(
        date_time: chrono::NaiveDateTime,
        offset: Option<chrono::FixedOffset>,
    ) -> Self {
        Self { date_time, offset }
    }
}

/// A `NaiveTimeWithOffset` is a combination of a [`chrono::NaiveTime`] and
/// an optional [`chrono::FixedOffset`].
///
/// It represents `xs:time`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NaiveTimeWithOffset {
    pub(crate) time: chrono::NaiveTime,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl TryFrom<Atomic> for NaiveTimeWithOffset {
    type Error = error::Error;

    fn try_from(a: Atomic) -> Result<Self, Self::Error> {
        match a {
            Atomic::Time(d) => Ok(d.as_ref().clone()),
            _ => Err(error::Error::XPTY0004),
        }
    }
}

impl NaiveTimeWithOffset {
    pub(crate) fn new(time: chrono::NaiveTime, offset: Option<chrono::FixedOffset>) -> Self {
        Self { time, offset }
    }
}

impl ToDateTimeStamp for NaiveTimeWithOffset {
    fn to_date_time_stamp(
        &self,
        default_offset: chrono::FixedOffset,
    ) -> chrono::DateTime<chrono::FixedOffset> {
        let offset = self.offset.unwrap_or(default_offset);
        // https://www.w3.org/TR/xpath-functions-31/#func-subtract-times
        let date_time = chrono::NaiveDate::from_ymd_opt(1972, 12, 31)
            .unwrap()
            .and_time(self.time);
        offset.from_local_datetime(&date_time).unwrap()
    }
}

impl From<NaiveTimeWithOffset> for Atomic {
    fn from(time: NaiveTimeWithOffset) -> Self {
        Atomic::Time(time.into())
    }
}

/// A `NaiveDateWithOffset` is a combination of a [`chrono::NaiveDate`] and
/// an optional [`chrono::FixedOffset`].
///
/// It represents `xs:date`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NaiveDateWithOffset {
    pub(crate) date: chrono::NaiveDate,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl TryFrom<Atomic> for NaiveDateWithOffset {
    type Error = error::Error;

    fn try_from(a: Atomic) -> Result<Self, Self::Error> {
        match a {
            Atomic::Date(d) => Ok(d.as_ref().clone()),
            _ => Err(error::Error::XPTY0004),
        }
    }
}

impl NaiveDateWithOffset {
    pub(crate) fn new(date: chrono::NaiveDate, offset: Option<chrono::FixedOffset>) -> Self {
        Self { date, offset }
    }
}

impl ToDateTimeStamp for NaiveDateWithOffset {
    fn to_date_time_stamp(
        &self,
        default_offset: chrono::FixedOffset,
    ) -> chrono::DateTime<chrono::FixedOffset> {
        let offset = self.offset.unwrap_or(default_offset);
        let date_time = self.date.and_hms_opt(0, 0, 0).unwrap();
        offset.from_local_datetime(&date_time).unwrap()
    }
}

impl From<NaiveDateWithOffset> for Atomic {
    fn from(date: NaiveDateWithOffset) -> Self {
        Atomic::Date(date.into())
    }
}

/// A `GYearMonth` is a combination of a year and a month, and an optional
/// [`chrono::FixedOffset`].
///
/// It represents `xs:gYearMonth`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GYearMonth {
    pub(crate) year: i32,
    pub(crate) month: u32,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl GYearMonth {
    pub(crate) fn new(year: i32, month: u32, offset: Option<chrono::FixedOffset>) -> Self {
        Self {
            year,
            month,
            offset,
        }
    }
}

impl From<GYearMonth> for Atomic {
    fn from(g_year_month: GYearMonth) -> Self {
        Atomic::GYearMonth(g_year_month.into())
    }
}

/// A `GYear` is a combination of a year and an optional
/// [`chrono::FixedOffset`].
///
/// It represents `xs:gYear`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GYear {
    pub(crate) year: i32,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl GYear {
    pub(crate) fn new(year: i32, offset: Option<chrono::FixedOffset>) -> Self {
        Self { year, offset }
    }
}

impl From<GYear> for Atomic {
    fn from(g_year: GYear) -> Self {
        Atomic::GYear(g_year.into())
    }
}

/// A `GMonthDay` is a combination of a month and a day, and an optional
/// [`chrono::FixedOffset`].
///
/// It represents `xs:gMonthDay`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GMonthDay {
    pub(crate) month: u32,
    pub(crate) day: u32,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl GMonthDay {
    pub(crate) fn new(month: u32, day: u32, offset: Option<chrono::FixedOffset>) -> Self {
        Self { month, day, offset }
    }
}

impl From<GMonthDay> for Atomic {
    fn from(g_month_day: GMonthDay) -> Self {
        Atomic::GMonthDay(g_month_day.into())
    }
}

/// A `GDay` is a combination of a day and an optional
/// [`chrono::FixedOffset`].
///
/// It represents `xs:gDay`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GDay {
    pub(crate) day: u32,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl GDay {
    pub(crate) fn new(day: u32, offset: Option<chrono::FixedOffset>) -> Self {
        Self { day, offset }
    }
}

impl From<GDay> for Atomic {
    fn from(g_day: GDay) -> Self {
        Atomic::GDay(g_day.into())
    }
}

/// A `GMonth` is a combination of a month and an optional
/// [`chrono::FixedOffset`].
///
/// It represents `xs:gMonth`
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GMonth {
    pub(crate) month: u32,
    pub(crate) offset: Option<chrono::FixedOffset>,
}

impl GMonth {
    pub(crate) fn new(month: u32, offset: Option<chrono::FixedOffset>) -> Self {
        Self { month, offset }
    }
}

impl From<GMonth> for Atomic {
    fn from(g_month: GMonth) -> Self {
        Atomic::GMonth(g_month.into())
    }
}

impl From<chrono::Duration> for Atomic {
    fn from(duration: chrono::Duration) -> Self {
        Atomic::DayTimeDuration(duration.into())
    }
}

impl From<chrono::DateTime<chrono::FixedOffset>> for Atomic {
    fn from(date_time: chrono::DateTime<chrono::FixedOffset>) -> Self {
        Atomic::DateTimeStamp(date_time.into())
    }
}

#[cfg(test)]
mod tests {
    use crate::atomic::{AtomicCompare, OpGt};

    use super::*;

    #[test]
    fn test_compare_dates() {
        let a_date = NaiveDateWithOffset::new(
            chrono::NaiveDate::from_ymd_opt(2004, 12, 25).unwrap(),
            Some(chrono::offset::Utc.fix()),
        );
        let b_date = NaiveDateWithOffset::new(
            chrono::NaiveDate::from_ymd_opt(2004, 12, 25).unwrap(),
            Some(chrono::FixedOffset::east_opt(60 * 60 * 7).unwrap()),
        );

        let a: Atomic = Atomic::Date(a_date.into());
        let b: Atomic = Atomic::Date(b_date.into());

        assert!(
            OpGt::atomic_compare(a.clone(), b.clone(), str::cmp, chrono::offset::Utc.fix())
                .unwrap()
        );
    }
}