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
//! The low-level parsers for date, datetime, duration and time.
//!
//! The top-level functions [`date`][`crate::date`], [`datetime`][`crate::datetime`],
//! [`duration`][`crate::duration`] and [`time`][`crate::time`]
//! provide convenient wrappers around the low-level parsers,
//! but throw away leftover input on success.
//!
//! Using the low-level functions provided here allows to recover leftover input
//! or to combine these parsers with other parser combinators.

use core::str;

use nom::{
    branch::alt,
    bytes::complete::{tag, take_while, take_while_m_n},
    character::complete::one_of,
    character::is_digit,
    combinator::{map, map_res, not, opt},
    error::Error,
    sequence::{preceded, separated_pair, terminated, tuple},
    Err, IResult,
};

use crate::{Date, DateTime, Duration, Time};

#[cfg(test)]
mod tests;

// UTILITY

fn take_digits(i: &[u8]) -> IResult<&[u8], u32> {
    let (i, digits) = take_while(is_digit)(i)?;

    if digits.is_empty() {
        return Err(Err::Error(Error::new(i, nom::error::ErrorKind::Eof)));
    }

    let s = str::from_utf8(digits).expect("Invalid data, expected UTF-8 string");
    let res = s
        .parse()
        .expect("Invalid string, expected ASCII representation of a number");

    Ok((i, res))
}

fn take_n_digits(i: &[u8], n: usize) -> IResult<&[u8], u32> {
    let (i, digits) = take_while_m_n(n, n, is_digit)(i)?;

    let s = str::from_utf8(digits).expect("Invalid data, expected UTF-8 string");
    let res = s
        .parse()
        .expect("Invalid string, expected ASCII representation of a number");

    Ok((i, res))
}

fn n_digit_in_range(
    i: &[u8],
    n: usize,
    range: impl core::ops::RangeBounds<u32>,
) -> IResult<&[u8], u32> {
    let (new_i, number) = take_n_digits(i, n)?;

    if range.contains(&number) {
        Ok((new_i, number))
    } else {
        Err(Err::Error(Error::new(i, nom::error::ErrorKind::Eof)))
    }
}

fn sign(i: &[u8]) -> IResult<&[u8], i32> {
    map(alt((tag(b"-"), tag(b"+"))), |s: &[u8]| match s {
        b"-" => -1,
        _ => 1,
    })(i)
}

// DATE

// [+/-]YYYY
fn date_year(i: &[u8]) -> IResult<&[u8], i32> {
    // The sign is optional, but defaults to `+`
    map(
        tuple((
            opt(sign),               // [+/-]
            |i| take_n_digits(i, 4), // year
        )),
        |(s, year)| s.unwrap_or(1) * year as i32,
    )(i)
}

// MM
fn date_month(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 2, 1..=12)
}

// DD
fn date_day(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 2, 1..=31)
}

// WW
fn date_week(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 2, 1..=52)
}

fn date_week_day(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 1, 1..=7)
}

// ordinal DDD
fn date_ord_day(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 3, 1..=366)
}

// YYYY-MM-DD
fn date_ymd(i: &[u8]) -> IResult<&[u8], Date> {
    map(
        tuple((
            date_year,      // YYYY
            opt(tag(b"-")), // -
            date_month,     // MM
            opt(tag(b"-")), // -
            date_day,       //DD
        )),
        |(year, _, month, _, day)| Date::YMD { year, month, day },
    )(i)
}

// YYYY-DDD
fn date_ordinal(i: &[u8]) -> IResult<&[u8], Date> {
    map(
        separated_pair(date_year, opt(tag(b"-")), date_ord_day),
        |(year, ddd)| Date::Ordinal { year, ddd },
    )(i)
}

// YYYY-"W"WW-D
fn date_iso_week(i: &[u8]) -> IResult<&[u8], Date> {
    map(
        tuple((
            date_year,                          // y
            tuple((opt(tag(b"-")), tag(b"W"))), // [-]W
            date_week,                          // w
            opt(tag(b"-")),                     // [-]
            date_week_day,                      // d
        )),
        |(year, _, ww, _, d)| Date::Week { year, ww, d },
    )(i)
}

/// Parses a date string.
///
/// See [`date`][`crate::date`] for the supported formats.
pub fn parse_date(i: &[u8]) -> IResult<&[u8], Date> {
    alt((date_ymd, date_iso_week, date_ordinal))(i)
}

// TIME

// HH
fn time_hour(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 2, 0..=24)
}

// MM
fn time_minute(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 2, 0..=59)
}

// SS
fn time_second(i: &[u8]) -> IResult<&[u8], u32> {
    n_digit_in_range(i, 2, 0..=60)
}

// Converts the fractional part if-any of a number of seconds to milliseconds
// truncating towards zero if there are more than three digits.
// e.g. "" -> 0, "1" -> 100, "12" -> 120, "123" -> 123, "1234" -> 123
fn fraction_millisecond(i: &[u8]) -> IResult<&[u8], u32> {
    let (i, mut digits) = take_while(is_digit)(i)?;
    let mut l = digits.len();
    if l > 3 {
        digits = digits.get(0..3).unwrap();
    }
    let mut result = 0;
    if l > 0 {
        let digits = str::from_utf8(digits).unwrap(); // This can't panic, `digits` will only include digits.
        result = digits.parse().unwrap();
    }
    while l < 3 {
        result *= 10;
        l += 1;
    }
    Ok((i, result))
}

