diff --git a/lettre/src/error.rs b/lettre/src/error.rs index c8ceabc..6281e49 100644 --- a/lettre/src/error.rs +++ b/lettre/src/error.rs @@ -1,8 +1,8 @@ +use self::Error::*; use std::{ error::Error as StdError, fmt::{self, Display, Formatter}, }; -use self::Error::*; /// Error type for email content #[derive(Debug, Clone, Copy)] diff --git a/lettre/src/file/error.rs b/lettre/src/file/error.rs index 704be4f..c81c890 100644 --- a/lettre/src/file/error.rs +++ b/lettre/src/file/error.rs @@ -1,12 +1,12 @@ //! Error and result type for file transport +use self::Error::*; +use serde_json; +use std::io; use std::{ error::Error as StdError, fmt::{self, Display, Formatter}, }; -use serde_json; -use std::io; -use self::Error::*; /// An enum of all error kinds. #[derive(Debug)] @@ -21,20 +21,16 @@ pub enum Error { impl Display for Error { fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { - fmt.write_str(self.description()) + match *self { + Client(err) => fmt.write_str(err), + Io(ref err) => err.fmt(fmt), + JsonSerialization(ref err) => err.fmt(fmt), + } } } impl StdError for Error { - fn description(&self) -> &str { - match *self { - Client(err) => err, - Io(ref err) => err.description(), - JsonSerialization(ref err) => err.description(), - } - } - - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { match *self { Io(ref err) => Some(&*err), JsonSerialization(ref err) => Some(&*err), diff --git a/lettre/src/lib.rs b/lettre/src/lib.rs index bdf2ac9..6e0a5f1 100644 --- a/lettre/src/lib.rs +++ b/lettre/src/lib.rs @@ -30,11 +30,11 @@ extern crate serde; #[cfg(feature = "serde-impls")] #[macro_use] extern crate serde_derive; -#[cfg(feature = "file-transport")] -extern crate serde_json; extern crate fast_chemail; #[cfg(feature = "connection-pool")] extern crate r2d2; +#[cfg(feature = "file-transport")] +extern crate serde_json; pub mod error; #[cfg(feature = "file-transport")] @@ -73,7 +73,7 @@ pub struct EmailAddress(String); impl EmailAddress { pub fn new(address: String) -> EmailResult { if !is_valid_email(&address) && !address.ends_with("localhost") { - Err(Error::InvalidEmailAddress)?; + return Err(Error::InvalidEmailAddress); } Ok(EmailAddress(address)) } @@ -123,7 +123,7 @@ impl Envelope { /// Creates a new envelope, which may fail if `to` is empty. pub fn new(from: Option, to: Vec) -> EmailResult { if to.is_empty() { - Err(Error::MissingTo)?; + return Err(Error::MissingTo); } Ok(Envelope { forward_path: to, @@ -143,7 +143,7 @@ impl Envelope { } pub enum Message { - Reader(Box), + Reader(Box), Bytes(Cursor>), } @@ -175,7 +175,7 @@ impl SendableEmail { pub fn new_with_reader( envelope: Envelope, message_id: String, - message: Box, + message: Box, ) -> SendableEmail { SendableEmail { envelope, diff --git a/lettre/src/sendmail/error.rs b/lettre/src/sendmail/error.rs index 856a2f6..9c632fb 100644 --- a/lettre/src/sendmail/error.rs +++ b/lettre/src/sendmail/error.rs @@ -1,11 +1,11 @@ //! Error and result type for sendmail transport +use self::Error::*; +use std::io; use std::{ error::Error as StdError, fmt::{self, Display, Formatter}, }; -use self::Error::*; -use std::io; /// An enum of all error kinds. #[derive(Debug)] @@ -18,19 +18,15 @@ pub enum Error { impl Display for Error { fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { - fmt.write_str(self.description()) + match *self { + Client(ref err) => err.fmt(fmt), + Io(ref err) => err.fmt(fmt), + } } } impl StdError for Error { - fn description(&self) -> &str { - match *self { - Client(err) => err, - Io(ref err) => err.description(), - } - } - - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { match *self { Io(ref err) => Some(&*err), _ => None, diff --git a/lettre/src/sendmail/mod.rs b/lettre/src/sendmail/mod.rs index dc2ba80..9677c13 100644 --- a/lettre/src/sendmail/mod.rs +++ b/lettre/src/sendmail/mod.rs @@ -72,7 +72,7 @@ impl<'a> Transport<'a> for SendmailTransport { Ok(()) } else { // TODO display stderr - Err(error::Error::Client("The message could not be sent"))? + Err(error::Error::Client("The message could not be sent")) } } } diff --git a/lettre/src/smtp/client/mod.rs b/lettre/src/smtp/client/mod.rs index d0c759e..e91c1a9 100644 --- a/lettre/src/smtp/client/mod.rs +++ b/lettre/src/smtp/client/mod.rs @@ -193,7 +193,7 @@ impl InnerClient { } /// Sends the message content - pub fn message(&mut self, message: Box) -> SmtpResult { + pub fn message(&mut self, message: Box) -> SmtpResult { let mut out_buf: Vec = vec![]; let mut codec = ClientCodec::new(); diff --git a/lettre/src/smtp/commands.rs b/lettre/src/smtp/commands.rs index 392424b..544a125 100644 --- a/lettre/src/smtp/commands.rs +++ b/lettre/src/smtp/commands.rs @@ -245,7 +245,7 @@ impl AuthCommand { challenge: Option, ) -> Result { let response = if mechanism.supports_initial_response() || challenge.is_some() { - Some(mechanism.response(&credentials, challenge.as_ref().map(String::as_str))?) + Some(mechanism.response(&credentials, challenge.as_deref())?) } else { None }; diff --git a/lettre/src/smtp/error.rs b/lettre/src/smtp/error.rs index 0f46679..587ad89 100644 --- a/lettre/src/smtp/error.rs +++ b/lettre/src/smtp/error.rs @@ -42,36 +42,31 @@ pub enum Error { impl Display for Error { fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> { - fmt.write_str(self.description()) + match *self { + // Try to display the first line of the server's response that usually + // contains a short humanly readable error message + Transient(ref err) => fmt.write_str(match err.first_line() { + Some(line) => line, + None => "transient error during SMTP transaction", + }), + Permanent(ref err) => fmt.write_str(match err.first_line() { + Some(line) => line, + None => "permanent error during SMTP transaction", + }), + ResponseParsing(err) => fmt.write_str(err), + ChallengeParsing(ref err) => err.fmt(fmt), + Utf8Parsing(ref err) => err.fmt(fmt), + Resolution => fmt.write_str("could not resolve hostname"), + Client(err) => fmt.write_str(err), + Io(ref err) => err.fmt(fmt), + Tls(ref err) => err.fmt(fmt), + Parsing(ref err) => fmt.write_str(err.description()), + } } } impl StdError for Error { - #[cfg_attr(feature = "cargo-clippy", allow(clippy::match_same_arms))] - fn description(&self) -> &str { - match *self { - // Try to display the first line of the server's response that usually - // contains a short humanly readable error message - Transient(ref err) => match err.first_line() { - Some(line) => line, - None => "undetailed transient error during SMTP transaction", - }, - Permanent(ref err) => match err.first_line() { - Some(line) => line, - None => "undetailed permanent error during SMTP transaction", - }, - ResponseParsing(err) => err, - ChallengeParsing(ref err) => err.description(), - Utf8Parsing(ref err) => err.description(), - Resolution => "could not resolve hostname", - Client(err) => err, - Io(ref err) => err.description(), - Tls(ref err) => err.description(), - Parsing(ref err) => err.description(), - } - } - - fn cause(&self) -> Option<&StdError> { + fn cause(&self) -> Option<&dyn StdError> { match *self { ChallengeParsing(ref err) => Some(&*err), Utf8Parsing(ref err) => Some(&*err), diff --git a/lettre_email/src/lib.rs b/lettre_email/src/lib.rs index 24f1bb4..be701ec 100644 --- a/lettre_email/src/lib.rs +++ b/lettre_email/src/lib.rs @@ -16,9 +16,9 @@ extern crate base64; extern crate email as email_format; extern crate lettre; +pub extern crate mime; extern crate time; extern crate uuid; -pub extern crate mime; pub mod error; @@ -301,10 +301,7 @@ impl EmailBuilder { pub fn text>(self, body: S) -> EmailBuilder { let text = PartBuilder::new() .body(body) - .header(( - "Content-Type", - mime::TEXT_PLAIN_UTF_8.to_string(), - )) + .header(("Content-Type", mime::TEXT_PLAIN_UTF_8.to_string())) .build(); self.child(text) } @@ -313,10 +310,7 @@ impl EmailBuilder { pub fn html>(self, body: S) -> EmailBuilder { let html = PartBuilder::new() .body(body) - .header(( - "Content-Type", - mime::TEXT_HTML_UTF_8.to_string(), - )) + .header(("Content-Type", mime::TEXT_HTML_UTF_8.to_string())) .build(); self.child(html) } @@ -329,18 +323,12 @@ impl EmailBuilder { ) -> EmailBuilder { let text = PartBuilder::new() .body(body_text) - .header(( - "Content-Type", - mime::TEXT_PLAIN_UTF_8.to_string(), - )) + .header(("Content-Type", mime::TEXT_PLAIN_UTF_8.to_string())) .build(); let html = PartBuilder::new() .body(body_html) - .header(( - "Content-Type", - mime::TEXT_HTML_UTF_8.to_string(), - )) + .header(("Content-Type", mime::TEXT_HTML_UTF_8.to_string())) .build(); let alternate = PartBuilder::new() @@ -399,7 +387,7 @@ impl EmailBuilder { } } let from = Some(EmailAddress::from_str(&match self.sender { - Some(x) => Ok(x.address.clone()), // if we have a sender_header, use it + Some(x) => Ok(x.address), // if we have a sender_header, use it None => { // use a from header debug_assert!(self.from.len() <= 1); // else we'd have sender_header @@ -411,15 +399,11 @@ impl EmailBuilder { // if it's an author group, use the first author Some(mailbox) => Ok(mailbox.address.clone()), // for an empty author group (the rarest of the rare cases) - None => Err(Error::Envelope( - LettreError::MissingFrom, - )), // empty envelope sender + None => Err(Error::Envelope(LettreError::MissingFrom)), // empty envelope sender }, }, // if we don't have a from header - None => Err(Error::Envelope( - LettreError::MissingFrom, - )), // empty envelope sender + None => Err(Error::Envelope(LettreError::MissingFrom)), // empty envelope sender } } }?)?); @@ -443,9 +427,7 @@ impl EmailBuilder { .message .header(Header::new_with_value("From".into(), from).unwrap()); } else { - Err(Error::Envelope( - LettreError::MissingFrom, - ))?; + return Err(Error::Envelope(LettreError::MissingFrom)); } if !self.cc.is_empty() { self.message = self @@ -597,5 +579,4 @@ mod test { .as_slice() ); } - }