| 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 |
EndOfInputAnnotation, |
| 7 |
EndOfInputAnnotationClose, |
| 8 |
EndOfInputAnnotationKey, |
| 9 |
EndOfInputAnnotationSeparator, |
| 10 |
EndOfInputAnnotationValue, |
| 11 |
EndOfInputTzAnnotationClose, |
| 12 |
UnexpectedByteAnnotation { byte: u8 }, |
| 13 |
UnexpectedByteAnnotationClose { byte: u8 }, |
| 14 |
UnexpectedByteAnnotationKey { byte: u8 }, |
| 15 |
UnexpectedByteAnnotationValue { byte: u8 }, |
| 16 |
UnexpectedByteAnnotationSeparator { byte: u8 }, |
| 17 |
UnexpectedByteTzAnnotationClose { byte: u8 }, |
| 18 |
UnexpectedSlashAnnotationSeparator, |
| 19 |
UnsupportedAnnotationCritical, |
| 20 |
} |
| 21 |
|
| 22 |
impl error::IntoError for Error { |
| 23 |
fn into_error(self) -> error::Error { |
| 24 |
self.into() |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
impl From<Error> for error::Error { |
| 29 |
#[cold] |
| 30 |
#[inline(never)] |
| 31 |
fn from(err: Error) -> error::Error { |
| 32 |
error::ErrorKind::FmtRfc9557(err).into() |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
impl core::fmt::Display for Error { |
| 37 |
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { |
| 38 |
use self::Error::*; |
| 39 |
|
| 40 |
match *self { |
| 41 |
EndOfInputAnnotation => f.write_str( |
| 42 |
"expected the start of an RFC 9557 annotation or IANA \ |
| 43 |
time zone component name, but found end of input instead", |
| 44 |
), |
| 45 |
EndOfInputAnnotationClose => f.write_str( |
| 46 |
"expected an `]` after parsing an RFC 9557 annotation key \ |
| 47 |
and value, but found end of input instead", |
| 48 |
), |
| 49 |
EndOfInputAnnotationKey => f.write_str( |
| 50 |
"expected the start of an RFC 9557 annotation key, \ |
| 51 |
but found end of input instead", |
| 52 |
), |
| 53 |
EndOfInputAnnotationSeparator => f.write_str( |
| 54 |
"expected an `=` after parsing an RFC 9557 annotation key, \ |
| 55 |
but found end of input instead", |
| 56 |
), |
| 57 |
EndOfInputAnnotationValue => f.write_str( |
| 58 |
"expected the start of an RFC 9557 annotation value, \ |
| 59 |
but found end of input instead", |
| 60 |
), |
| 61 |
EndOfInputTzAnnotationClose => f.write_str( |
| 62 |
"expected an `]` after parsing an RFC 9557 time zone \ |
| 63 |
annotation, but found end of input instead", |
| 64 |
), |
| 65 |
UnexpectedByteAnnotation { byte } => write!( |
| 66 |
f, |
| 67 |
"expected ASCII alphabetic byte (or underscore or period) \ |
| 68 |
at the start of an RFC 9557 annotation or time zone \ |
| 69 |
component name, but found `{byte}` instead", |
| 70 |
byte = escape::Byte(byte), |
| 71 |
), |
| 72 |
UnexpectedByteAnnotationClose { byte } => write!( |
| 73 |
f, |
| 74 |
"expected an `]` after parsing an RFC 9557 annotation key \ |
| 75 |
and value, but found `{byte}` instead", |
| 76 |
byte = escape::Byte(byte), |
| 77 |
), |
| 78 |
UnexpectedByteAnnotationKey { byte } => write!( |
| 79 |
f, |
| 80 |
"expected lowercase alphabetic byte (or underscore) \ |
| 81 |
at the start of an RFC 9557 annotation key, \ |
| 82 |
but found `{byte}` instead", |
| 83 |
byte = escape::Byte(byte), |
| 84 |
), |
| 85 |
UnexpectedByteAnnotationValue { byte } => write!( |
| 86 |
f, |
| 87 |
"expected alphanumeric ASCII byte \ |
| 88 |
at the start of an RFC 9557 annotation value, \ |
| 89 |
but found `{byte}` instead", |
| 90 |
byte = escape::Byte(byte), |
| 91 |
), |
| 92 |
UnexpectedByteAnnotationSeparator { byte } => write!( |
| 93 |
f, |
| 94 |
"expected an `=` after parsing an RFC 9557 annotation \ |
| 95 |
key, but found `{byte}` instead", |
| 96 |
byte = escape::Byte(byte), |
| 97 |
), |
| 98 |
UnexpectedByteTzAnnotationClose { byte } => write!( |
| 99 |
f, |
| 100 |
"expected an `]` after parsing an RFC 9557 time zone \ |
| 101 |
annotation, but found `{byte}` instead", |
| 102 |
byte = escape::Byte(byte), |
| 103 |
), |
| 104 |
UnexpectedSlashAnnotationSeparator => f.write_str( |
| 105 |
"expected an `=` after parsing an RFC 9557 annotation \ |
| 106 |
key, but found `/` instead (time zone annotations must \ |
| 107 |
come first)", |
| 108 |
), |
| 109 |
UnsupportedAnnotationCritical => f.write_str( |
| 110 |
"found unsupported RFC 9557 annotation \ |
| 111 |
with the critical flag (`!`) set", |
| 112 |
), |
| 113 |
} |
| 114 |
} |
| 115 |
} |