Rename SmtpError to Error

This commit is contained in:
Alexis Mousset
2015-07-14 01:10:28 +02:00
parent d46bbeebf0
commit 29c9b7661e
3 changed files with 20 additions and 28 deletions

View File

@@ -9,17 +9,17 @@
//! Error and result type for SMTP clients
use std::error::Error;
use std::error::Error as StdError;
use std::io;
use std::fmt::{Display, Formatter};
use std::fmt;
use response::{Severity, Response};
use self::SmtpError::*;
use self::Error::*;
/// An enum of all error kinds.
#[derive(Debug)]
pub enum SmtpError {
pub enum Error {
/// Transient SMTP error, 4xx reply code
///
/// [RFC 5321, section 4.2.1](https://tools.ietf.org/html/rfc5321#section-4.2.1)
@@ -34,13 +34,13 @@ pub enum SmtpError {
IoError(io::Error),
}
impl Display for SmtpError {
impl Display for Error {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), fmt::Error> {
fmt.write_str(self.description())
}
}
impl Error for SmtpError {
impl StdError for Error {
fn description(&self) -> &str {
match *self {
TransientError(_) => "a transient error occured during the SMTP transaction",
@@ -50,22 +50,22 @@ impl Error for SmtpError {
}
}
fn cause(&self) -> Option<&Error> {
fn cause(&self) -> Option<&StdError> {
match *self {
IoError(ref err) => Some(&*err as &Error),
IoError(ref err) => Some(&*err as &StdError),
_ => None,
}
}
}
impl From<io::Error> for SmtpError {
fn from(err: io::Error) -> SmtpError {
impl From<io::Error> for Error {
fn from(err: io::Error) -> Error {
IoError(err)
}
}
impl From<Response> for SmtpError {
fn from(response: Response) -> SmtpError {
impl From<Response> for Error {
fn from(response: Response) -> Error {
match response.severity() {
Severity::TransientNegativeCompletion => TransientError(response),
Severity::PermanentNegativeCompletion => PermanentError(response),
@@ -74,14 +74,14 @@ impl From<Response> for SmtpError {
}
}
impl From<&'static str> for SmtpError {
fn from(string: &'static str) -> SmtpError {
impl From<&'static str> for Error {
fn from(string: &'static str) -> Error {
ClientError(string.to_string())
}
}
/// SMTP result type
pub type SmtpResult = Result<Response, SmtpError>;
pub type SmtpResult = Result<Response, Error>;
#[cfg(test)]
mod test {

View File

@@ -15,7 +15,7 @@ use std::result::Result as RResult;
use self::Severity::*;
use self::Category::*;
use error::{SmtpResult, SmtpError};
use error::{SmtpResult, Error};
/// First digit indicates severity
#[derive(PartialEq,Eq,Copy,Clone,Debug)]
@@ -115,10 +115,10 @@ pub struct Code {
}
impl FromStr for Code {
type Err = SmtpError;
type Err = Error;
#[inline]
fn from_str(s: &str) -> RResult<Code, SmtpError> {
fn from_str(s: &str) -> RResult<Code, Error> {
if s.len() == 3 {
match (s[0..1].parse::<Severity>(), s[1..2].parse::<Category>(), s[2..3].parse::<u8>()) {
(Ok(severity), Ok(category), Ok(detail)) => Ok(Code {severity: severity, category: category, detail: detail}),
@@ -166,7 +166,7 @@ impl ResponseParser {
}
/// Parses a line and return a `bool` indicating if there are more lines to come
pub fn read_line(&mut self, line: &str) -> RResult<bool, SmtpError> {
pub fn read_line(&mut self, line: &str) -> RResult<bool, Error> {
if line.len() < 3 {
return Err(From::from("Could not parse reply code, line too short"));
@@ -176,7 +176,6 @@ impl ResponseParser {
self.code = Some(try!(line[0..3].parse::<Code>()));
} else {
if self.code.as_ref().unwrap().code() != line[0..3] {
println!("pouet");
return Err(From::from("Could not parse reply code"));
}
}
@@ -238,12 +237,6 @@ impl Response {
self.message.clone()
}
/// Gets the first line beginning with the given string
/// TODO testing
pub fn get_line_beginning_with(&self, start: &str) -> Option<String> {
self.message.iter().find(|&x| (*x).starts_with(start)).map(|s| s.to_string())
}
/// Returns the severity (i.e. 1st digit)
pub fn severity(&self) -> Severity {
self.code.severity
@@ -401,7 +394,6 @@ mod test {
"SIZE 42".to_string(), "AUTH PLAIN CRAM-MD5".to_string()],
}
);
}
#[test]

View File

@@ -14,7 +14,7 @@ use std::net::{SocketAddr, ToSocketAddrs};
use SMTP_PORT;
use extension::Extension;
use error::{SmtpResult, SmtpError};
use error::{SmtpResult, Error};
use sendable_email::SendableEmail;
use sender::server_info::ServerInfo;
use client::Client;
@@ -183,7 +183,7 @@ impl Sender {
});
},
Err(error) => match error {
SmtpError::PermanentError(ref response) if response.has_code(550) => {
Error::PermanentError(ref response) if response.has_code(550) => {
match self.client.helo(&self.client_info.hello_name) {
Ok(response) => {self.server_info = Some(
ServerInfo{