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
//! Error detail implementation for [`Timestamp`] parsing.
//!
//! [`Timestamp`]s can fail to parse for a few reasons, detailed by
//! [`TimestampParseErrorType`]'s variants. Of note is the
//! [`TimestampParseErrorType::Range`] variant detailing the field that was out
//! of range. This can occur when, for example, a given [hour] is 24 but the
//! acceptable range is from 0 to 23, inclusively.
//!
//! [`Timestamp`]: super::Timestamp
//! [hour]: RangeField::Hour

use super::constant::{self, Month};
use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
};

/// Reason that an ISO 8601 format couldn't be parsed.
#[derive(Debug)]
pub struct TimestampParseError {
    /// Type of error that occurred.
    kind: TimestampParseErrorType,
}

impl TimestampParseError {
    /// Error that was caused by the datetime being of an improper format.
    pub(super) const FORMAT: TimestampParseError = TimestampParseError {
        kind: TimestampParseErrorType::Format,
    };

    /// Error that was caused by the datetime parsing to a unix timestamp of
    /// zero.
    pub(super) const ZERO: TimestampParseError = TimestampParseError {
        kind: TimestampParseErrorType::Zero,
    };

    /// Immutable reference to the type of error that occurred.
    #[must_use = "retrieving the type has no effect if left unused"]
    pub const fn kind(&self) -> &TimestampParseErrorType {
        &self.kind
    }

    /// Consume the error, returning the source error if there is any.
    #[allow(clippy::unused_self)]
    #[must_use = "consuming the error and retrieving the source has no effect if left unused"]
    pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
        None
    }

    /// Consume the error, returning the owned error type and the source error.
    #[must_use = "consuming the error into its parts has no effect if left unused"]
    pub fn into_parts(
        self,
    ) -> (
        TimestampParseErrorType,
        Option<Box<dyn Error + Send + Sync>>,
    ) {
        (self.kind, None)
    }

    /// Create a new error with a year range issue.
    pub(super) const fn year(value: u16) -> Self {
        Self {
            kind: TimestampParseErrorType::Range {
                field: RangeField::Year { value },
            },
        }
    }

    /// Create a new error with a month range issue.
    pub(super) const fn month(value: u8) -> Self {
        Self {
            kind: TimestampParseErrorType::Range {
                field: RangeField::Month { value },
            },
        }
    }

    /// Create a new error with a day range issue.
    pub(super) const fn day(year: u16, month: u8, day: u8) -> Self {
        Self {
            kind: TimestampParseErrorType::Range {
                field: RangeField::Day {
                    value: day,
                    month,
                    year,
                },
            },
        }
    }

    /// Create a new error with an hour range issue.
    pub(super) const fn hour(value: u8) -> Self {
        Self {
            kind: TimestampParseErrorType::Range {
                field: RangeField::Hour { value },
            },
        }
    }

    /// Create a new error with a minute range issue.
    pub(super) const fn minute(value: u8) -> Self {
        Self {
            kind: TimestampParseErrorType::Range {
                field: RangeField::Minute { value },
            },
        }
    }

    /// Create a new error with a second range issue.
    pub(super) const fn second(value: u8) -> Self {
        Self {
            kind: TimestampParseErrorType::Range {
                field: RangeField::Second { value },
            },
        }
    }
}

