relate-core 1.0.0

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
406
407
408
409
410
411
412
413
414
//! Instant-pattern matching: the collected [`Fields`], the token matcher,
//! and the per-field input readers.

use chrono::Weekday;
use chrono_tz::Tz;

use crate::Result;

use super::super::format::{MONTH_NAMES, WEEKDAY_NAMES};
use super::super::range_err;
use super::super::zone::{parse_offset, zone_from_short_name};
use super::token::{FieldKind, Token};
use super::{as_u32, nanos_from_fraction, read_literal, read_number, Digits};

/// The era a pattern's `G` field named; a year-of-era under `Bc` is
/// shifted by one (44 BC is proleptic year -43).
#[derive(Default, Clone, Copy, PartialEq, Eq)]
pub(super) enum Era {
    #[default]
    Ad,
    Bc,
}

/// The field values an instant pattern collected, before the target kind
/// decides which of them it uses (CIP-228: a character outside the kind's
/// checkmarks is parsed but ignored).
#[derive(Default, Clone)]
pub(super) struct Fields {
    pub(super) year: Option<i64>,
    pub(super) week_year: Option<i64>,
    pub(super) month: Option<u32>,
    pub(super) day: Option<u32>,
    pub(super) day_of_year: Option<u32>,
    pub(super) week: Option<u32>,
    pub(super) quarter: Option<u32>,
    pub(super) day_of_week: Option<u32>,
    pub(super) day_of_week_name: Option<Weekday>,
    pub(super) aligned_day_of_week: Option<u32>,
    pub(super) modified_julian: Option<i64>,
    pub(super) hour_of_day: Option<u32>,
    pub(super) clock_hour: Option<u32>,
    pub(super) hour_of_am_pm: Option<u32>,
    pub(super) minute: Option<u32>,
    pub(super) second: Option<u32>,
    pub(super) nano: Option<u32>,
    pub(super) milli_of_day: Option<i64>,
    pub(super) nano_of_day: Option<i64>,
    pub(super) am_pm: Option<bool>,
    /// The `y` spelling is year-of-era; `u` is the proleptic year.
    pub(super) year_of_era: bool,
    /// A parsed `BC`/`BCE` era shifts a year-of-era by one.
    pub(super) era: Era,
    pub(super) offset: Option<i32>,
    pub(super) zone: Option<Tz>,
}

impl Fields {
    /// The proleptic year: a `BC`/`BCE` era shifts a year-of-era (`y`) by
    /// one (44 BC is year -43); a proleptic `u` year is already absolute.
    pub(super) fn proleptic_year(&self) -> Option<i64> {
        self.year.map(|year| {
            if self.year_of_era && self.era == Era::Bc {
                1 - year
            } else {
                year
            }
        })
    }

    /// Whether the pattern named a time-of-day field — derived from the
    /// collected fields rather than tracked beside them.
    pub(super) fn has_time(&self) -> bool {
        self.hour_of_day.is_some()
            || self.clock_hour.is_some()
            || self.hour_of_am_pm.is_some()
            || self.minute.is_some()
            || self.second.is_some()
            || self.nano.is_some()
            || self.milli_of_day.is_some()
            || self.nano_of_day.is_some()
    }
}

/// `Ok(None)` is a shape mismatch (the input does not fit the pattern);
/// `Err` is a value the grammar parsed but cannot represent (a component
/// that overflows `i64`), which must not read as a mismatch.
pub(super) fn match_tokens(
    tokens: &[Token],
    input: &str,
    pos: usize,
    fields: &mut Fields,
) -> Result<Option<usize>> {
    let mut pos = pos;
    for token in tokens {
        match token {
            Token::Literal(text) => {
                if read_literal(input, &mut pos, text).is_none() {
                    return Ok(None);
                }
            }
            Token::Optional(inner) => {
                let mut trial = fields.clone();
                if let Some(end) = match_tokens(inner, input, pos, &mut trial)? {
                    *fields = trial;
                    pos = end;
                }
            }
            Token::Field {
                kind,
                letter,
                count,
                pad,
            } => {
                let Some(end) = read_field(*kind, *letter, *count, *pad, input, pos, fields)?
                else {
                    return Ok(None);
                };
                pos = end;
            }
        }
    }
    Ok(Some(pos))
}

