relate-core 1.0.1

Core values, errors, and IDs for Relate
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
//! The component-field map policy's unit suite: the date-form classifier, the
//! prerequisite and cascade rules, the source-field vocabulary, and the two
//! epoch spellings' exclusions.

use super::clock::ClockKind;
use super::field_rules::*;
use super::Temporal;
use crate::{Error, Result, Value};

fn map(entries: &[(&str, Value)]) -> Fields {
    entries
        .iter()
        .map(|(key, value)| ((*key).to_string(), value.clone()))
        .collect()
}

fn class<T>(result: Result<T>) -> &'static str {
    match result {
        Ok(_) => "ok",
        Err(Error::ArgumentError(_)) => "ArgumentError",
        Err(Error::Type(_)) => "TypeError",
        Err(other) => panic!("unexpected error class: {other:?}"),
    }
}

fn date(entries: &[(&str, Value)]) -> &'static str {
    class(validate(ClockKind::Date, &map(entries)))
}

fn localdatetime(entries: &[(&str, Value)]) -> &'static str {
    class(validate(ClockKind::LocalDateTime, &map(entries)))
}

fn datetime(entries: &[(&str, Value)]) -> &'static str {
    class(validate(ClockKind::DateTime, &map(entries)))
}

fn localtime(entries: &[(&str, Value)]) -> &'static str {
    class(validate(ClockKind::LocalTime, &map(entries)))
}

/// The four date forms are mutually exclusive (22G05), while each form's
/// own spelling is legal.
#[test]
fn date_forms_are_exclusive() {
    for entries in [
        &[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("week", Value::Int(11)),
        ][..],
        &[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("dayOfWeek", Value::Int(6)),
        ],
        &[
            ("year", Value::Int(1984)),
            ("ordinalDay", Value::Int(173)),
            ("day", Value::Int(2)),
        ],
        &[
            ("year", Value::Int(1984)),
            ("quarter", Value::Int(2)),
            ("day", Value::Int(11)),
        ],
        &[
            ("year", Value::Int(1984)),
            ("week", Value::Int(2)),
            ("quarter", Value::Int(11)),
        ],
    ] {
        assert_eq!(date(entries), "TypeError", "{entries:?}");
    }
    for entries in [
        &[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("day", Value::Int(11)),
        ][..],
        &[("year", Value::Int(1984)), ("ordinalDay", Value::Int(173))],
        &[
            ("year", Value::Int(1984)),
            ("week", Value::Int(2)),
            ("dayOfWeek", Value::Int(3)),
        ],
        &[
            ("year", Value::Int(1984)),
            ("quarter", Value::Int(2)),
            ("dayOfQuarter", Value::Int(10)),
        ],
    ] {
        assert_eq!(date(entries), "ok", "{entries:?}");
    }
}

/// The classifier answers one form per field set — the answer the reader
/// consumes, so a null-valued field is absent for both.
#[test]
fn the_classifier_names_the_selected_form() {
    let form = |entries: &[(&str, Value)]| {
        validate(ClockKind::Date, &map(entries))
            .expect("valid date map")
            .form
    };
    assert_eq!(
        form(&[("year", Value::Int(1984)), ("month", Value::Int(2))]),
        Some(DateForm::Calendar)
    );
    assert_eq!(
        form(&[
            ("year", Value::Int(1984)),
            ("week", Value::Int(2)),
            ("dayOfWeek", Value::Int(3)),
        ]),
        Some(DateForm::Week)
    );
    assert_eq!(
        form(&[("year", Value::Int(1984)), ("ordinalDay", Value::Int(173))]),
        Some(DateForm::Ordinal)
    );
    assert_eq!(
        form(&[
            ("year", Value::Int(1984)),
            ("quarter", Value::Int(2)),
            ("dayOfQuarter", Value::Int(10)),
        ]),
        Some(DateForm::Quarter)
    );
    // `year` alone is the default calendar form.
    assert_eq!(
        form(&[("year", Value::Int(1984))]),
        Some(DateForm::Calendar)
    );
    // A null-valued form field is absent, not a selection: the answer is
    // the source-supplied date, so no form shifts it.
    let source = Value::Temporal(Temporal::Date(
        chrono::NaiveDate::from_ymd_opt(2021, 1, 1).unwrap(),
    ));
    assert_eq!(
        form(&[("date", source), ("week", Value::Null)]),
        None,
        "a null `week` must not select the week form"
    );
}

