logo
  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
434
435
436
437
438
439
440
441
442
443
444
//! Parsing for various types.

use crate::{
    error,
    format::{parse_fmt_string, well_known, FormatItem, Padding, Specifier},
    Format, UtcOffset, Weekday,
};
#[cfg(not(feature = "std"))]
use alloc::boxed::Box;
use core::{
    fmt::{self, Display, Formatter},
    num::{NonZeroU16, NonZeroU8},
    str::FromStr,
};

/// Helper type to avoid repeating the error type.
pub(crate) type ParseResult<T> = Result<T, Error>;

/// An error occurred while parsing.
#[cfg_attr(__time_02_supports_non_exhaustive, non_exhaustive)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum Error {
    /// The nanosecond present was not valid.
    InvalidNanosecond,
    /// The second present was not valid.
    InvalidSecond,
    /// The minute present was not valid.
    InvalidMinute,
    /// The hour present was not valid.
    InvalidHour,
    /// The AM/PM was not valid.
    InvalidAmPm,
    /// The month present was not valid.
    InvalidMonth,
    /// The year present was not valid.
    InvalidYear,
    /// The week present was not valid.
    InvalidWeek,
    /// The day of week present was not valid.
    InvalidDayOfWeek,
    /// The day of month present was not valid.
    InvalidDayOfMonth,
    /// The day of year present was not valid.
    InvalidDayOfYear,
    /// The UTC offset present was not valid.
    InvalidOffset,
    /// There was no character following a `%`.
    MissingFormatSpecifier,
    /// The character following `%` is not valid.
    InvalidFormatSpecifier(char),
    /// A character literal was expected to be present but was not.
    UnexpectedCharacter {
        /// The character that was expected to be present.
        expected: char,
        /// The character that was present in the string.
        actual: char,
    },
    /// The string ended, but there should be more content.
    UnexpectedEndOfString,
    /// There was not enough information provided to create the requested type.
    InsufficientInformation,
    /// A component was out of range.
    ComponentOutOfRange(Box<error::ComponentRange>),
    #[cfg(not(__time_02_supports_non_exhaustive))]
    #[doc(hidden)]
    __NonExhaustive,
}

impl From<error::ComponentRange> for Error {
    fn from(error: error::ComponentRange) -> Self {
        Error::ComponentOutOfRange(Box::new(error))
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        use Error::*;
        match self {
            InvalidNanosecond => f.write_str("invalid nanosecond"),
            InvalidSecond => f.write_str("invalid second"),
            InvalidMinute => f.write_str("invalid minute"),
            InvalidHour => f.write_str("invalid hour"),
            InvalidAmPm => f.write_str("invalid am/pm"),
            InvalidMonth => f.write_str("invalid month"),
            InvalidYear => f.write_str("invalid year"),
            InvalidWeek => f.write_str("invalid week"),
            InvalidDayOfWeek => f.write_str("invalid day of week"),
            InvalidDayOfMonth => f.write_str("invalid day of month"),
            InvalidDayOfYear => f.write_str("invalid day of year"),
            InvalidOffset => f.write_str("invalid offset"),
            MissingFormatSpecifier => f.write_str("missing format specifier after `%`"),
            InvalidFormatSpecifier(c) => write!(f, "invalid format specifier `{}` after `%`", c),
            UnexpectedCharacter { expected, actual } => {
                write!(f, "expected character `{}`, found `{}`", expected, actual)
            }
            UnexpectedEndOfString => f.write_str("unexpected end of string"),
            InsufficientInformation => {
                f.write_str("insufficient information provided to create the requested type")
            }
            ComponentOutOfRange(e) => write!(f, "{}", e),
            #[cfg(not(__time_02_supports_non_exhaustive))]
            __NonExhaustive => unreachable!(),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::ComponentOutOfRange(e) => Some(e.as_ref()),
            _ => None,
        }
    }
}

/// A value representing a time that is either "AM" or "PM".
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum AmPm {
    /// A time before noon.
    AM,
    /// A time at or after noon.
    PM,
}

/// All information gathered from parsing a provided string.
#[derive(Debug, Clone, Copy)]
pub(crate) struct ParsedItems {
    /// Year the ISO week belongs to.
    pub(crate) week_based_year: Option<i32>,
    /// The year the month, day, and ordinal day belong to.
    pub(crate) year: Option<i32>,
    /// One-indexed month number.
    pub(crate) month: Option<NonZeroU8>,
    /// Day of the month.
    pub(crate) day: Option<NonZeroU8>,
    /// Day of the week.
    pub(crate) weekday: Option<Weekday>,
    /// Day of the year.
    pub(crate) ordinal_day: Option<NonZeroU16>,
    /// ISO week within the year. Week 1 contains the year's first Thursday.
    pub(crate) iso_week: Option<NonZeroU8>,
    /// Week number, counted from the first Sunday. May be zero.
    pub(crate) sunday_week: Option<u8>,
    /// Week number, counted from the first Monday. May be zero.
    pub(crate) monday_week: Option<u8>,
    /// Hour in the 12-hour clock.
    pub(crate) hour_12: Option<NonZeroU8>,
    /// Hour in the 24-hour clock.
    pub(crate) hour_24: Option<u8>,
    /// Minute within the hour.
    pub(crate) minute: Option<u8>,
    /// Second within the minute.
    pub(crate) second: Option<u8>,
    /// Nanosecond within the second.
    pub(crate) nanosecond: Option<u32>,
    /// The UTC offset of the datetime.
    pub(crate) offset: Option<UtcOffset>,
    /// Whether the hour indicated is AM or PM.
    pub(crate) am_pm: Option<AmPm>,
}

