Add enum imports

This commit is contained in:
Alexis Mousset
2014-11-20 17:04:06 +01:00
parent 490eebbd02
commit 1ac6150d50
5 changed files with 57 additions and 53 deletions

View File

@@ -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<S: Connecter + ClientStream + Clone> Client<S> {
/// 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<S: Connecter + ClientStream + Clone> Client<S> {
/// Send a HELO command and fills `server_info`
pub fn helo<S>(&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<S: Connecter + ClientStream + Clone> Client<S> {
/// Sends a EHLO command and fills `server_info`
pub fn ehlo<S>(&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<S: Connecter + ClientStream + Clone> Client<S> {
// 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<S>(&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<S>(&mut self) -> SmtpResult {
self.send_command(command::Data)
self.send_command(Command::Data)
}
/// Sends the message content
@@ -298,15 +298,15 @@ impl<S: Connecter + ClientStream + Clone> Client<S> {
};
// 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<S: Connecter + ClientStream + Clone> Client<S> {
/// Sends a QUIT command
pub fn quit<S>(&mut self) -> SmtpResult {
self.send_command(command::Quit)
self.send_command(Command::Quit)
}
/// Sends a RSET command
pub fn rset<S>(&mut self) -> SmtpResult {
self.send_command(command::Reset)
self.send_command(Command::Reset)
}
/// Sends a NOOP command
pub fn noop<S>(&mut self) -> SmtpResult {
self.send_command(command::Noop)
self.send_command(Command::Noop)
}
/// Sends a VRFY command
pub fn vrfy<S>(&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<S>(&mut self, list: &str) -> SmtpResult {
self.send_command(command::Expand(list.to_string()))
self.send_command(Command::Expand(list.to_string()))
}
}

View File

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

View File

@@ -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)]

View File

@@ -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)]

View File

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