/// A component named without its prerequisite is 22G05, except a missing
/// `year`, which is the 22007 family.
#[test]
fn component_prerequisites_are_typed() {
    assert_eq!(
        date(&[("year", Value::Int(1984)), ("day", Value::Int(11))]),
        "TypeError"
    );
    assert_eq!(
        date(&[("year", Value::Int(1984)), ("dayOfWeek", Value::Int(3))]),
        "TypeError"
    );
    assert_eq!(
        date(&[("year", Value::Int(1984)), ("dayOfQuarter", Value::Int(3))]),
        "TypeError"
    );
    assert_eq!(date(&[("month", Value::Int(2))]), "ArgumentError");
    assert_eq!(date(&[("week", Value::Int(2))]), "ArgumentError");
}

/// A time field makes the date form complete: the default calendar form
/// needs month and day, the other forms their own companion.
#[test]
fn a_time_field_requires_a_complete_date() {
    assert_eq!(
        localdatetime(&[("year", Value::Int(1984)), ("hour", Value::Int(11))]),
        "TypeError"
    );
    assert_eq!(
        localdatetime(&[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("hour", Value::Int(11)),
        ]),
        "TypeError"
    );
    assert_eq!(
        localdatetime(&[
            ("year", Value::Int(1984)),
            ("week", Value::Int(2)),
            ("hour", Value::Int(11)),
        ]),
        "TypeError"
    );
    assert_eq!(
        datetime(&[
            ("year", Value::Int(1984)),
            ("quarter", Value::Int(2)),
            ("hour", Value::Int(11)),
        ]),
        "TypeError"
    );
    assert_eq!(
        localdatetime(&[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("day", Value::Int(11)),
            ("hour", Value::Int(11)),
        ]),
        "ok"
    );
    // A `date` source supplies the missing components.
    let source = Value::Temporal(Temporal::Date(
        chrono::NaiveDate::from_ymd_opt(1984, 2, 11).unwrap(),
    ));
    assert_eq!(
        datetime(&[("date", source), ("hour", Value::Int(11))]),
        "ok"
    );
}

/// The time cascade: hour before minute before second before subsecond.
#[test]
fn time_fields_need_the_coarser_component() {
    assert_eq!(localtime(&[("minute", Value::Int(30))]), "TypeError");
    assert_eq!(
        localtime(&[("hour", Value::Int(12)), ("second", Value::Int(45))]),
        "TypeError"
    );
    assert_eq!(
        localtime(&[("hour", Value::Int(12)), ("minute", Value::Int(31))]),
        "ok"
    );
    assert_eq!(
        localtime(&[("hour", Value::Int(12)), ("millisecond", Value::Int(645))]),
        "TypeError"
    );
    assert_eq!(
        localtime(&[
            ("hour", Value::Int(12)),
            ("minute", Value::Int(31)),
            ("nanosecond", Value::Int(1)),
        ]),
        "TypeError"
    );
}

/// Unknown or kind-incompatible keys are the 22G05 `Not supported`
/// family, even with a null value; an empty map is 22007.
#[test]
fn vocabulary_and_emptiness() {
    assert_eq!(
        date(&[("year", Value::Int(1984)), ("foo", Value::Int(1))]),
        "TypeError"
    );
    assert_eq!(date(&[("time", Value::Null)]), "TypeError");
    assert_eq!(localtime(&[("year", Value::Null)]), "TypeError");
    assert_eq!(date(&[]), "ArgumentError");
    assert_eq!(localtime(&[]), "ArgumentError");
}

/// A time zone beside other fields is rejected on the zoneless kinds and
/// legal on the zoned ones.
#[test]
fn timezone_is_a_clock_reading_on_zoneless_kinds() {
    assert_eq!(
        date(&[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("day", Value::Int(11)),
            ("timezone", Value::String("+01:00".into())),
        ]),
        "TypeError"
    );
    assert_eq!(
        localdatetime(&[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("day", Value::Int(11)),
            ("timezone", Value::String("+01:00".into())),
        ]),
        "TypeError"
    );
    assert_eq!(
        datetime(&[
            ("year", Value::Int(1984)),
            ("month", Value::Int(2)),
            ("day", Value::Int(11)),
            ("timezone", Value::String("+01:00".into())),
        ]),
        "ok"
    );
}