impl Display for TimestampParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        match &self.kind {
            TimestampParseErrorType::Format => {
                f.write_str("provided value is not in an iso 8601 format")
            }
            TimestampParseErrorType::Range { field } => match field {
                RangeField::Day { year, month, value } => {
                    if *value == 0 {
                        return f.write_str("day of 0 provided, but days are 1-indexed");
                    }

                    let is_leap_year = constant::is_leap_year(u64::from(*year));
                    let days_in_month = Month::new(*month as u8)
                        .map(|month| month.days(is_leap_year))
                        .unwrap_or_default();

                    f.write_str("day of ")?;
                    Display::fmt(&value, f)?;
                    f.write_str(" provided but month ")?;
                    Display::fmt(&month, f)?;
                    f.write_str(" only has ")?;
                    Display::fmt(&days_in_month, f)?;

                    f.write_str(" days")
                }
                RangeField::Hour { value } => {
                    f.write_str("hour is ")?;
                    Display::fmt(&value, f)?;

                    f.write_str(" but must be at most 23")
                }
                RangeField::Minute { value } => {
                    f.write_str("minute is ")?;
                    Display::fmt(&value, f)?;

                    f.write_str(" but must be at most 59")
                }
                RangeField::Month { value } => {
                    f.write_str("month is ")?;
                    Display::fmt(&value, f)?;

                    f.write_str(" but must be 1-12, inclusively")
                }
                RangeField::Second { value } => {
                    f.write_str("second is ")?;
                    Display::fmt(&value, f)?;

                    f.write_str(" but must at most 59, inclusively")
                }
                RangeField::Year { value } => {
                    f.write_str("year is ")?;
                    Display::fmt(&value, f)?;

                    f.write_str(" but must be at least 2010")
                }
            },
            TimestampParseErrorType::Zero => {
                f.write_str("datetime value is equivalent to zero; must be non-zero")
            }
        }
    }
}

impl Error for TimestampParseError {}

/// Type of [`TimestampParseError`] that occurred.
#[derive(Debug)]
pub enum TimestampParseErrorType {
    /// Format of the input datetime is invalid.
    ///
    /// A datetime can take two forms: with microseconds and without
    /// microseconds.
    Format,
    /// Value of a field is not in an acceptable range.
    Range {
        /// Field that is out of range and why.
        field: RangeField,
    },
    /// Provided datetime value is equivalent to zero.
    ///
    /// Datetime values must be non-zero.
    Zero,
}

/// Field that is out of range.
///
/// During parsing of timestamps the values of fields - such as the hour or day
/// portion of a timestamp - are validated. If one is outside of the accepted
/// range then the function is short-circuited an an error is returned.
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub enum RangeField {
    /// Day field is out of the acceptable range.
    ///
    /// The acceptable ranges are:
    ///
    /// - [1, 31] in the case of January, March, May, July, August, October, and
    /// December;
    /// - [1, 30] in the case of April, June, September, and November;
    /// - [1, 29] in the case of February on a leap year;
    /// - [1, 28] in the case of February on a year that is not a leap year.
    Day {
        /// Year.
        ///
        /// Can be used in combination with [`month`] to determine the range of
        /// acceptable days.
        ///
        /// [`month`]: Self::Day::month
        year: u16,
        /// Month of the year.
        ///
        /// Can be used in combination with [`year`] to determine the range of
        /// acceptable days.
        ///
        /// [`year`]: Self::Day::year
        month: u8,
        /// Provided value.
        value: u8,
    },
    /// Hour field is out of the acceptable range.
    ///
    /// The acceptable range is [0, 23].
    Hour {
        /// Provided value.
        value: u8,
    },
    /// Minute field is out of the acceptable range.
    ///
    /// The acceptable range is [0, 59].
    Minute {
        /// Provided value.
        value: u8,
    },
    /// Month field is out of the acceptable range.
    ///
    /// The acceptable range is [1, 12].
    Month {
        /// Provided value.
        value: u8,
    },
    /// Second field is out of the acceptable range.
    ///
    /// The acceptable range is [0, 59], or [0, 60] in the case of a leap
    /// second.
    Second {
        /// Provided value.
        value: u8,
    },
    /// Year field is out of the acceptable range.
    ///
    /// The acceptable range is [2010, 2038].
    Year {
        /// Provided value.
        value: u16,
    },
}

#[cfg(test)]
mod tests {
    use super::{RangeField, TimestampParseError, TimestampParseErrorType};
    use static_assertions::assert_impl_all;
    use std::{error::Error, fmt::Debug, hash::Hash};

    assert_impl_all!(RangeField: Clone, Debug, Eq, Hash, PartialEq, Send, Sync);
    assert_impl_all!(TimestampParseErrorType: Debug, Send, Sync);
    assert_impl_all!(TimestampParseError: Error, Send, Sync);
}