diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2019-10-05 13:45:15 +0200 |
---|---|---|
committer | Matthias Beyer <mail@beyermatthias.de> | 2019-10-05 13:51:41 +0200 |
commit | db24b96292543c3eceb326e9e05e3c121f8719c7 (patch) | |
tree | 8877b95ddf39c9a6f5eb9c8766a4af6d08b73d3e | |
parent | 08d28d143c62ec3590e933d4a6b4c1c22f0b7d77 (diff) |
Move error type to own module
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r-- | src/error.rs | 72 | ||||
-rw-r--r-- | src/lib.rs | 73 |
2 files changed, 74 insertions, 71 deletions
diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..251aafc --- /dev/null +++ b/src/error.rs @@ -0,0 +1,72 @@ +use std::error; +use std::fmt; +use std::ops::Deref; + +/// An error type that represents the different kinds of errors that may be +/// encountered during message parsing. +#[derive(Debug)] +pub enum MailParseError { + /// Data that was specified as being in the quoted-printable transfer-encoding + /// could not be successfully decoded as quoted-printable data. + QuotedPrintableDecodeError(quoted_printable::QuotedPrintableError), + /// Data that was specified as being in the base64 transfer-encoding could + /// not be successfully decoded as base64 data. + Base64DecodeError(base64::DecodeError), + /// An error occurred when converting the raw byte data to Rust UTF-8 string + /// format using the charset specified in the message. + EncodingError(std::borrow::Cow<'static, str>), + /// Some other error occurred while parsing the message; the description string + /// provides additional details. + Generic(&'static str), +} + +impl fmt::Display for MailParseError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match *self { + MailParseError::QuotedPrintableDecodeError(ref err) => { + write!(f, "QuotedPrintable decode error: {}", err) + } + MailParseError::Base64DecodeError(ref err) => write!(f, "Base64 decode error: {}", err), + MailParseError::EncodingError(ref err) => write!(f, "Encoding error: {}", err), + MailParseError::Generic(ref description) => write!(f, "{}", description), + } + } +} + +impl error::Error for MailParseError { + fn description(&self) -> &str { + match *self { + MailParseError::QuotedPrintableDecodeError(ref err) => err.description(), + MailParseError::Base64DecodeError(ref err) => err.description(), + MailParseError::EncodingError(ref err) => err.deref(), + _ => "An error occurred while attempting to parse the input", + } + } + + fn cause(&self) -> Option<&dyn error::Error> { + match *self { + MailParseError::QuotedPrintableDecodeError(ref err) => Some(err), + MailParseError::Base64DecodeError(ref err) => Some(err), + _ => None, + } + } +} + +impl From<quoted_printable::QuotedPrintableError> for MailParseError { + fn from(err: quoted_printable::QuotedPrintableError) -> MailParseError { + MailParseError::QuotedPrintableDecodeError(err) + } +} + +impl From<base64::DecodeError> for MailParseError { + fn from(err: base64::DecodeError) -> MailParseError { + MailParseError::Base64DecodeError(err) + } +} + +impl From<std::borrow::Cow<'static, str>> for MailParseError { + fn from(err: std::borrow::Cow<'static, str>) -> MailParseError { + MailParseError::EncodingError(err) + } +} + @@ -3,86 +3,17 @@ extern crate charset; extern crate quoted_printable; use std::collections::BTreeMap; -use std::error; -use std::fmt; -use std::ops::Deref; use charset::decode_latin1; use charset::Charset; pub mod body; mod dateparse; +mod error; use body::Body; pub use dateparse::dateparse; - -/// An error type that represents the different kinds of errors that may be -/// encountered during message parsing. -#[derive(Debug)] -pub enum MailParseError { - /// Data that was specified as being in the quoted-printable transfer-encoding - /// could not be successfully decoded as quoted-printable data. - QuotedPrintableDecodeError(quoted_printable::QuotedPrintableError), - /// Data that was specified as being in the base64 transfer-encoding could - /// not be successfully decoded as base64 data. - Base64DecodeError(base64::DecodeError), - /// An error occurred when converting the raw byte data to Rust UTF-8 string - /// format using the charset specified in the message. - EncodingError(std::borrow::Cow<'static, str>), - /// Some other error occurred while parsing the message; the description string - /// provides additional details. - Generic(&'static str), -} - -impl fmt::Display for MailParseError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - MailParseError::QuotedPrintableDecodeError(ref err) => { - write!(f, "QuotedPrintable decode error: {}", err) - } - MailParseError::Base64DecodeError(ref err) => write!(f, "Base64 decode error: {}", err), - MailParseError::EncodingError(ref err) => write!(f, "Encoding error: {}", err), - MailParseError::Generic(ref description) => write!(f, "{}", description), - } - } -} - -impl error::Error for MailParseError { - fn description(&self) -> &str { - match *self { - MailParseError::QuotedPrintableDecodeError(ref err) => err.description(), - MailParseError::Base64DecodeError(ref err) => err.description(), - MailParseError::EncodingError(ref err) => err.deref(), - _ => "An error occurred while attempting to parse the input", - } - } - - fn cause(&self) -> Option<&error::Error> { - match *self { - MailParseError::QuotedPrintableDecodeError(ref err) => Some(err), - MailParseError::Base64DecodeError(ref err) => Some(err), - _ => None, - } - } -} - -impl From<quoted_printable::QuotedPrintableError> for MailParseError { - fn from(err: quoted_printable::QuotedPrintableError) -> MailParseError { - MailParseError::QuotedPrintableDecodeError(err) - } -} - -impl From<base64::DecodeError> for MailParseError { - fn from(err: base64::DecodeError) -> MailParseError { - MailParseError::Base64DecodeError(err) - } -} - -impl From<std::borrow::Cow<'static, str>> for MailParseError { - fn from(err: std::borrow::Cow<'static, str>) -> MailParseError { - MailParseError::EncodingError(err) - } -} +pub use error::*; /// A struct that represents a single header in the message. /// It holds slices into the raw byte array passed to parse_mail, and so the |