From b010126c19827aa4a722d7957e5b9643a976e831 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Sat, 18 Nov 2017 19:39:57 +0100 Subject: [PATCH] Update dependencies and improve style --- lettre/Cargo.toml | 4 +-- lettre/src/lib.rs | 2 +- lettre/src/smtp/authentication.rs | 4 +-- lettre/src/smtp/client/mock.rs | 4 +-- lettre/src/smtp/client/mod.rs | 9 ++++-- lettre/src/smtp/client/net.rs | 4 +-- lettre/src/smtp/commands.rs | 4 +-- lettre/src/smtp/error.rs | 2 +- lettre/src/smtp/mod.rs | 25 ++++++++-------- lettre/src/stub/mod.rs | 4 +-- lettre_email/examples/smtp.rs | 2 +- lettre_email/src/error.rs | 3 +- lettre_email/src/lib.rs | 47 +++++++++++++++++++++---------- 13 files changed, 64 insertions(+), 50 deletions(-) diff --git a/lettre/Cargo.toml b/lettre/Cargo.toml index da8f45b..b708956 100644 --- a/lettre/Cargo.toml +++ b/lettre/Cargo.toml @@ -18,8 +18,8 @@ travis-ci = { repository = "lettre/lettre" } log = "^0.3" bufstream = { version = "^0.1", optional = true } native-tls = { version = "^0.1", optional = true } -base64 = { version = "^0.7", optional = true } -hex = { version = "^0.2", optional = true } +base64 = { version = "^0.8", optional = true } +hex = { version = "^0.3", optional = true } rust-crypto = { version = "^0.2", optional = true } serde = { version = "^1.0", optional = true } serde_json = { version = "^1.0", optional = true } diff --git a/lettre/src/lib.rs b/lettre/src/lib.rs index 89d7fc9..6fbb5c1 100644 --- a/lettre/src/lib.rs +++ b/lettre/src/lib.rs @@ -37,7 +37,7 @@ pub use file::FileEmailTransport; #[cfg(feature = "sendmail-transport")] pub use sendmail::SendmailTransport; #[cfg(feature = "smtp-transport")] -pub use smtp::{SmtpTransport, ClientSecurity}; +pub use smtp::{ClientSecurity, SmtpTransport}; #[cfg(feature = "smtp-transport")] pub use smtp::client::net::ClientTlsParameters; use std::fmt::{self, Display, Formatter}; diff --git a/lettre/src/smtp/authentication.rs b/lettre/src/smtp/authentication.rs index 8b4ef4a..9db9fa5 100644 --- a/lettre/src/smtp/authentication.rs +++ b/lettre/src/smtp/authentication.rs @@ -7,7 +7,7 @@ use crypto::mac::Mac; #[cfg(feature = "crammd5-auth")] use crypto::md5::Md5; #[cfg(feature = "crammd5-auth")] -use hex::ToHex; +use hex; use smtp::NUL; use smtp::error::Error; use std::fmt::{self, Display, Formatter}; @@ -160,7 +160,7 @@ impl Mechanism { Ok(format!( "{} {}", credentials.username, - hmac.result().code().to_hex() + hex::encode(hmac.result().code()) )) } } diff --git a/lettre/src/smtp/client/mock.rs b/lettre/src/smtp/client/mock.rs index 7c8f956..e7989af 100644 --- a/lettre/src/smtp/client/mock.rs +++ b/lettre/src/smtp/client/mock.rs @@ -41,11 +41,11 @@ impl MockStream { vec } - pub fn next_vec(&mut self, vec: Vec) { + pub fn next_vec(&mut self, vec: &[u8]) { let mut cursor = self.reader.lock().unwrap(); cursor.set_position(0); cursor.get_mut().clear(); - cursor.get_mut().extend_from_slice(vec.as_slice()); + cursor.get_mut().extend_from_slice(vec); } pub fn swap(&mut self) { diff --git a/lettre/src/smtp/client/mod.rs b/lettre/src/smtp/client/mod.rs index 96dd488..56896f5 100644 --- a/lettre/src/smtp/client/mod.rs +++ b/lettre/src/smtp/client/mod.rs @@ -188,7 +188,7 @@ impl Client { response = self.smtp_command(AuthCommand::new_from_response( mechanism, credentials.clone(), - response, + &response, )?)?; } @@ -209,8 +209,11 @@ impl Client { out_buf.clear(); let consumed = match message_reader.fill_buf() { - Ok(bytes) => { codec.encode(bytes, &mut out_buf)?; bytes.len() }, - Err(ref err) => panic!("Failed with: {}", err) + Ok(bytes) => { + codec.encode(bytes, &mut out_buf)?; + bytes.len() + } + Err(ref err) => panic!("Failed with: {}", err), }; message_reader.consume(consumed); diff --git a/lettre/src/smtp/client/net.rs b/lettre/src/smtp/client/net.rs index 626a96e..45cde83 100644 --- a/lettre/src/smtp/client/net.rs +++ b/lettre/src/smtp/client/net.rs @@ -1,6 +1,6 @@ //! A trait to represent a stream -use native_tls::{TlsConnector, TlsStream, Protocol}; +use native_tls::{Protocol, TlsConnector, TlsStream}; use smtp::client::mock::MockStream; use std::io::{self, ErrorKind, Read, Write}; use std::net::{Ipv4Addr, Shutdown, SocketAddr, SocketAddrV4, TcpStream}; @@ -27,7 +27,7 @@ impl ClientTlsParameters { /// Accepted protocols by default. /// This removes TLS 1.0 compared to tls-native defaults. -pub const DEFAULT_TLS_PROTOCOLS : &'static [Protocol] = &[Protocol::Tlsv11, Protocol::Tlsv12]; +pub const DEFAULT_TLS_PROTOCOLS: &'static [Protocol] = &[Protocol::Tlsv11, Protocol::Tlsv12]; #[derive(Debug)] /// Represents the different types of underlying network streams diff --git a/lettre/src/smtp/commands.rs b/lettre/src/smtp/commands.rs index c3e1c24..ce51f10 100644 --- a/lettre/src/smtp/commands.rs +++ b/lettre/src/smtp/commands.rs @@ -269,7 +269,7 @@ impl AuthCommand { pub fn new_from_response( mechanism: Mechanism, credentials: Credentials, - response: Response, + response: &Response, ) -> Result { if !response.has_code(334) { return Err(Error::ResponseParsing("Expecting a challenge")); @@ -418,7 +418,7 @@ mod test { AuthCommand::new_from_response( Mechanism::CramMd5, credentials.clone(), - Response::new(Code::from_str("334").unwrap(), vec!["dGVzdAo=".to_string()]), + &Response::new(Code::from_str("334").unwrap(), vec!["dGVzdAo=".to_string()]), ).unwrap() ), "dXNlciA1NTIzNThiMzExOWFjOWNkYzM2YWRiN2MxNWRmMWJkNw==\r\n" diff --git a/lettre/src/smtp/error.rs b/lettre/src/smtp/error.rs index 67fdfa9..8309253 100644 --- a/lettre/src/smtp/error.rs +++ b/lettre/src/smtp/error.rs @@ -2,13 +2,13 @@ use self::Error::*; use base64::DecodeError; +use native_tls; use smtp::response::{Response, Severity}; use std::error::Error as StdError; use std::fmt; use std::fmt::{Display, Formatter}; use std::io; use std::string::FromUtf8Error; -use native_tls; /// An enum of all error kinds. #[derive(Debug)] diff --git a/lettre/src/smtp/mod.rs b/lettre/src/smtp/mod.rs index b86739f..6721d77 100644 --- a/lettre/src/smtp/mod.rs +++ b/lettre/src/smtp/mod.rs @@ -40,7 +40,6 @@ //! //! ```rust,no_run //! use lettre::smtp::authentication::{Credentials, Mechanism}; -//! use lettre::smtp::SUBMISSION_PORT; //! use lettre::{SimpleSendableEmail, EmailTransport, EmailAddress, SmtpTransport}; //! use lettre::smtp::extension::ClientId; //! use lettre::smtp::ConnectionReuseParameters; @@ -54,7 +53,7 @@ //! ); //! //! // Connect to a remote server on a custom port -//! let mut mailer = SmtpTransport::simple_builder("server.tld".to_string()).unwrap() +//! let mut mailer = SmtpTransport::simple_builder("server.tld").unwrap() //! // Set the name sent during EHLO/HELO, default is `localhost` //! .hello_name(ClientId::Domain("my.hostname.tld".to_string())) //! // Add credentials for authentication @@ -111,9 +110,9 @@ use smtp::authentication::{Credentials, DEFAULT_ENCRYPTED_MECHANISMS, DEFAULT_UNENCRYPTED_MECHANISMS, Mechanism}; use smtp::client::Client; use smtp::client::net::ClientTlsParameters; +use smtp::client::net::DEFAULT_TLS_PROTOCOLS; use smtp::commands::*; use smtp::error::{Error, SmtpResult}; -use smtp::client::net::DEFAULT_TLS_PROTOCOLS; use smtp::extension::{ClientId, Extension, MailBodyParameter, MailParameter, ServerInfo}; use std::io::Read; use std::net::{SocketAddr, ToSocketAddrs}; @@ -140,19 +139,19 @@ pub const SUBMISSION_PORT: u16 = 587; // Useful strings and characters /// The word separator for SMTP transactions -pub const SP: &'static str = " "; +pub const SP: &str = " "; /// The line ending for SMTP transactions (carriage return + line feed) -pub const CRLF: &'static str = "\r\n"; +pub const CRLF: &str = "\r\n"; /// Colon -pub const COLON: &'static str = ":"; +pub const COLON: &str = ":"; /// The ending of message content -pub const MESSAGE_ENDING: &'static str = "\r\n.\r\n"; +pub const MESSAGE_ENDING: &str = "\r\n.\r\n"; /// NUL unicode character -pub const NUL: &'static str = "\0"; +pub const NUL: &str = "\0"; /// How to apply TLS to a client connection #[derive(Clone)] @@ -322,18 +321,16 @@ impl<'a> SmtpTransport { /// Simple and secure transport, should be used when possible. /// Creates an encrypted transport over submission port, using the provided domain /// to validate TLS certificates. - pub fn simple_builder(domain: String) -> Result { + pub fn simple_builder(domain: &str) -> Result { let mut tls_builder = TlsConnector::builder()?; tls_builder.supported_protocols(DEFAULT_TLS_PROTOCOLS)?; - let tls_parameters = ClientTlsParameters::new( - domain.clone(), - tls_builder.build().unwrap(), - ); + let tls_parameters = + ClientTlsParameters::new(domain.to_string(), tls_builder.build().unwrap()); SmtpTransportBuilder::new( - (domain.as_ref(), SUBMISSION_PORT), + (domain, SUBMISSION_PORT), ClientSecurity::Required(tls_parameters), ) } diff --git a/lettre/src/stub/mod.rs b/lettre/src/stub/mod.rs index d0f385b..52a58db 100644 --- a/lettre/src/stub/mod.rs +++ b/lettre/src/stub/mod.rs @@ -41,9 +41,7 @@ impl StubEmailTransport { /// Creates a new transport that always returns a success response pub fn new_positive() -> StubEmailTransport { - StubEmailTransport { - response: Ok(()), - } + StubEmailTransport { response: Ok(()) } } } diff --git a/lettre_email/examples/smtp.rs b/lettre_email/examples/smtp.rs index 88cb892..761fbe9 100644 --- a/lettre_email/examples/smtp.rs +++ b/lettre_email/examples/smtp.rs @@ -14,7 +14,7 @@ fn main() { .from("user@example.com") .subject("Hi, Hello world") .text("Hello world.") - .attachment(Path::new("Cargo.toml"), None, mime::TEXT_PLAIN).unwrap() + .attachment(Path::new("Cargo.toml"), None, &mime::TEXT_PLAIN).unwrap() .build() .unwrap(); diff --git a/lettre_email/src/error.rs b/lettre_email/src/error.rs index 462c4e7..1323c65 100644 --- a/lettre_email/src/error.rs +++ b/lettre_email/src/error.rs @@ -2,8 +2,8 @@ use self::Error::*; use std::error::Error as StdError; -use std::io; use std::fmt::{self, Display, Formatter}; +use std::io; /// An enum of all error kinds. #[derive(Debug)] @@ -40,4 +40,3 @@ impl From for Error { Io(err) } } - diff --git a/lettre_email/src/lib.rs b/lettre_email/src/lib.rs index 069f7f0..8caea9a 100644 --- a/lettre_email/src/lib.rs +++ b/lettre_email/src/lib.rs @@ -66,11 +66,11 @@ pub use email_format::{Address, Header, Mailbox, MimeMessage, MimeMultipartType} use error::Error; use lettre::{EmailAddress, SendableEmail}; use mime::Mime; +use std::fs::File; +use std::io::Read; +use std::path::Path; use time::{Tm, now}; use uuid::Uuid; -use std::fs::File; -use std::path::Path; -use std::io::Read; /// Converts an address or an address with an alias to a `Header` pub trait IntoHeader { @@ -434,13 +434,13 @@ impl PartBuilder { } /// Adds a `ContentType` header with the given MIME type - pub fn content_type(mut self, content_type: Mime) -> PartBuilder { + pub fn content_type(mut self, content_type: &Mime) -> PartBuilder { self.set_content_type(content_type); self } /// Adds a `ContentType` header with the given MIME type - pub fn set_content_type(&mut self, content_type: Mime) { + pub fn set_content_type(&mut self, content_type: &Mime) { self.add_header(("Content-Type", format!("{}", content_type).as_ref())); } @@ -600,15 +600,25 @@ impl EmailBuilder { } /// Adds an attachment to the email - pub fn attachment(mut self, path: &Path, filename: Option<&str>, content_type: Mime) -> Result { + pub fn attachment( + mut self, + path: &Path, + filename: Option<&str>, + content_type: &Mime, + ) -> Result { self.set_attachment(path, filename, content_type)?; Ok(self) } /// Adds an attachment to the email /// If filename is not provided, the name of the file will be used. - pub fn set_attachment(&mut self, path: &Path, filename: Option<&str>, content_type: Mime) -> Result<(), Error> { - let file = File::open(path); + pub fn set_attachment( + &mut self, + path: &Path, + filename: Option<&str>, + content_type: &Mime, + ) -> Result<(), Error> { + let file = File::open(path); let body = match file { Ok(mut f) => { let mut data = String::new(); @@ -627,18 +637,25 @@ impl EmailBuilder { let actual_filename = match filename { Some(name) => name, - None => match path.file_name() { - Some(name) => match name.to_str() { - Some(name) => name, + None => { + match path.file_name() { + Some(name) => { + match name.to_str() { + Some(name) => name, + None => return Err(Error::CannotParseFilename), + } + } None => return Err(Error::CannotParseFilename), - }, - None => return Err(Error::CannotParseFilename), - }, + } + } }; let content = PartBuilder::new() .body(body) - .header(("Content-Disposition", format!("attachment; filename=\"{}\"", actual_filename))) + .header(( + "Content-Disposition", + format!("attachment; filename=\"{}\"", actual_filename), + )) .header(("Content-Type", content_type.to_string())) .build();