/// Parses a time string.
///
/// See [`time`][`crate::time`] for the supported formats.
// HH:MM:[SS][.(m*)][(Z|+...|-...)]
pub fn parse_time(i: &[u8]) -> IResult<&[u8], Time> {
    map(
        tuple((
            time_hour,                                         // HH
            opt(tag(b":")),                                    // :
            time_minute,                                       // MM
            opt(preceded(opt(tag(b":")), time_second)),        // [SS]
            opt(preceded(one_of(",."), fraction_millisecond)), // [.(m*)]
            opt(alt((timezone_hour, timezone_utc))),           // [(Z|+...|-...)]
        )),
        |(h, _, m, s, ms, z)| {
            let (tz_offset_hours, tz_offset_minutes) = z.unwrap_or((0, 0));

            Time {
                hour: h,
                minute: m,
                second: s.unwrap_or(0),
                millisecond: ms.unwrap_or(0),
                tz_offset_hours,
                tz_offset_minutes,
            }
        },
    )(i)
}

fn timezone_hour(i: &[u8]) -> IResult<&[u8], (i32, i32)> {
    map(
        tuple((sign, time_hour, opt(preceded(opt(tag(b":")), time_minute)))),
        |(s, h, m)| (s * (h as i32), s * (m.unwrap_or(0) as i32)),
    )(i)
}

fn timezone_utc(i: &[u8]) -> IResult<&[u8], (i32, i32)> {
    map(tag(b"Z"), |_| (0, 0))(i)
}

/// Parses a datetime string.
///
/// See [`datetime`][`crate::datetime`] for supported formats.
// Full ISO8601 datetime
pub fn parse_datetime(i: &[u8]) -> IResult<&[u8], DateTime> {
    map(
        separated_pair(parse_date, tag(b"T"), parse_time),
        |(d, t)| DateTime { date: d, time: t },
    )(i)
}

// DURATION

///    dur-year          = 1*DIGIT "Y" [dur-month]
fn duration_year(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"Y"))(i)
}

///    dur-month         = 1*DIGIT "M" [dur-day]
fn duration_month(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"M"))(i)
}

///    dur-week          = 1*DIGIT "W"
fn duration_week(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"W"))(i)
}

//    dur-day           = 1*DIGIT "D"
fn duration_day(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"D"))(i)
}

///    dur-hour          = 1*DIGIT "H" [dur-minute]
///    dur-time          = "T" (dur-hour / dur-minute / dur-second)
fn duration_hour(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"H"))(i)
}

///    dur-minute        = 1*DIGIT "M" [dur-second]
fn duration_minute(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"M"))(i)
}

////    dur-second        = 1*DIGIT "S"
fn duration_second(i: &[u8]) -> IResult<&[u8], u32> {
    terminated(take_digits, tag(b"S"))(i)
}

///    dur-second-ext    = 1*DIGIT (,|.) 1*DIGIT "S"
fn duration_second_and_millisecond(i: &[u8]) -> IResult<&[u8], (u32, u32)> {
    alt((
        // no milliseconds
        map(duration_second, |m| (m, 0)),
        terminated(
            // with milliseconds
            separated_pair(take_digits, one_of(",."), fraction_millisecond),
            tag(b"S"),
        ),
    ))(i)
}

fn duration_time(i: &[u8]) -> IResult<&[u8], (u32, u32, u32, u32)> {
    map(
        tuple((
            opt(duration_hour),
            opt(duration_minute),
            opt(duration_second_and_millisecond),
        )),
        |(h, m, s)| {
            let (s, ms) = s.unwrap_or((0, 0));

            (h.unwrap_or(0), m.unwrap_or(0), s, ms)
        },
    )(i)
}

fn duration_ymdhms(i: &[u8]) -> IResult<&[u8], Duration> {
    map_res(
        preceded(
            tag(b"P"),
            tuple((
                opt(duration_year),
                opt(duration_month),
                opt(duration_day),
                opt(preceded(tag(b"T"), duration_time)),
            )),
        ),
        |(y, mo, d, time)| {
            // at least one element must be present for a valid duration representation
            if y.is_none() && mo.is_none() && d.is_none() && time.is_none() {
                return Err(Err::Error((i, nom::error::ErrorKind::Eof)));
            }

            let (h, mi, s, ms) = time.unwrap_or((0, 0, 0, 0));

            Ok(Duration::YMDHMS {
                year: y.unwrap_or(0),
                month: mo.unwrap_or(0),
                day: d.unwrap_or(0),
                hour: h,
                minute: mi,
                second: s,
                millisecond: ms,
            })
        },
    )(i)
}

fn duration_weeks(i: &[u8]) -> IResult<&[u8], Duration> {
    map(preceded(tag(b"P"), duration_week), Duration::Weeks)(i)
}

// YYYY, no sign
fn duration_datetime_year(i: &[u8]) -> IResult<&[u8], u32> {
    take_n_digits(i, 4)
}

fn duration_datetime(i: &[u8]) -> IResult<&[u8], Duration> {
    map(
        preceded(
            tuple((tag(b"P"), not(sign))),
            tuple((
                duration_datetime_year,
                opt(tag(b"-")),
                date_month,
                opt(tag(b"-")),
                date_day,
                tag(b"T"),
                parse_time,
            )),
        ),
        |(year, _, month, _, day, _, t)| Duration::YMDHMS {
            year,
            month,
            day,
            hour: t.hour,
            minute: t.minute,
            second: t.second,
            millisecond: t.millisecond,
        },
    )(i)
}

/// Parses a duration string.
///
/// See [`duration`][`crate::duration`] for supported formats.
pub fn parse_duration(i: &[u8]) -> IResult<&[u8], Duration> {
    alt((duration_ymdhms, duration_weeks, duration_datetime))(i)
}