| 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 |
Empty, |
| 7 |
ExpectedColonAfterMinute, |
| 8 |
ExpectedIntegerAfterSign, |
| 9 |
ExpectedMinuteAfterHour, |
| 10 |
ExpectedOneMoreUnitAfterComma, |
| 11 |
ExpectedOneSign, |
| 12 |
ExpectedSecondAfterMinute, |
| 13 |
ExpectedUnitSuffix, |
| 14 |
ExpectedWhitespaceAfterComma { byte: u8 }, |
| 15 |
ExpectedWhitespaceAfterCommaEndOfInput, |
| 16 |
Failed, |
| 17 |
} |
| 18 |
|
| 19 |
impl error::IntoError for Error { |
| 20 |
fn into_error(self) -> error::Error { |
| 21 |
self.into() |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
impl From<Error> for error::Error { |
| 26 |
#[cold] |
| 27 |
#[inline(never)] |
| 28 |
fn from(err: Error) -> error::Error { |
| 29 |
error::ErrorKind::FmtFriendly(err).into() |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl core::fmt::Display for Error { |
| 34 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 35 |
use self::Error::*; |
| 36 |
|
| 37 |
match *self { |
| 38 |
Empty => f.write_str("an empty string is not valid"), |
| 39 |
ExpectedColonAfterMinute => f.write_str( |
| 40 |
"when parsing the `HH:MM:SS` format, \ |
| 41 |
expected to parse `:` following minute", |
| 42 |
), |
| 43 |
ExpectedIntegerAfterSign => f.write_str( |
| 44 |
"expected duration to start \ |
| 45 |
with a unit value (a decimal integer) after an \ |
| 46 |
optional sign, but no integer was found", |
| 47 |
), |
| 48 |
ExpectedMinuteAfterHour => f.write_str( |
| 49 |
"when parsing the `HH:MM:SS` format, \ |
| 50 |
expected to parse minute following hour", |
| 51 |
), |
| 52 |
ExpectedOneMoreUnitAfterComma => f.write_str( |
| 53 |
"found comma at the end of duration, \ |
| 54 |
but a comma indicates at least one more \ |
| 55 |
unit follows", |
| 56 |
), |
| 57 |
ExpectedOneSign => f.write_str( |
| 58 |
"expected to find either a prefix sign (+/-) or \ |
| 59 |
a suffix sign (`ago`), but found both", |
| 60 |
), |
| 61 |
ExpectedSecondAfterMinute => f.write_str( |
| 62 |
"when parsing the `HH:MM:SS` format, \ |
| 63 |
expected to parse second following minute", |
| 64 |
), |
| 65 |
ExpectedUnitSuffix => f.write_str( |
| 66 |
"expected to find unit designator suffix \ |
| 67 |
(e.g., `years` or `secs`) after parsing \ |
| 68 |
integer", |
| 69 |
), |
| 70 |
ExpectedWhitespaceAfterComma { byte } => write!( |
| 71 |
f, |
| 72 |
"expected whitespace after comma, but found `{byte}`", |
| 73 |
byte = escape::Byte(byte), |
| 74 |
), |
| 75 |
ExpectedWhitespaceAfterCommaEndOfInput => f.write_str( |
| 76 |
"expected whitespace after comma, but found end of input", |
| 77 |
), |
| 78 |
Failed => f.write_str( |
| 79 |
"failed to parse input in the \"friendly\" duration format", |
| 80 |
), |
| 81 |
} |
| 82 |
} |
| 83 |
} |