Clean macros

This commit is contained in:
Alexis Mousset
2015-03-13 23:14:46 +01:00
parent 269bf70a7f
commit 6c4a59456d
4 changed files with 11 additions and 50 deletions

View File

@@ -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.

View File

@@ -35,35 +35,12 @@ pub struct Client<S = TcpStream> {
}
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<S = TcpStream> Client<S> {
/// Creates a new SMTP client
///
@@ -173,7 +150,7 @@ impl<S: Connecter + Write + Read = TcpStream> Client<S> {
/// 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())
}

View File

@@ -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());

View File

@@ -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,
}
})
);