/// The source fields must carry the part they select, and `datetime`
/// excludes `date`/`time`. A present non-temporal source value is the
/// same typed rejection — never a silently absent field.
#[test]
fn source_fields_select_a_part() {
    let date_value = Value::Temporal(Temporal::Date(
        chrono::NaiveDate::from_ymd_opt(1984, 2, 11).unwrap(),
    ));
    let time_value = Value::Temporal(Temporal::LocalTime(
        chrono::NaiveTime::from_hms_opt(12, 30, 0).unwrap(),
    ));
    assert_eq!(localtime(&[("time", date_value.clone())]), "TypeError");
    assert_eq!(date(&[("date", time_value.clone())]), "TypeError");
    assert_eq!(
        localdatetime(&[
            ("datetime", date_value.clone()),
            ("date", date_value.clone()),
        ]),
        "ArgumentError"
    );
    assert_eq!(localdatetime(&[("datetime", date_value)]), "TypeError");
    assert_eq!(localdatetime(&[("time", time_value)]), "ArgumentError");
    // A non-temporal source value is a typed rejection, not an absent
    // field the constructor defaults over.
    assert_eq!(date(&[("date", Value::Int(42))]), "TypeError");
    assert_eq!(localdatetime(&[("time", Value::Int(42))]), "TypeError");
    assert_eq!(
        datetime(&[
            ("year", Value::Int(2020)),
            ("month", Value::Int(1)),
            ("day", Value::Int(1)),
            ("time", Value::Int(42)),
        ]),
        "TypeError"
    );
}

/// The day-of-week value range.
#[test]
fn day_of_week_is_one_to_seven() {
    for day in [0, -1, 8] {
        assert_eq!(
            date(&[
                ("year", Value::Int(1984)),
                ("week", Value::Int(2)),
                ("dayOfWeek", Value::Int(day)),
            ]),
            "ArgumentError",
            "dayOfWeek {day}"
        );
    }
}

/// The epoch form's exclusions are 22007, and only `datetime` accepts
/// the form at all. Both spellings of each field are the same field, so
/// the exclusion and the selection see them alike.
#[test]
fn epoch_form_exclusions() {
    let epoch =
        |entries: &[(&str, Value)]| class(validate_epoch(ClockKind::DateTime, &map(entries)));
    assert_eq!(
        epoch(&[
            ("epochSeconds", Value::Int(1)),
            ("epochMillis", Value::Int(2)),
        ]),
        "ArgumentError"
    );
    // The lowercase spelling is the same field: mixing it with the
    // camelCase sibling of the other field is the same exclusion.
    assert_eq!(
        epoch(&[
            ("epochSeconds", Value::Int(1)),
            ("epochmillis", Value::Int(2)),
        ]),
        "ArgumentError"
    );
    let at = Value::Temporal(Temporal::DateTime {
        utc: chrono::NaiveDate::from_ymd_opt(2025, 1, 1)
            .unwrap()
            .and_hms_opt(0, 0, 0)
            .unwrap(),
        offset: 0,
        zone: Some("Z".into()),
    });
    assert_eq!(
        epoch(&[("epochSeconds", Value::Int(1)), ("datetime", at)]),
        "ArgumentError"
    );
    assert_eq!(
        class(validate_epoch(
            ClockKind::Date,
            &map(&[("epochSeconds", Value::Int(1))])
        )),
        "TypeError"
    );
}

/// Every accepted epoch spelling selects its own field, so the reader
/// reads exactly the field the validator selected.
#[test]
fn epoch_selection_names_the_present_spelling() {
    let select = |entries: &[(&str, Value)]| {
        validate_epoch(ClockKind::DateTime, &map(entries)).expect("valid epoch map")
    };
    assert_eq!(
        select(&[("epochseconds", Value::Int(1))]),
        EpochField::Seconds
    );
    assert_eq!(
        select(&[("epochMillis", Value::Int(1))]),
        EpochField::Millis
    );
    assert_eq!(EpochField::Seconds.split(5), (5, 0));
    assert_eq!(EpochField::Millis.split(1_234), (1, 234_000_000));
}