| 1 |
use crate::{error, util::escape}; |
| 2 |
|
| 3 |
#[derive(Clone, Debug)] |
| 4 |
#[cfg_attr(feature = "defmt", derive(defmt::Format))] |
| 5 |
pub(crate) enum Error { |
| 6 |
ColonAfterHours, |
| 7 |
EndOfInput, |
| 8 |
EndOfInputHour, |
| 9 |
EndOfInputMinute, |
| 10 |
EndOfInputNumeric, |
| 11 |
EndOfInputSecond, |
| 12 |
InvalidHours, |
| 13 |
InvalidMinutes, |
| 14 |
InvalidSeconds, |
| 15 |
InvalidSecondsFractional, |
| 16 |
InvalidSign, |
| 17 |
InvalidSignPlusOrMinus, |
| 18 |
MissingMinuteAfterHour, |
| 19 |
MissingSecondAfterMinute, |
| 20 |
NoColonAfterHours, |
| 21 |
ParseHours, |
| 22 |
ParseMinutes, |
| 23 |
ParseSeconds, |
| 24 |
PrecisionLoss, |
| 25 |
SeparatorAfterHours, |
| 26 |
SeparatorAfterMinutes, |
| 27 |
SubminutePrecisionNotEnabled, |
| 28 |
SubsecondPrecisionNotEnabled, |
| 29 |
UnexpectedLetterOffsetNoZulu(u8), |
| 30 |
} |
| 31 |
|
| 32 |
impl error::IntoError for Error { |
| 33 |
fn into_error(self) -> error::Error { |
| 34 |
self.into() |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl From<Error> for error::Error { |
| 39 |
#[cold] |
| 40 |
#[inline(never)] |
| 41 |
fn from(err: Error) -> error::Error { |
| 42 |
error::ErrorKind::FmtOffset(err).into() |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
impl core::fmt::Display for Error { |
| 47 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 48 |
use self::Error::*; |
| 49 |
|
| 50 |
match *self { |
| 51 |
ColonAfterHours => f.write_str( |
| 52 |
"parsed hour component of time zone offset, \ |
| 53 |
but found colon after hours which is not allowed", |
| 54 |
), |
| 55 |
EndOfInput => { |
| 56 |
f.write_str("expected UTC offset, but found end of input") |
| 57 |
} |
| 58 |
EndOfInputHour => f.write_str( |
| 59 |
"expected two digit hour after sign, but found end of input", |
| 60 |
), |
| 61 |
EndOfInputMinute => f.write_str( |
| 62 |
"expected two digit minute after hours, \ |
| 63 |
but found end of input", |
| 64 |
), |
| 65 |
EndOfInputNumeric => f.write_str( |
| 66 |
"expected UTC numeric offset, but found end of input", |
| 67 |
), |
| 68 |
EndOfInputSecond => f.write_str( |
| 69 |
"expected two digit second after minutes, \ |
| 70 |
but found end of input", |
| 71 |
), |
| 72 |
InvalidHours => { |
| 73 |
f.write_str("failed to parse hours in UTC numeric offset") |
| 74 |
} |
| 75 |
InvalidMinutes => { |
| 76 |
f.write_str("failed to parse minutes in UTC numeric offset") |
| 77 |
} |
| 78 |
InvalidSeconds => { |
| 79 |
f.write_str("failed to parse seconds in UTC numeric offset") |
| 80 |
} |
| 81 |
InvalidSecondsFractional => f.write_str( |
| 82 |
"failed to parse fractional seconds in UTC numeric offset", |
| 83 |
), |
| 84 |
InvalidSign => { |
| 85 |
f.write_str("failed to parse sign in UTC numeric offset") |
| 86 |
} |
| 87 |
InvalidSignPlusOrMinus => f.write_str( |
| 88 |
"expected `+` or `-` sign at start of UTC numeric offset", |
| 89 |
), |
| 90 |
MissingMinuteAfterHour => f.write_str( |
| 91 |
"parsed hour component of time zone offset, \ |
| 92 |
but could not find required minute component", |
| 93 |
), |
| 94 |
MissingSecondAfterMinute => f.write_str( |
| 95 |
"parsed hour and minute components of time zone offset, \ |
| 96 |
but could not find required second component", |
| 97 |
), |
| 98 |
NoColonAfterHours => f.write_str( |
| 99 |
"parsed hour component of time zone offset, \ |
| 100 |
but could not find required colon separator", |
| 101 |
), |
| 102 |
ParseHours => f.write_str( |
| 103 |
"failed to parse hours (requires a two digit integer)", |
| 104 |
), |
| 105 |
ParseMinutes => f.write_str( |
| 106 |
"failed to parse minutes (requires a two digit integer)", |
| 107 |
), |
| 108 |
ParseSeconds => f.write_str( |
| 109 |
"failed to parse seconds (requires a two digit integer)", |
| 110 |
), |
| 111 |
PrecisionLoss => f.write_str( |
| 112 |
"due to precision loss from fractional seconds, \ |
| 113 |
time zone offset is rounded to a value that is out of bounds", |
| 114 |
), |
| 115 |
SeparatorAfterHours => f.write_str( |
| 116 |
"failed to parse separator after hours in \ |
| 117 |
UTC numeric offset", |
| 118 |
), |
| 119 |
SeparatorAfterMinutes => f.write_str( |
| 120 |
"failed to parse separator after minutes in \ |
| 121 |
UTC numeric offset", |
| 122 |
), |
| 123 |
SubminutePrecisionNotEnabled => f.write_str( |
| 124 |
"subminute precision for UTC numeric offset \ |
| 125 |
is not enabled in this context (must provide only \ |
| 126 |
integral minutes)", |
| 127 |
), |
| 128 |
SubsecondPrecisionNotEnabled => f.write_str( |
| 129 |
"subsecond precision for UTC numeric offset \ |
| 130 |
is not enabled in this context (must provide only \ |
| 131 |
integral minutes or seconds)", |
| 132 |
), |
| 133 |
UnexpectedLetterOffsetNoZulu(byte) => write!( |
| 134 |
f, |
| 135 |
"found `{z}` where a numeric UTC offset \ |
| 136 |
was expected (this context does not permit \ |
| 137 |
the Zulu offset)", |
| 138 |
z = escape::Byte(byte), |
| 139 |
), |
| 140 |
} |
| 141 |
} |
| 142 |
} |