/// Read a numeric field into a `u32` slot: `Ok(false)` is a shape mismatch.
fn take_u32(slot: &mut Option<u32>, input: &str, pos: &mut usize, count: usize) -> Result<bool> {
    let Some(raw) = read_number(input, pos, field_width(count), false)? else {
        return Ok(false);
    };
    *slot = Some(as_u32(raw.value)?);
    Ok(true)
}

/// Read a numeric field into an `i64` slot, with an optional leading sign.
fn take_i64(
    slot: &mut Option<i64>,
    input: &str,
    pos: &mut usize,
    count: usize,
    sign: bool,
) -> Result<bool> {
    let Some(raw) = read_number(input, pos, field_width(count), sign)? else {
        return Ok(false);
    };
    *slot = Some(raw.value);
    Ok(true)
}

/// Read one field; `Ok(None)` is a shape mismatch. Every arm evaluates to
/// whether it matched, and the position is only reported on a match.
fn read_field(
    kind: FieldKind,
    letter: char,
    count: usize,
    pad: usize,
    input: &str,
    mut pos: usize,
    fields: &mut Fields,
) -> Result<Option<usize>> {
    let p = &mut pos;
    let matched = match kind {
        FieldKind::Year => {
            let raw = if pad == 0 {
                read_number(input, p, field_width(count), false)?
            } else {
                read_padded_number(input, p, pad)?
            };
            match raw {
                Some(raw) => {
                    fields.year_of_era = letter == 'y';
                    fields.year = Some(if count == 2 {
                        2000 + raw.value.rem_euclid(100)
                    } else {
                        raw.value
                    });
                    true
                }
                None => false,
            }
        }
        FieldKind::WeekYear => take_i64(&mut fields.week_year, input, p, count, false)?,
        FieldKind::Month if count >= 3 => {
            fields.month = read_alpha(input, p).and_then(month_from_name);
            fields.month.is_some()
        }
        FieldKind::Month => take_u32(&mut fields.month, input, p, count)?,
        FieldKind::Day => take_u32(&mut fields.day, input, p, count)?,
        FieldKind::DayOfYear => take_u32(&mut fields.day_of_year, input, p, count)?,
        FieldKind::Week => take_u32(&mut fields.week, input, p, count)?,
        // Defined but does not contribute to the kinds this engine
        // constructs; still consumes its input.
        FieldKind::WeekOfMonth => read_number(input, p, field_width(count), false)?.is_some(),
        FieldKind::Quarter if count >= 4 => {
            fields.quarter = read_quarter(input, p);
            fields.quarter.is_some()
        }
        FieldKind::Quarter => take_u32(&mut fields.quarter, input, p, count)?,
        FieldKind::DayOfWeekNum => take_u32(&mut fields.day_of_week, input, p, count)?,
        FieldKind::DayOfWeekName => {
            fields.day_of_week_name = read_alpha(input, p).and_then(weekday_from_name);
            fields.day_of_week_name.is_some()
        }
        FieldKind::AlignedDayOfWeek => take_u32(&mut fields.aligned_day_of_week, input, p, count)?,
        FieldKind::ModifiedJulianDay => {
            take_i64(&mut fields.modified_julian, input, p, count, true)?
        }
        FieldKind::HourOfDay => take_u32(&mut fields.hour_of_day, input, p, count)?,
        FieldKind::ClockHour => take_u32(&mut fields.clock_hour, input, p, count)?,
        FieldKind::HourOfAmPm => take_u32(&mut fields.hour_of_am_pm, input, p, count)?,
        FieldKind::Minute => take_u32(&mut fields.minute, input, p, count)?,
        FieldKind::Second => take_u32(&mut fields.second, input, p, count)?,
        FieldKind::Fraction => {
            let width = count.min(9);
            match read_number(input, p, Some(width), false)? {
                Some(raw) => {
                    fields.nano = Some(as_u32(nanos_from_fraction(raw.value, raw.digits)?)?);
                    true
                }
                None => false,
            }
        }
        FieldKind::NanoOfSecond => take_u32(&mut fields.nano, input, p, count)?,
        FieldKind::MilliOfDay => take_i64(&mut fields.milli_of_day, input, p, count, true)?,
        FieldKind::NanoOfDay => take_i64(&mut fields.nano_of_day, input, p, count, true)?,
        FieldKind::AmPm => {
            fields.am_pm = read_alpha(input, p).and_then(|marker| {
                match marker.to_ascii_uppercase().as_str() {
                    "AM" => Some(false),
                    "PM" => Some(true),
                    _ => None,
                }
            });
            fields.am_pm.is_some()
        }
        FieldKind::PeriodOfDay => {
            // A day-period phrase is informational; it consumes the rest of
            // the input and contributes nothing to the value.
            *p = input.len();
            true
        }
        FieldKind::Era => {
            let era =
                read_alpha(input, p).and_then(|era| match era.to_ascii_uppercase().as_str() {
                    "AD" | "CE" => Some(Era::Ad),
                    "BC" | "BCE" => Some(Era::Bc),
                    _ => None,
                });
            if let Some(era) = era {
                fields.era = era;
            }
            era.is_some()
        }
        FieldKind::ZoneId => read_zone_id(input, p, fields),
        FieldKind::ZoneName => read_zone_name(input, p, fields),
        FieldKind::OffsetFractional | FieldKind::OffsetZ => {
            fields.offset = read_offset(input, p);
            fields.offset.is_some()
        }
        FieldKind::OffsetLocalized => read_localized_offset(input, p, fields),
    };
    Ok(matched.then_some(pos))
}

