From cedfd8bfbb49acdbc6bb1656be0e5ac2ab656394 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Tue, 4 Aug 2020 11:14:11 +0200 Subject: [PATCH] chore: simplify Error and Display implementations --- src/error.rs | 24 ++++++++++++------------ src/transport/smtp/authentication.rs | 14 +++++--------- src/transport/smtp/commands.rs | 4 ++-- src/transport/smtp/extension.rs | 22 +++++++++------------- src/transport/stub/mod.rs | 8 ++------ 5 files changed, 30 insertions(+), 42 deletions(-) diff --git a/src/error.rs b/src/error.rs index 0daa895..1e456a9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -28,18 +28,18 @@ pub enum Error { } impl Display for Error { - fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), fmt::Error> { - fmt.write_str(&match self { - Error::MissingFrom => "missing source address, invalid envelope".to_string(), - Error::MissingTo => "missing destination address, invalid envelope".to_string(), - Error::TooManyFrom => "there can only be one source address".to_string(), - Error::EmailMissingAt => "missing @ in email address".to_string(), - Error::EmailMissingLocalPart => "missing local part in email address".to_string(), - Error::EmailMissingDomain => "missing domain in email address".to_string(), - Error::CannotParseFilename => "could not parse attachment filename".to_string(), - Error::NonAsciiChars => "contains non-ASCII chars".to_string(), - Error::Io(e) => e.to_string(), - }) + fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { + match self { + Error::MissingFrom => f.write_str("missing source address, invalid envelope"), + Error::MissingTo => f.write_str("missing destination address, invalid envelope"), + Error::TooManyFrom => f.write_str("there can only be one source address"), + Error::EmailMissingAt => f.write_str("missing @ in email address"), + Error::EmailMissingLocalPart => f.write_str("missing local part in email address"), + Error::EmailMissingDomain => f.write_str("missing domain in email address"), + Error::CannotParseFilename => f.write_str("could not parse attachment filename"), + Error::NonAsciiChars => f.write_str("contains non-ASCII chars"), + Error::Io(e) => e.fmt(f), + } } } diff --git a/src/transport/smtp/authentication.rs b/src/transport/smtp/authentication.rs index 077ffb0..75e3195 100644 --- a/src/transport/smtp/authentication.rs +++ b/src/transport/smtp/authentication.rs @@ -62,15 +62,11 @@ pub enum Mechanism { impl Display for Mechanism { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!( - f, - "{}", - match *self { - Mechanism::Plain => "PLAIN", - Mechanism::Login => "LOGIN", - Mechanism::Xoauth2 => "XOAUTH2", - } - ) + f.write_str(match *self { + Mechanism::Plain => "PLAIN", + Mechanism::Login => "LOGIN", + Mechanism::Xoauth2 => "XOAUTH2", + }) } } diff --git a/src/transport/smtp/commands.rs b/src/transport/smtp/commands.rs index 5a35c86..f710afe 100644 --- a/src/transport/smtp/commands.rs +++ b/src/transport/smtp/commands.rs @@ -147,8 +147,8 @@ pub struct Help { impl Display for Help { fn fmt(&self, f: &mut Formatter) -> fmt::Result { f.write_str("HELP")?; - if self.argument.is_some() { - write!(f, " {}", self.argument.as_ref().unwrap())?; + if let Some(argument) = &self.argument { + write!(f, " {}", argument)?; } f.write_str("\r\n") } diff --git a/src/transport/smtp/extension.rs b/src/transport/smtp/extension.rs index 4346745..0df11f8 100644 --- a/src/transport/smtp/extension.rs +++ b/src/transport/smtp/extension.rs @@ -81,9 +81,9 @@ pub enum Extension { impl Display for Extension { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match *self { - Extension::EightBitMime => write!(f, "8BITMIME"), - Extension::SmtpUtfEight => write!(f, "SMTPUTF8"), - Extension::StartTls => write!(f, "STARTTLS"), + Extension::EightBitMime => f.write_str("8BITMIME"), + Extension::SmtpUtfEight => f.write_str("SMTPUTF8"), + Extension::StartTls => f.write_str("STARTTLS"), Extension::Authentication(ref mechanism) => write!(f, "AUTH {}", mechanism), } } @@ -105,16 +105,12 @@ pub struct ServerInfo { impl Display for ServerInfo { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!( - f, - "{} with {}", - self.name, - if self.features.is_empty() { - "no supported features".to_string() - } else { - format!("{:?}", self.features) - } - ) + let features = if self.features.is_empty() { + "no supported features".to_string() + } else { + format!("{:?}", self.features) + }; + write!(f, "{} with {}", self.name, features) } } diff --git a/src/transport/stub/mod.rs b/src/transport/stub/mod.rs index 3129719..24bbf4e 100644 --- a/src/transport/stub/mod.rs +++ b/src/transport/stub/mod.rs @@ -36,15 +36,11 @@ pub struct Error; impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "stub error") + f.write_str("stub error") } } -impl StdError for Error { - fn source(&self) -> Option<&(dyn StdError + 'static)> { - None - } -} +impl StdError for Error {} /// This transport logs the message envelope and returns the given response #[derive(Debug, Clone, Copy)]