diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/lib.rs | 96 | ||||
-rw-r--r-- | src/percent_encoded_character_decoder.rs | 19 |
3 files changed, 28 insertions, 88 deletions
@@ -8,3 +8,4 @@ edition = "2018" [dependencies] lazy_static = "1.4" +thiserror = "1.0" @@ -200,86 +200,40 @@ impl std::fmt::Display for Context { } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, thiserror::Error, PartialEq)] pub enum Error { - CannotExpressAsUtf8(std::string::FromUtf8Error), + #[error("URI contains non-UTF8 sequences")] + CannotExpressAsUtf8(#[from] std::string::FromUtf8Error), + + #[error("scheme expected but missing")] EmptyScheme, + + #[error("illegal character in {0}")] IllegalCharacter(Context), - IllegalPercentEncoding(percent_encoded_character_decoder::Error), - IllegalPortNumber(std::num::ParseIntError), + + #[error("illegal percent encoding")] + IllegalPercentEncoding(#[from] percent_encoded_character_decoder::Error), + + #[error("illegal port number")] + IllegalPortNumber(#[source] std::num::ParseIntError), + + #[error("octet group expected")] InvalidDecimalOctet, + + #[error("too few address parts")] TooFewAddressParts, - TooManyAddressParts, - TooManyDigits, - TooManyDoubleColons, - TruncatedHost, -} -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::CannotExpressAsUtf8(_) => { - write!(f, "URI contains non-UTF8 sequences") - }, - Error::EmptyScheme => { - write!(f, "scheme expected but missing") - }, - Error::IllegalCharacter(context) => { - write!(f, "illegal character in {}", context) - }, - Error::IllegalPercentEncoding(_) => { - write!(f, "illegal percent encoding") - }, - Error::IllegalPortNumber(_) => { - write!(f, "illegal port number") - } - Error::TruncatedHost => { - write!(f, "truncated host") - }, - Error::InvalidDecimalOctet => { - write!(f, "octet group expected") - }, - Error::TooFewAddressParts => { - write!(f, "too few address parts") - }, - Error::TooManyAddressParts => { - write!(f, "too many address parts") - }, - Error::TooManyDigits => { - write!(f, "too many digits") - }, - Error::TooManyDoubleColons => { - write!(f, "too many double-colons") - }, - } - } -} + #[error("too many address parts")] + TooManyAddressParts, -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - Error::CannotExpressAsUtf8(source) => Some(source), - Error::IllegalPercentEncoding(source) => Some(source), - Error::IllegalPortNumber(source) => Some(source), - _ => None, - } - } -} + #[error("too many digits in IPv6 address part")] + TooManyDigits, -impl From<percent_encoded_character_decoder::Error> for Error { - fn from(error: percent_encoded_character_decoder::Error) -> Self { - match error { - percent_encoded_character_decoder::Error::IllegalCharacter => { - Error::IllegalPercentEncoding(error) - }, - } - } -} + #[error("too many double-colons in IPv6 address")] + TooManyDoubleColons, -impl From<std::string::FromUtf8Error> for Error { - fn from(error: std::string::FromUtf8Error) -> Self { - Error::CannotExpressAsUtf8(error) - } + #[error("truncated host")] + TruncatedHost, } // TODO: explore possibly returning an iterator instead of a String diff --git a/src/percent_encoded_character_decoder.rs b/src/percent_encoded_character_decoder.rs index 781a643..08a92e1 100644 --- a/src/percent_encoded_character_decoder.rs +++ b/src/percent_encoded_character_decoder.rs @@ -29,27 +29,12 @@ lazy_static! { // TODO: Learn about using thiserror to define library errors // [14:05] ABuffSeagull: You should use https://lib.rs/crates/thiserror for the errors // [14:07] 715209: i also recommend thiserror -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, thiserror::Error, PartialEq)] pub enum Error { + #[error("illegal character")] IllegalCharacter, } -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::IllegalCharacter => { - write!(f, "illegal character") - }, - } - } -} - -impl std::error::Error for Error { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - None - } -} - pub struct PercentEncodedCharacterDecoder { decoded_character: u8, digits_left: usize, |