/// `VV` is a zone id: an IANA region, or a `ZoneOffset` id such as
/// `+02:00` (java.time's `ZoneId` admits both). Anything else does not
/// match this field.
fn read_zone_id(input: &str, pos: &mut usize, fields: &mut Fields) -> bool {
    let Some(name) = read_non_space(input, pos) else {
        return false;
    };
    if name.starts_with(['+', '-']) {
        let Ok(offset) = parse_offset(name) else {
            return false;
        };
        fields.offset = Some(offset);
        true
    } else {
        accept_zone(name, fields)
    }
}

/// Record `name` as the pattern's zone when it is an IANA id; `false`
/// (a shape mismatch) otherwise. The one zone-id acceptance rule for `VV`
/// and `z`.
fn accept_zone(name: &str, fields: &mut Fields) -> bool {
    let Ok(zone) = name.parse::<Tz>() else {
        return false;
    };
    fields.zone = Some(zone);
    true
}

/// `z`: a short zone name, or a full IANA id.
fn read_zone_name(input: &str, pos: &mut usize, fields: &mut Fields) -> bool {
    let Some(name) = read_non_space(input, pos) else {
        return false;
    };
    accept_zone(zone_from_short_name(name).unwrap_or(name), fields)
}

/// `O` is the localized offset spelling: a `GMT`/`UTC` prefix and an
/// optional numeric offset (`GMT+2`, `UTC-08:00`).
fn read_localized_offset(input: &str, pos: &mut usize, fields: &mut Fields) -> bool {
    let Some(raw) = read_non_space(input, pos) else {
        return false;
    };
    let Some(rest) = raw.strip_prefix("GMT").or_else(|| raw.strip_prefix("UTC")) else {
        return false;
    };
    let offset = if rest.is_empty() {
        0
    } else {
        let Ok(offset) = parse_offset(rest) else {
            return false;
        };
        offset
    };
    fields.offset = Some(offset);
    true
}

