From 1ac6150d50713b6169101bc1e13a5cb3beb9ebcc Mon Sep 17 00:00:00 2001 From: Alexis Mousset Date: Thu, 20 Nov 2014 17:04:06 +0100 Subject: [PATCH] Add enum imports --- src/client/mod.rs | 32 ++++++++++---------- src/command.rs | 2 ++ src/error.rs | 1 + src/extension.rs | 1 + src/transaction.rs | 74 +++++++++++++++++++++++----------------------- 5 files changed, 57 insertions(+), 53 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index 4eea89f..ce95d29 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -19,7 +19,7 @@ use common::{CRLF, MESSAGE_ENDING}; use response::Response; use extension; use extension::Extension; -use command; +//use command; use command::Command; use transaction::TransactionState; use error::{SmtpResult, ErrorKind}; @@ -150,7 +150,7 @@ impl Client { /// Connects to the configured server pub fn connect(&mut self) -> SmtpResult { - let command = command::Connect; + let command = Command::Connect; check_command_sequence!(command self); // Connect should not be called when the client is already connected @@ -226,7 +226,7 @@ impl Client { /// Send a HELO command and fills `server_info` pub fn helo(&mut self) -> SmtpResult { let hostname = self.my_hostname.clone(); - let result = try!(self.send_command(command::Hello(hostname))); + let result = try!(self.send_command(Command::Hello(hostname))); self.server_info = Some( ServerInfo{ name: get_first_word(result.message.as_ref().unwrap().as_slice()).to_string(), @@ -239,7 +239,7 @@ impl Client { /// Sends a EHLO command and fills `server_info` pub fn ehlo(&mut self) -> SmtpResult { let hostname = self.my_hostname.clone(); - let result = try!(self.send_command(command::ExtendedHello(hostname))); + let result = try!(self.send_command(Command::ExtendedHello(hostname))); self.server_info = Some( ServerInfo{ name: get_first_word(result.message.as_ref().unwrap().as_slice()).to_string(), @@ -264,26 +264,26 @@ impl Client { // Checks message encoding according to the server's capability // TODO : Add an encoding check. - let options = match server_info.supports_feature(extension::EightBitMime) { + let options = match server_info.supports_feature(Extension::EightBitMime) { Some(extension) => Some(vec![extension.client_mail_option().unwrap().to_string()]), None => None, }; self.send_command( - command::Mail(unquote_email_address(from_address).to_string(), options) + Command::Mail(unquote_email_address(from_address).to_string(), options) ) } /// Sends a RCPT command pub fn rcpt(&mut self, to_address: &str) -> SmtpResult { self.send_command( - command::Recipient(unquote_email_address(to_address).to_string(), None) + Command::Recipient(unquote_email_address(to_address).to_string(), None) ) } /// Sends a DATA command pub fn data(&mut self) -> SmtpResult { - self.send_command(command::Data) + self.send_command(Command::Data) } /// Sends the message content @@ -298,15 +298,15 @@ impl Client { }; // Check message encoding - if !server_info.supports_feature(extension::EightBitMime).is_some() { + if !server_info.supports_feature(Extension::EightBitMime).is_some() { if !message_content.is_ascii() { fail_with_err!("Server does not accepts UTF-8 strings" self); } } // Get maximum message size if defined and compare to the message size - match server_info.supports_feature(extension::Size(0)) { - Some(extension::Size(max)) if message_content.len() > max => + match server_info.supports_feature(Extension::Size(0)) { + Some(Extension::Size(max)) if message_content.len() > max => fail_with_err!(Response{ code: 552, message: Some("Message exceeds fixed maximum message size".to_string()), @@ -319,27 +319,27 @@ impl Client { /// Sends a QUIT command pub fn quit(&mut self) -> SmtpResult { - self.send_command(command::Quit) + self.send_command(Command::Quit) } /// Sends a RSET command pub fn rset(&mut self) -> SmtpResult { - self.send_command(command::Reset) + self.send_command(Command::Reset) } /// Sends a NOOP command pub fn noop(&mut self) -> SmtpResult { - self.send_command(command::Noop) + self.send_command(Command::Noop) } /// Sends a VRFY command pub fn vrfy(&mut self, to_address: &str) -> SmtpResult { - self.send_command(command::Verify(to_address.to_string())) + self.send_command(Command::Verify(to_address.to_string())) } /// Sends a EXPN command pub fn expn(&mut self, list: &str) -> SmtpResult { - self.send_command(command::Expand(list.to_string())) + self.send_command(Command::Expand(list.to_string())) } } diff --git a/src/command.rs b/src/command.rs index a1c2323..d0706d9 100644 --- a/src/command.rs +++ b/src/command.rs @@ -17,6 +17,8 @@ use std::fmt::{Show, Formatter, Result}; use response::Response; use error::SmtpResult; use common::SP; +use self::Command::{Connect, StartTls, ExtendedHello, Hello, Mail, Recipient, Data, Message, Reset, + Verify, Expand, Help, Quit}; /// Supported SMTP commands /// diff --git a/src/error.rs b/src/error.rs index b80847b..291afa4 100644 --- a/src/error.rs +++ b/src/error.rs @@ -16,6 +16,7 @@ use std::io::IoError; use std::error::FromError; use response::Response; +use self::ErrorKind::{TransientError, PermanentError, UnknownError, InternalIoError}; /// An enum of all error kinds. #[deriving(PartialEq, Eq, Clone, Show)] diff --git a/src/extension.rs b/src/extension.rs index 9db7988..5591561 100644 --- a/src/extension.rs +++ b/src/extension.rs @@ -16,6 +16,7 @@ use std::fmt::{Show, Formatter, Result}; use common::CRLF; use response::Response; +use self::Extension::{EightBitMime, SmtpUtfEight, StartTls, Size}; /// Supported ESMTP keywords #[deriving(PartialEq,Eq,Clone)] diff --git a/src/transaction.rs b/src/transaction.rs index 83e3010..65bf487 100644 --- a/src/transaction.rs +++ b/src/transaction.rs @@ -14,8 +14,8 @@ use std::fmt; use std::fmt::{Show, Formatter}; -use command; use command::Command; +use self::TransactionState::{Unconnected, Connected, HelloSent, MailSent, RecipientSent, DataSent}; /// Contains the state of the current transaction #[deriving(PartialEq,Eq,Clone)] @@ -58,25 +58,25 @@ impl TransactionState { /// Tests if the given command is allowed in the current state pub fn is_command_allowed(&self, command: &Command) -> bool { match (*self, command) { - (Unconnected, &command::Connect) => true, + (Unconnected, &Command::Connect) => true, (Unconnected, _) => false, // Only a message can follow the DATA command - (DataSent, &command::Message) => true, + (DataSent, &Command::Message) => true, (DataSent, _) => false, // Commands that can be issued everytime - (_, &command::ExtendedHello(_)) => true, - (_, &command::Hello(_)) => true, - (_, &command::Reset) => true, - (_, &command::Verify(_)) => true, - (_, &command::Expand(_)) => true, - (_, &command::Help(_)) => true, - (_, &command::Noop) => true, - (_, &command::Quit) => true, + (_, &Command::ExtendedHello(_)) => true, + (_, &Command::Hello(_)) => true, + (_, &Command::Reset) => true, + (_, &Command::Verify(_)) => true, + (_, &Command::Expand(_)) => true, + (_, &Command::Help(_)) => true, + (_, &Command::Noop) => true, + (_, &Command::Quit) => true, // Commands that require a particular state - (HelloSent, &command::Mail(_, _)) => true, - (MailSent, &command::Recipient(_, _)) => true, - (RecipientSent, &command::Recipient(_, _)) => true, - (RecipientSent, &command::Data) => true, + (HelloSent, &Command::Mail(_, _)) => true, + (MailSent, &Command::Recipient(_, _)) => true, + (RecipientSent, &Command::Recipient(_, _)) => true, + (RecipientSent, &Command::Data) => true, // Everything else (_, _) => false, } @@ -87,25 +87,25 @@ impl TransactionState { /// A `None` return value means the comand is not allowed. pub fn next_state(&mut self, command: &Command) -> Option { match (*self, command) { - (Unconnected, &command::Connect) => Some(Connected), + (Unconnected, &Command::Connect) => Some(Connected), (Unconnected, _) => None, - (DataSent, &command::Message) => Some(HelloSent), + (DataSent, &Command::Message) => Some(HelloSent), (DataSent, _) => None, // Commands that can be issued everytime - (_, &command::ExtendedHello(_)) => Some(HelloSent), - (_, &command::Hello(_)) => Some(HelloSent), - (Connected, &command::Reset) => Some(Connected), - (_, &command::Reset) => Some(HelloSent), - (state, &command::Verify(_)) => Some(state), - (state, &command::Expand(_)) => Some(state), - (state, &command::Help(_)) => Some(state), - (state, &command::Noop) => Some(state), - (_, &command::Quit) => Some(Unconnected), + (_, &Command::ExtendedHello(_)) => Some(HelloSent), + (_, &Command::Hello(_)) => Some(HelloSent), + (Connected, &Command::Reset) => Some(Connected), + (_, &Command::Reset) => Some(HelloSent), + (state, &Command::Verify(_)) => Some(state), + (state, &Command::Expand(_)) => Some(state), + (state, &Command::Help(_)) => Some(state), + (state, &Command::Noop) => Some(state), + (_, &Command::Quit) => Some(Unconnected), // Commands that require a particular state - (HelloSent, &command::Mail(_, _)) => Some(MailSent), - (MailSent, &command::Recipient(_, _)) => Some(RecipientSent), - (RecipientSent, &command::Recipient(_, _)) => Some(RecipientSent), - (RecipientSent, &command::Data) => Some(DataSent), + (HelloSent, &Command::Mail(_, _)) => Some(MailSent), + (MailSent, &Command::Recipient(_, _)) => Some(RecipientSent), + (RecipientSent, &Command::Recipient(_, _)) => Some(RecipientSent), + (RecipientSent, &Command::Data) => Some(DataSent), // Everything else (_, _) => None, } @@ -124,17 +124,17 @@ mod test { #[test] fn test_is_command_allowed() { - assert!(!super::Unconnected.is_command_allowed(&command::Noop)); - assert!(!super::DataSent.is_command_allowed(&command::Noop)); - assert!(super::HelloSent.is_command_allowed(&command::Mail("".to_string(), None))); - assert!(!super::MailSent.is_command_allowed(&command::Mail("".to_string(), None))); + assert!(!super::Unconnected.is_command_allowed(&Command::Noop)); + assert!(!super::DataSent.is_command_allowed(&Command::Noop)); + assert!(super::HelloSent.is_command_allowed(&Command::Mail("".to_string(), None))); + assert!(!super::MailSent.is_command_allowed(&Command::Mail("".to_string(), None))); } #[test] fn test_next_state() { - assert_eq!(super::MailSent.next_state(&command::Noop), Some(super::MailSent)); - assert_eq!(super::HelloSent.next_state(&command::Mail("".to_string(), None)), + assert_eq!(super::MailSent.next_state(&Command::Noop), Some(super::MailSent)); + assert_eq!(super::HelloSent.next_state(&Command::Mail("".to_string(), None)), Some(super::MailSent)); - assert_eq!(super::MailSent.next_state(&command::Mail("".to_string(), None)), None); + assert_eq!(super::MailSent.next_state(&Command::Mail("".to_string(), None)), None); } }