impl ParsedItems {
    /// Create a new `ParsedItems` with nothing known.
    pub(crate) const fn new() -> Self {
        Self {
            week_based_year: None,
            year: None,
            month: None,
            day: None,
            weekday: None,
            ordinal_day: None,
            iso_week: None,
            sunday_week: None,
            monday_week: None,
            hour_12: None,
            hour_24: None,
            minute: None,
            second: None,
            nanosecond: None,
            offset: None,
            am_pm: None,
        }
    }
}

/// Attempt to consume the provided character.
pub(crate) fn try_consume_char(s: &mut &str, expected: char) -> ParseResult<()> {
    match s.char_indices().next() {
        Some((index, actual_char)) if actual_char == expected => {
            *s = &s[(index + actual_char.len_utf8())..];
            Ok(())
        }
        Some((_, actual)) => Err(Error::UnexpectedCharacter { expected, actual }),
        None => Err(Error::UnexpectedEndOfString),
    }
}

/// Attempt to consume the provided character, ignoring case.
pub(crate) fn try_consume_char_case_insensitive(s: &mut &str, expected: char) -> ParseResult<()> {
    match s.char_indices().next() {
        Some((index, actual_char)) if actual_char.eq_ignore_ascii_case(&expected) => {
            *s = &s[(index + actual_char.len_utf8())..];
            Ok(())
        }
        Some((_, actual)) => Err(Error::UnexpectedCharacter { expected, actual }),
        None => Err(Error::UnexpectedEndOfString),
    }
}

/// Attempt to consume the provided string.
pub(crate) fn try_consume_str(s: &mut &str, expected: &str) -> ParseResult<()> {
    if s.starts_with(expected) {
        *s = &s[expected.len()..];
        Ok(())
    } else {
        // Iterate through the characters, returning the error where differing.
        for c in expected.chars() {
            try_consume_char(s, c)?;
        }
        // TODO Find a way to allow the compiler to prove the following is not
        // necessary.
        unreachable!("The previous loop should always cause the function to return.");
    }
}

/// Attempt to find one of the strings provided, returning the first value.
pub(crate) fn try_consume_first_match<T: Copy>(
    s: &mut &str,
    opts: impl IntoIterator<Item = (impl AsRef<str>, T)>,
) -> Option<T> {
    opts.into_iter().find_map(|(expected, value)| {
        if s.starts_with(expected.as_ref()) {
            *s = &s[expected.as_ref().len()..];
            Some(value)
        } else {
            None
        }
    })
}

/// Attempt to consume a number of digits. Consumes the maximum amount possible
/// within the range provided.
pub(crate) fn try_consume_digits<T: FromStr>(
    s: &mut &str,
    min_digits: usize,
    max_digits: usize,
) -> Option<T> {
    // Determine how many digits the string starts with, up to the upper limit
    // of the range.
    let len = s
        .chars()
        .take(max_digits)
        .take_while(char::is_ascii_digit)
        .count();

    // We don't have enough digits.
    if len < min_digits {
        return None;
    }

    // Because we're only dealing with ASCII digits here, we know that the
    // length is equal to the number of bytes, as ASCII values are always one
    // byte in Unicode.
    let digits = &s[..len];
    *s = &s[len..];
    digits.parse::<T>().ok()
}

/// Attempt to consume an exact number of digits.
pub(crate) fn try_consume_exact_digits<T: FromStr>(
    s: &mut &str,
    num_digits: usize,
    padding: Padding,
) -> Option<T> {
    let pad_size = match padding {
        Padding::Space => consume_padding(s, padding, num_digits - 1),
        _ => 0,
    };

    if padding == Padding::None {
        try_consume_digits(s, 1, num_digits - pad_size)
    } else {
        // Ensure all the necessary characters are ASCII digits.
        if !s
            .chars()
            .take(num_digits - pad_size)
            .all(|c| c.is_ascii_digit())
        {
            return None;
        }

        // Ensure the string is long enough to perform the slicing.
        if (num_digits - pad_size) > s.len() {
            return None;
        }

        // Because we're only dealing with ASCII digits here, we know that the
        // length is equal to the number of bytes, as ASCII values are always one
        // byte in Unicode.
        let digits = &s[..(num_digits - pad_size)];
        *s = &s[(num_digits - pad_size)..];
        digits.parse::<T>().ok()
    }
}

