diff --git a/lettre/examples/smtp.rs b/lettre/examples/smtp.rs index 0fbc80e..e9bde92 100644 --- a/lettre/examples/smtp.rs +++ b/lettre/examples/smtp.rs @@ -1,6 +1,6 @@ extern crate lettre; -use lettre::{EmailAddress, EmailTransport, SimpleSendableEmail, SecurityLevel, SmtpTransport}; +use lettre::{EmailAddress, EmailTransport, SecurityLevel, SimpleSendableEmail, SmtpTransport}; fn main() { let email = SimpleSendableEmail::new( diff --git a/lettre/src/file/mod.rs b/lettre/src/file/mod.rs index a33937c..498702c 100644 --- a/lettre/src/file/mod.rs +++ b/lettre/src/file/mod.rs @@ -74,9 +74,7 @@ impl EmailTransport for FileEmailTransport { email.message(), ); - f.write_all( - serde_json::to_string(&simple_email)?.as_bytes(), - )?; + f.write_all(serde_json::to_string(&simple_email)?.as_bytes())?; Ok(()) } diff --git a/lettre/src/lib.rs b/lettre/src/lib.rs index 4392bfc..1f7be25 100644 --- a/lettre/src/lib.rs +++ b/lettre/src/lib.rs @@ -29,15 +29,15 @@ pub mod stub; #[cfg(feature = "file-transport")] pub mod file; -pub use smtp::SmtpTransport; -pub use smtp::SecurityLevel; #[cfg(feature = "file-transport")] pub use file::FileEmailTransport; -pub use stub::StubEmailTransport; pub use sendmail::SendmailTransport; +pub use smtp::SecurityLevel; +pub use smtp::SmtpTransport; use std::fmt; use std::fmt::{Display, Formatter}; +pub use stub::StubEmailTransport; /// Email address #[derive(PartialEq, Eq, Clone, Debug)] diff --git a/lettre/src/sendmail/mod.rs b/lettre/src/sendmail/mod.rs index 68b0612..c95c822 100644 --- a/lettre/src/sendmail/mod.rs +++ b/lettre/src/sendmail/mod.rs @@ -33,12 +33,16 @@ pub struct SendmailTransport { impl SendmailTransport { /// Creates a new transport with the default `/usr/sbin/sendmail` command pub fn new() -> SendmailTransport { - SendmailTransport { command: "/usr/sbin/sendmail".to_string() } + SendmailTransport { + command: "/usr/sbin/sendmail".to_string(), + } } /// Creates a new transport to the given sendmail command pub fn new_with_command>(command: S) -> SendmailTransport { - SendmailTransport { command: command.into() } + SendmailTransport { + command: command.into(), + } } } @@ -47,21 +51,21 @@ impl EmailTransport for SendmailTransport { // Spawn the sendmail command let to_addresses: Vec = email.to().iter().map(|x| x.to_string()).collect(); let mut process = Command::new(&self.command) - .args( - &[ - "-i", - "-f", - &email.from().to_string(), - &to_addresses.join(" "), - ], - ) + .args(&[ + "-i", + "-f", + &email.from().to_string(), + &to_addresses.join(" "), + ]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .spawn()?; - match process.stdin.as_mut().unwrap().write_all( - email.message().as_bytes(), - ) { + match process + .stdin + .as_mut() + .unwrap() + .write_all(email.message().as_bytes()) { Ok(_) => (), Err(error) => return Err(From::from(error)), } diff --git a/lettre/src/smtp/authentication.rs b/lettre/src/smtp/authentication.rs index 0af35ff..44a090d 100644 --- a/lettre/src/smtp/authentication.rs +++ b/lettre/src/smtp/authentication.rs @@ -21,7 +21,8 @@ pub const DEFAULT_ENCRYPTED_MECHANISMS: &'static [Mechanism] = /// Accepted authentication mecanisms on an encrypted connection /// Trying LOGIN last as it is deprecated. #[cfg(not(feature = "crammd5-auth"))] -pub const DEFAULT_ENCRYPTED_MECHANISMS: &'static [Mechanism] = &[Mechanism::Plain, Mechanism::Login]; +pub const DEFAULT_ENCRYPTED_MECHANISMS: &'static [Mechanism] = + &[Mechanism::Plain, Mechanism::Login]; /// Accepted authentication mecanisms on an unencrypted connection #[cfg(feature = "crammd5-auth")] @@ -212,7 +213,7 @@ mod test { mechanism .response( &credentials, - Some("PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg=="), + Some("PDE3ODkzLjEzMjA2NzkxMjNAdGVzc2VyYWN0LnN1c2FtLmluPg==") ) .unwrap(), "alice a540ebe4ef2304070bbc3c456c1f64c0" diff --git a/lettre/src/smtp/client/mod.rs b/lettre/src/smtp/client/mod.rs index 954ddb9..d61a726 100644 --- a/lettre/src/smtp/client/mod.rs +++ b/lettre/src/smtp/client/mod.rs @@ -156,9 +156,8 @@ impl Client { // TODO let mut challenges = 10; - let mut response = self.smtp_command( - AuthCommand::new(mechanism, credentials.clone(), None)?, - )?; + let mut response = + self.smtp_command(AuthCommand::new(mechanism, credentials.clone(), None)?)?; while challenges > 0 && response.has_code(334) { challenges -= 1; diff --git a/lettre/src/smtp/commands.rs b/lettre/src/smtp/commands.rs index ce79acb..6e5e917 100644 --- a/lettre/src/smtp/commands.rs +++ b/lettre/src/smtp/commands.rs @@ -27,7 +27,9 @@ impl Display for EhloCommand { impl EhloCommand { /// Creates a EHLO command pub fn new(client_id: ClientId) -> EhloCommand { - EhloCommand { client_id: client_id } + EhloCommand { + client_id: client_id, + } } } @@ -231,11 +233,7 @@ impl Display for AuthCommand { }; if self.mechanism.supports_initial_response() { - write!(f, - "AUTH {} {}", - self.mechanism, - encoded_response.unwrap(), - )?; + write!(f, "AUTH {} {}", self.mechanism, encoded_response.unwrap(),)?; } else { match encoded_response { Some(response) => f.write_str(&response)?, @@ -254,10 +252,10 @@ 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_ref().map(String::as_str))?, + ) } else { None }; @@ -299,10 +297,10 @@ impl AuthCommand { debug!("auth decoded challenge: {}", decoded_challenge); - let response = Some(mechanism.response( - &credentials, - Some(decoded_challenge.as_ref()), - )?); + let response = Some( + mechanism + .response(&credentials, Some(decoded_challenge.as_ref()))?, + ); Ok(AuthCommand { mechanism: mechanism, diff --git a/lettre/src/smtp/extension.rs b/lettre/src/smtp/extension.rs index e010508..450ae74 100644 --- a/lettre/src/smtp/extension.rs +++ b/lettre/src/smtp/extension.rs @@ -153,9 +153,8 @@ impl ServerInfo { /// Checks if the server supports an ESMTP feature pub fn supports_auth_mechanism(&self, mechanism: Mechanism) -> bool { - self.features.contains( - &Extension::Authentication(mechanism), - ) + self.features + .contains(&Extension::Authentication(mechanism)) } } @@ -350,13 +349,9 @@ mod test { let mut features2 = HashSet::new(); assert!(features2.insert(Extension::EightBitMime)); - assert!(features2.insert( - Extension::Authentication(Mechanism::Plain), - )); + assert!(features2.insert(Extension::Authentication(Mechanism::Plain),)); #[cfg(feature = "crammd5-auth")] - assert!(features2.insert( - Extension::Authentication(Mechanism::CramMd5), - )); + assert!(features2.insert(Extension::Authentication(Mechanism::CramMd5),)); let server_info2 = ServerInfo { name: "me".to_string(), diff --git a/lettre/src/smtp/mod.rs b/lettre/src/smtp/mod.rs index ebbe654..e174c01 100644 --- a/lettre/src/smtp/mod.rs +++ b/lettre/src/smtp/mod.rs @@ -29,7 +29,8 @@ //! //! // Open a local connection on port 25 //! let mut mailer = -//! SmtpTransport::builder_localhost().unwrap().security_level(SecurityLevel::Opportunistic).build(); +//! SmtpTransport::builder_localhost().unwrap() +//! .security_level(SecurityLevel::Opportunistic).build(); //! // Send the email //! let result = mailer.send(email); //! @@ -59,7 +60,9 @@ //! // Add credentials for authentication //! .credentials(Credentials::new("username".to_string(), "password".to_string())) //! // Specify a TLS security level. You can also specify an TlsConnector with -//! // .tls_connector(TlsConnector::builder().unwrap().supported_protocols(vec![Protocol::Tlsv12]).build().unwrap()) +//! // .tls_connector(TlsConnector::builder().unwrap() +//! // .supported_protocols(&vec![Protocol::Tlsv12]) +//! // .build().unwrap()) //! .security_level(SecurityLevel::AlwaysEncrypt) //! // Enable SMTPUTF8 if the server supports it //! .smtp_utf8(true) @@ -95,8 +98,12 @@ //! let mut email_client: Client = Client::new(); //! let _ = email_client.connect(&("localhost", SMTP_PORT), None); //! let _ = email_client.smtp_command(EhloCommand::new(ClientId::new("my_hostname".to_string()))); -//! let _ = email_client.smtp_command(MailCommand::new(Some(EmailAddress::new("user@example.com".to_string())), vec![])); -//! let _ = email_client.smtp_command(RcptCommand::new(EmailAddress::new("user@example.org".to_string()), vec![])); +//! let _ = email_client.smtp_command( +//! MailCommand::new(Some(EmailAddress::new("user@example.com".to_string())), vec![]) +//! ); +//! let _ = email_client.smtp_command( +//! RcptCommand::new(EmailAddress::new("user@example.org".to_string()), vec![]) +//! ); //! let _ = email_client.smtp_command(DataCommand); //! let _ = email_client.message("Test email"); //! let _ = email_client.smtp_command(QuitCommand); @@ -341,7 +348,7 @@ impl SmtpTransport { pub fn builder(addr: A) -> Result { SmtpTransportBuilder::new(addr) } - /// Creates a new local SMTP client to port 25 + /// Creates a new local SMTP client to port 25 pub fn builder_localhost() -> Result { SmtpTransportBuilder::new(("localhost", SMTP_PORT)) } @@ -381,7 +388,7 @@ impl SmtpTransport { let ehlo_response = try_smtp!( self.client.smtp_command(EhloCommand::new( ClientId::new(self.client_info.hello_name.to_string()), - )), + ),), self ); @@ -425,9 +432,10 @@ impl EmailTransport for SmtpTransport { match ( &self.client_info.security_level, - self.server_info.as_ref().unwrap().supports_feature( - Extension::StartTls, - ), + self.server_info + .as_ref() + .unwrap() + .supports_feature(Extension::StartTls), ) { (&SecurityLevel::AlwaysEncrypt, false) => { return Err(From::from("Could not encrypt connection, aborting")) @@ -438,9 +446,8 @@ impl EmailTransport for SmtpTransport { (_, true) => { try_smtp!(self.client.smtp_command(StarttlsCommand), self); try_smtp!( - self.client.upgrade_tls_stream( - &self.client_info.tls_connector, - ), + self.client + .upgrade_tls_stream(&self.client_info.tls_connector,), self ); @@ -467,16 +474,15 @@ impl EmailTransport for SmtpTransport { }; for mechanism in accepted_mechanisms { - if self.server_info.as_ref().unwrap().supports_auth_mechanism( - mechanism, - ) + if self.server_info + .as_ref() + .unwrap() + .supports_auth_mechanism(mechanism) { found = true; try_smtp!( - self.client.auth( - mechanism, - self.client_info.credentials.as_ref().unwrap(), - ), + self.client + .auth(mechanism, self.client_info.credentials.as_ref().unwrap(),), self ); break; @@ -492,25 +498,25 @@ impl EmailTransport for SmtpTransport { // Mail let mut mail_options = vec![]; - if self.server_info.as_ref().unwrap().supports_feature( - Extension::EightBitMime, - ) + if self.server_info + .as_ref() + .unwrap() + .supports_feature(Extension::EightBitMime) { mail_options.push(MailParameter::Body(MailBodyParameter::EightBitMime)); } - if self.server_info.as_ref().unwrap().supports_feature( - Extension::SmtpUtfEight, - ) + if self.server_info + .as_ref() + .unwrap() + .supports_feature(Extension::SmtpUtfEight) { mail_options.push(MailParameter::SmtpUtfEight); } try_smtp!( - self.client.smtp_command(MailCommand::new( - Some(email.from().clone()), - mail_options, - )), + self.client + .smtp_command(MailCommand::new(Some(email.from().clone()), mail_options,),), self ); @@ -520,9 +526,8 @@ impl EmailTransport for SmtpTransport { // Recipient for to_address in &email.to() { try_smtp!( - self.client.smtp_command( - RcptCommand::new(to_address.clone(), vec![]), - ), + self.client + .smtp_command(RcptCommand::new(to_address.clone(), vec![]),), self ); // Log the rcpt command diff --git a/lettre/src/smtp/response.rs b/lettre/src/smtp/response.rs index 9e0a741..e999eb0 100644 --- a/lettre/src/smtp/response.rs +++ b/lettre/src/smtp/response.rs @@ -209,7 +209,7 @@ impl ResponseParser { if code.to_string() != line[0..3] { return Err(Error::ResponseParsing( "Response code has changed during a \ - reponse", + reponse", )); } } @@ -231,7 +231,7 @@ impl ResponseParser { None => { Err(Error::ResponseParsing( "Incomplete response, could not read response \ - code", + code", )) } } @@ -274,9 +274,9 @@ impl Response { /// Returns only the first word of the message if possible pub fn first_word(&self) -> Option<&str> { - self.message.get(0).and_then( - |line| line.split_whitespace().next(), - ) + self.message + .get(0) + .and_then(|line| line.split_whitespace().next()) } /// Returns only the line of the message if possible diff --git a/lettre/src/smtp/util.rs b/lettre/src/smtp/util.rs index 0c08c0b..7afc111 100644 --- a/lettre/src/smtp/util.rs +++ b/lettre/src/smtp/util.rs @@ -39,8 +39,7 @@ mod tests { ("bjørn", "bjørn"), ("Ø+= ❤️‰", "Ø+2B+3D+20❤️‰"), ("+", "+2B"), - ] - { + ] { assert_eq!(format!("{}", XText(input)), expect); } } diff --git a/lettre/tests/transport_file.rs b/lettre/tests/transport_file.rs index dc2431f..70d818c 100644 --- a/lettre/tests/transport_file.rs +++ b/lettre/tests/transport_file.rs @@ -34,7 +34,7 @@ mod test { assert_eq!( buffer, "{\"to\":[\"root@localhost\"],\"from\":\"user@localhost\",\ - \"message_id\":\"file_id\",\"message\":\"Hello file\"}" + \"message_id\":\"file_id\",\"message\":\"Hello file\"}" ); remove_file(file).unwrap(); diff --git a/lettre_email/examples/smtp.rs b/lettre_email/examples/smtp.rs index 8a84d01..5448315 100644 --- a/lettre_email/examples/smtp.rs +++ b/lettre_email/examples/smtp.rs @@ -1,7 +1,7 @@ extern crate lettre; extern crate lettre_email; -use lettre::{SmtpTransport, EmailTransport}; +use lettre::{EmailTransport, SmtpTransport}; use lettre_email::email::EmailBuilder; fn main() { diff --git a/lettre_email/src/email.rs b/lettre_email/src/email.rs index c31f789..a872e7d 100644 --- a/lettre_email/src/email.rs +++ b/lettre_email/src/email.rs @@ -330,7 +330,9 @@ impl Display for Email { impl PartBuilder { /// Creates a new empty part pub fn new() -> PartBuilder { - PartBuilder { message: MimeMessage::new_blank_message() } + PartBuilder { + message: MimeMessage::new_blank_message(), + } } /// Adds a generic header @@ -513,9 +515,8 @@ impl EmailBuilder { /// Adds a `Subject` header pub fn set_subject>(&mut self, subject: S) { - self.message.add_header( - ("Subject".to_string(), subject.into()), - ); + self.message + .add_header(("Subject".to_string(), subject.into())); } /// Adds a `Date` header with the given date @@ -526,9 +527,8 @@ impl EmailBuilder { /// Adds a `Date` header with the given date pub fn set_date(&mut self, date: &Tm) { - self.message.add_header( - ("Date", Tm::rfc822z(date).to_string()), - ); + self.message + .add_header(("Date", Tm::rfc822z(date).to_string())); self.date_issued = true; } @@ -578,10 +578,8 @@ impl EmailBuilder { /// Sets the email body to HTML content pub fn set_html>(&mut self, body: S) { self.message.set_body(body); - self.message.add_header(( - "Content-Type", - format!("{}", mime::TEXT_HTML).as_ref(), - )); + self.message + .add_header(("Content-Type", format!("{}", mime::TEXT_HTML).as_ref())); } /// Sets the email content @@ -666,9 +664,10 @@ impl EmailBuilder { // we need to generate the envelope let mut e = Envelope::new(); // add all receivers in to_header and cc_header - for receiver in self.to_header.iter().chain(self.cc_header.iter()).chain( - self.bcc_header.iter(), - ) + for receiver in self.to_header + .iter() + .chain(self.cc_header.iter()) + .chain(self.bcc_header.iter()) { match *receiver { Address::Mailbox(ref m) => e.add_to(m.address.clone()), @@ -709,12 +708,8 @@ impl EmailBuilder { // Add the collected addresses as mailbox-list all at once. // The unwraps are fine because the conversions for Vec
never errs. if !self.to_header.is_empty() { - self.message.add_header( - Header::new_with_value( - "To".into(), - self.to_header, - ).unwrap(), - ); + self.message + .add_header(Header::new_with_value("To".into(), self.to_header).unwrap()); } if !self.from_header.is_empty() { self.message.add_header( @@ -724,12 +719,8 @@ impl EmailBuilder { return Err(Error::MissingFrom); } if !self.cc_header.is_empty() { - self.message.add_header( - Header::new_with_value( - "Cc".into(), - self.cc_header, - ).unwrap(), - ); + self.message + .add_header(Header::new_with_value("Cc".into(), self.cc_header).unwrap()); } if !self.reply_to_header.is_empty() { self.message.add_header( @@ -738,10 +729,8 @@ impl EmailBuilder { } if !self.date_issued { - self.message.add_header(( - "Date", - Tm::rfc822z(&now()).to_string().as_ref(), - )); + self.message + .add_header(("Date", Tm::rfc822z(&now()).to_string().as_ref())); } self.message.add_header(("MIME-Version", "1.0")); @@ -751,8 +740,7 @@ impl EmailBuilder { if let Ok(header) = Header::new_with_value( "Message-ID".to_string(), format!("<{}.lettre@localhost>", message_id), - ) - { + ) { self.message.add_header(header) } @@ -766,7 +754,11 @@ impl EmailBuilder { impl SendableEmail for Email { fn to(&self) -> Vec { - self.envelope.to.iter().map(|x| EmailAddress::new(x.clone())).collect() + self.envelope + .to + .iter() + .map(|x| EmailAddress::new(x.clone())) + .collect() } fn from(&self) -> EmailAddress { @@ -838,10 +830,10 @@ mod test { format!("{}", email), format!( "Subject: Hello\r\nContent-Type: text/plain; \ - charset=utf-8\r\nX-test: value\r\nTo: \r\nFrom: \ - \r\nCc: \"Alias\" \r\nReply-To: \ - \r\nDate: {}\r\nMIME-Version: 1.0\r\nMessage-ID: \ - <{}.lettre@localhost>\r\n\r\nHello World!\r\n", + charset=utf-8\r\nX-test: value\r\nTo: \r\nFrom: \ + \r\nCc: \"Alias\" \r\nReply-To: \ + \r\nDate: {}\r\nMIME-Version: 1.0\r\nMessage-ID: \ + <{}.lettre@localhost>\r\n\r\nHello World!\r\n", date_now.rfc822z(), email.message_id() ) @@ -869,8 +861,7 @@ mod test { ); email.message.headers.insert( - Header::new_with_value("To".to_string(), "to@example.com".to_string()) - .unwrap(), + Header::new_with_value("To".to_string(), "to@example.com".to_string()).unwrap(), ); email.message.body = "body".to_string(); @@ -902,9 +893,9 @@ mod test { format!("{}", email), format!( "Date: {}\r\nSubject: Invitation\r\nSender: \ - \r\nTo: \r\nFrom: \ - , \r\nMIME-Version: \ - 1.0\r\nMessage-ID: <{}.lettre@localhost>\r\n\r\nWe invite you!\r\n", + \r\nTo: \r\nFrom: \ + , \r\nMIME-Version: \ + 1.0\r\nMessage-ID: <{}.lettre@localhost>\r\n\r\nWe invite you!\r\n", date_now.rfc822z(), email.message_id() ) @@ -933,10 +924,10 @@ mod test { format!("{}", email), format!( "Date: {}\r\nSubject: Hello\r\nX-test: value\r\nSender: \ - \r\nTo: \r\nFrom: \ - \r\nCc: \"Alias\" \r\nReply-To: \ - \r\nMIME-Version: 1.0\r\nMessage-ID: \ - <{}.lettre@localhost>\r\n\r\nHello World!\r\n", + \r\nTo: \r\nFrom: \ + \r\nCc: \"Alias\" \r\nReply-To: \ + \r\nMIME-Version: 1.0\r\nMessage-ID: \ + <{}.lettre@localhost>\r\n\r\nHello World!\r\n", date_now.rfc822z(), email.message_id() ) diff --git a/rustfmt.toml b/rustfmt.toml index 0468aa5..c10d7f1 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -1,2 +1,3 @@ +write_mode = "Overwrite" reorder_imports = true reorder_imported_names = true