/// The input width a field token consumes: a repeated letter run (`MM`,
/// `yyyy`) is fixed-width; a single letter consumes the whole digit run.
fn field_width(count: usize) -> Option<usize> {
    (count > 1).then_some(count)
}

/// A `p`-padded field: exactly `width` characters, leading spaces ignored.
fn read_padded_number(input: &str, pos: &mut usize, width: usize) -> Result<Option<Digits>> {
    let Some(text) = input.get(*pos..*pos + width) else {
        return Ok(None);
    };
    let trimmed = text.trim_start();
    if trimmed.is_empty() || !trimmed.bytes().all(|b| b.is_ascii_digit()) {
        return Ok(None);
    }
    let value = trimmed.parse().map_err(|_| range_err())?;
    *pos += width;
    Ok(Some(Digits {
        value,
        digits: trimmed.len(),
    }))
}

/// Take the longest run of characters `take` accepts; `None` when the run
/// is empty.
fn read_while<'a>(input: &'a str, pos: &mut usize, take: impl Fn(char) -> bool) -> Option<&'a str> {
    let start = *pos;
    while let Some(c) = input[*pos..].chars().next() {
        if !take(c) {
            break;
        }
        *pos += c.len_utf8();
    }
    (*pos > start).then(|| &input[start..*pos])
}

fn read_alpha<'a>(input: &'a str, pos: &mut usize) -> Option<&'a str> {
    read_while(input, pos, char::is_alphabetic)
}

fn read_non_space<'a>(input: &'a str, pos: &mut usize) -> Option<&'a str> {
    read_while(input, pos, |c| !c.is_whitespace())
}

/// `QQQQ`: a quarter ordinal plus the literal word `quarter`, e.g.
/// `2nd quarter`. The ordinal suffix is not inspected; the number is.
fn read_quarter(input: &str, pos: &mut usize) -> Option<u32> {
    let quarter: u32 = read_while(input, pos, |c| c.is_ascii_digit())?
        .parse()
        .ok()?;
    // The ordinal suffix is not inspected: it is consumed and dropped.
    let _ = read_alpha(input, pos);
    read_while(input, pos, |c| c == ' ')?;
    let word = input.get(*pos..)?.get(..7)?;
    if !word.eq_ignore_ascii_case("quarter") {
        return None;
    }
    *pos += 7;
    Some(quarter)
}

/// A `X`/`x`/`Z` zone offset: `Z` for UTC, otherwise a signed `HH`,
/// `HHMM`, `HH:MM` (optionally with seconds) run.
fn read_offset(input: &str, pos: &mut usize) -> Option<i32> {
    if matches!(input.as_bytes().get(*pos), Some(b'Z' | b'z')) {
        *pos += 1;
        return Some(0);
    }
    let start = *pos;
    if matches!(input.as_bytes().get(*pos), Some(b'+' | b'-')) {
        *pos += 1;
    }
    read_while(input, pos, |c| c.is_ascii_digit() || c == ':')?;
    parse_offset(input.get(start..*pos)?).ok()
}

/// Index of `name` in a full-name table, case-insensitively and accepting
/// the three-letter abbreviation (Java's `MMM`/`EEE` short forms) — the one
/// spelling of the name rule both tables read.
fn match_name(name: &str, table: &[&str]) -> Option<usize> {
    table.iter().position(|full| {
        full.eq_ignore_ascii_case(name) || (name.len() == 3 && full[..3].eq_ignore_ascii_case(name))
    })
}

fn month_from_name(name: &str) -> Option<u32> {
    match_name(name, &MONTH_NAMES).map(|index| index as u32 + 1)
}

fn weekday_from_name(name: &str) -> Option<Weekday> {
    let index = match_name(name, &WEEKDAY_NAMES)?;
    Weekday::try_from(u8::try_from(index).ok()?).ok()
}