/// Consume all leading padding up to the number of characters.
///
/// Returns the number of characters trimmed.
pub(crate) fn consume_padding(s: &mut &str, padding: Padding, max_chars: usize) -> usize {
    let pad_char = match padding {
        Padding::Space => ' ',
        Padding::Zero => '0',
        Padding::None => return 0,
    };

    let pad_width = s
        .chars()
        .take(max_chars)
        .take_while(|&c| c == pad_char)
        .count();
    *s = &s[pad_width..];
    pad_width
}

/// Attempt to parse the string with the provided format, returning a struct
/// containing all information found.
#[allow(clippy::too_many_lines)]
pub(crate) fn parse(s: &str, format: &Format) -> ParseResult<ParsedItems> {
    use super::{date, offset, time};

    // Make a copy of the provided string, letting us mutate as necessary.
    let mut s = <&str>::clone(&s);

    let mut items = ParsedItems::new();

    /// Parse the provided specifier with the given parameters.
    macro_rules! parse {
        ($module:ident :: $specifier_fn:ident $( ( $($params:expr),* ) )?) => {
            $module::$specifier_fn(&mut items, &mut s, $( $($params),* )?)?
        };
    }

    macro_rules! parse_char {
        ($c:literal) => {
            try_consume_char(&mut s, $c)?
        };
    }

    match &format {
        Format::Rfc3339 => well_known::rfc3339::parse(&mut items, &mut s)?,
        Format::Custom(format) => {
            for item in parse_fmt_string(format) {
                match item {
                    FormatItem::Literal(expected) => try_consume_str(&mut s, expected)?,
                    FormatItem::Specifier(specifier) => {
                        use Specifier::*;
                        match specifier {
                            a => parse!(date::parse_a),
                            A => parse!(date::parse_A),
                            b => parse!(date::parse_b),
                            B => parse!(date::parse_B),
                            c => {
                                parse!(date::parse_a);
                                parse_char!(' ');
                                parse!(date::parse_b);
                                parse_char!(' ');
                                parse!(date::parse_d(Padding::None));
                                parse_char!(' ');
                                parse!(time::parse_H(Padding::None));
                                parse_char!(':');
                                parse!(time::parse_M(Padding::Zero));
                                parse_char!(':');
                                parse!(time::parse_S(Padding::Zero));
                                parse_char!(' ');
                                parse!(date::parse_Y(Padding::None));
                            }
                            C { padding } => parse!(date::parse_C(padding)),
                            d { padding } => parse!(date::parse_d(padding)),
                            D => {
                                parse!(date::parse_m(Padding::None));
                                parse_char!('/');
                                parse!(date::parse_d(Padding::Zero));
                                parse_char!('/');
                                parse!(date::parse_y(Padding::Zero));
                            }
                            F => {
                                parse!(date::parse_Y(Padding::None));
                                parse_char!('-');
                                parse!(date::parse_m(Padding::Zero));
                                parse_char!('-');
                                parse!(date::parse_d(Padding::Zero));
                            }
                            g { padding } => parse!(date::parse_g(padding)),
                            G { padding } => parse!(date::parse_G(padding)),
                            H { padding } => parse!(time::parse_H(padding)),
                            I { padding } => parse!(time::parse_I(padding)),
                            j { padding } => parse!(date::parse_j(padding)),
                            M { padding } => parse!(time::parse_M(padding)),
                            m { padding } => parse!(date::parse_m(padding)),
                            N => parse!(time::parse_N),
                            p => parse!(time::parse_p),
                            P => parse!(time::parse_P),
                            r => {
                                parse!(time::parse_I(Padding::None));
                                parse_char!(':');
                                parse!(time::parse_M(Padding::Zero));
                                parse_char!(':');
                                parse!(time::parse_S(Padding::Zero));
                                parse_char!(' ');
                                parse!(time::parse_p);
                            }
                            R => {
                                parse!(time::parse_H(Padding::None));
                                parse_char!(':');
                                parse!(time::parse_M(Padding::Zero));
                            }
                            S { padding } => parse!(time::parse_S(padding)),
                            T => {
                                parse!(time::parse_H(Padding::None));
                                parse_char!(':');
                                parse!(time::parse_M(Padding::Zero));
                                parse_char!(':');
                                parse!(time::parse_S(Padding::Zero));
                            }
                            u => parse!(date::parse_u),
                            U { padding } => parse!(date::parse_U(padding)),
                            V { padding } => parse!(date::parse_V(padding)),
                            w => parse!(date::parse_w),
                            W { padding } => parse!(date::parse_W(padding)),
                            y { padding } => parse!(date::parse_y(padding)),
                            z => parse!(offset::parse_z),
                            Y { padding } => parse!(date::parse_Y(padding)),
                        }
                    }
                }
            }
        }
        #[cfg(not(__time_02_supports_non_exhaustive))]
        Format::__NonExhaustive => unreachable!(),
    }

    Ok(items)
}