tai_time/
errors.rs

1//! Error types.
2
3use core::fmt;
4
5/// The error type returned when the result of a conversion to or from a
6/// [`TaiTime`](crate::TaiTime) is outside the representable range, or the
7/// conversion would cause the result to overflow.
8#[derive(Debug, PartialEq, Eq, Clone, Copy)]
9#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
10#[cfg_attr(feature = "defmt", derive(defmt::Format))]
11pub struct OutOfRangeError(pub(crate) ());
12
13impl fmt::Display for OutOfRangeError {
14    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
15        "timestamp out of representable range".fmt(fmt)
16    }
17}
18
19#[cfg(feature = "std")]
20impl std::error::Error for OutOfRangeError {}
21
22/// The error type returned when date-time components are invalid or correspond
23/// to a timestamp outside the representable range.
24#[derive(Debug, PartialEq, Eq, Clone, Copy)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26#[cfg_attr(feature = "defmt", derive(defmt::Format))]
27pub enum DateTimeError {
28    /// The month is not between 1 and 12.
29    InvalidMonth(u8),
30    /// The day of the month is less than 1, or more than the maximum value for
31    /// this combination of year and month.
32    InvalidDayOfMonth(u8),
33    /// The hour field value is not between 0 and 23.
34    InvalidHour(u8),
35    /// The minute field value is not between 1 and 59.
36    InvalidMinute(u8),
37    /// The second field value is not between 1 and 59.
38    InvalidSecond(u8),
39    /// The nanosecond field value is more than 999 999 999.
40    InvalidNanosecond(u32),
41    /// This date-time value cannot be represented as a TAI timestamp with the
42    /// specified epoch.
43    OutOfRange,
44}
45
46impl fmt::Display for DateTimeError {
47    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match self {
49            Self::InvalidMonth(month) => write!(fmt, "month numeral '{}' is not valid", month),
50            Self::InvalidDayOfMonth(day) => {
51                write!(fmt, "day of month '{}' is not valid for this date", day)
52            }
53            Self::InvalidHour(hour) => write!(fmt, "hour numeral '{}' is not valid", hour),
54            Self::InvalidMinute(min) => write!(fmt, "minute numeral '{}' is not valid", min),
55            Self::InvalidSecond(sec) => write!(fmt, "second numeral '{}' is not valid", sec),
56            Self::InvalidNanosecond(nanosec) => {
57                write!(fmt, "nanosecond value '{}' is not valid", nanosec)
58            }
59            Self::OutOfRange => "timestamp outside representable range".fmt(fmt),
60        }
61    }
62}
63
64#[cfg(feature = "std")]
65impl std::error::Error for DateTimeError {}
66
67/// The error type returned when a date-time string is invalid or corresponds to
68/// a timestamp outside the representable range.
69#[derive(Debug, PartialEq, Eq, Clone)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71#[cfg_attr(feature = "defmt", derive(defmt::Format))]
72pub enum ParseDateTimeError {
73    /// A field value is either not of the expected numeric type or is out of
74    /// range for the expected numeric type.
75    InvalidFieldValue,
76    /// The width of a fixed-width or minimum-width field is invalid.
77    InvalidFieldWidth,
78    /// A field is missing.
79    MissingField,
80    /// One of the field value is out of its expected range, or the
81    /// corresponding timestamp is outside the representable range.
82    RangeError(DateTimeError),
83}
84
85impl fmt::Display for ParseDateTimeError {
86    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
87        match self {
88            Self::InvalidFieldValue => "one of the fields is invalid".fmt(fmt),
89            Self::InvalidFieldWidth => "the width of one of the fields is invalid".fmt(fmt),
90            Self::MissingField => "a fields is missing".fmt(fmt),
91            Self::RangeError(err) => err.fmt(fmt),
92        }
93    }
94}
95
96#[cfg(feature = "std")]
97impl std::error::Error for ParseDateTimeError {}