From 6c4a59456ddd4b443e2d8679de8d8a3560bcce28 Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Fri, 13 Mar 2015 23:14:46 +0100 Subject: [PATCH] Clean macros --- README.md | 2 +- src/client/mod.rs | 25 +------------------------ src/lib.rs | 3 +++ src/sender/mod.rs | 31 ++++++------------------------- 4 files changed, 11 insertions(+), 50 deletions(-) diff --git a/README.md b/README.md index 6534ce0..9232257 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -rust-smtp [![Build Status](https://travis-ci.org/amousset/rust-smtp.svg?branch=master)](https://travis-ci.org/amousset/rust-smtp) [![Gitter](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/amousset/rust-smtp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) +rust-smtp [![Build Status](https://travis-ci.org/amousset/rust-smtp.svg?branch=master)](https://travis-ci.org/amousset/rust-smtp) ========= This library implements a simple SMTP client. diff --git a/src/client/mod.rs b/src/client/mod.rs index d5869cf..1fa4ac3 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -35,35 +35,12 @@ pub struct Client { } -macro_rules! try_smtp ( - ($err: expr, $client: ident) => ({ - match $err { - Ok(val) => val, - Err(err) => return_err!(err, $client), - } - }) -); - macro_rules! return_err ( ($err: expr, $client: ident) => ({ return Err(FromError::from_error($err)) }) ); -macro_rules! check_response ( - ($result: ident) => ({ - match $result { - Ok(response) => { - match response.is_positive() { - true => Ok(response), - false => Err(FromError::from_error(response)), - } - }, - Err(_) => $result, - } - }) -); - impl Client { /// Creates a new SMTP client /// @@ -173,7 +150,7 @@ impl Client { /// Sends an AUTH command with CRAM-MD5 mecanism pub fn auth_cram_md5(&mut self, username: &str, password: &str) -> SmtpResult { - let encoded_challenge = try_smtp!(self.command("AUTH CRAM-MD5"), self).first_word().expect("No challenge"); + let encoded_challenge = try!(self.command("AUTH CRAM-MD5")).first_word().expect("No challenge"); self.command(format!("AUTH CRAM-MD5 {}", cram_md5(username, password, encoded_challenge.as_slice())).as_slice()) } diff --git a/src/lib.rs b/src/lib.rs index 0052b41..9c420d6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,6 +26,7 @@ //! ## Architecture //! //! This client is divided into three parts: +//! //! * `client`: a low level SMTP client providing all SMTP commands //! * `sender`: a high level SMTP client providing an easy method to send emails //! * `mailer`: generates the email to be sent with `sender` @@ -87,8 +88,10 @@ //! .credentials("username", "password") //! // Enable connection reuse //! .enable_connection_reuse(true).build(); +//! //! let result_1 = sender.send(email.clone()); //! assert!(result_1.is_ok()); +//! //! // The second email will use the same connection //! let result_2 = sender.send(email); //! assert!(result_2.is_ok()); diff --git a/src/sender/mod.rs b/src/sender/mod.rs index 6b45792..64d9c79 100644 --- a/src/sender/mod.rs +++ b/src/sender/mod.rs @@ -7,10 +7,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -//! SMTP client +//! Sends an email using the client use std::string::String; -use std::error::FromError; use std::net::TcpStream; use std::net::{SocketAddr, ToSocketAddrs}; use std::io::{Read, Write}; @@ -119,31 +118,13 @@ macro_rules! try_smtp ( ($err: expr, $client: ident) => ({ match $err { Ok(val) => val, - Err(err) => close_and_return_err!(err, $client), - } - }) -); - -macro_rules! close_and_return_err ( - ($err: expr, $client: ident) => ({ - if !$client.state.panic { - $client.state.panic = true; - $client.client.close(); - } - return Err(FromError::from_error($err)) - }) -); - -macro_rules! check_response ( - ($result: ident) => ({ - match $result { - Ok(response) => { - match response.is_positive() { - true => Ok(response), - false => Err(FromError::from_error(response)), + Err(err) => { + if !$client.state.panic { + $client.state.panic = true; + $client.client.close(); } + return Err(err) }, - Err(_) => $result, } }) );