#![unstable]
use std::str::FromStr;
use std::fmt::{Show, Formatter, Result};
use common::CRLF;
use response::Response;
use self::Extension::{EightBitMime, SmtpUtfEight, StartTls, Size};
#[deriving(PartialEq,Eq,Copy,Clone)]
pub enum Extension {
EightBitMime,
SmtpUtfEight,
StartTls,
Size(uint),
}
impl Show for Extension {
fn fmt(&self, f: &mut Formatter) -> Result {
f.write(
match self {
&EightBitMime => "8BITMIME".to_string(),
&SmtpUtfEight => "SMTPUTF8".to_string(),
&StartTls => "STARTTLS".to_string(),
&Size(ref size) => format!("SIZE={}", size)
}.as_bytes()
)
}
}
impl FromStr for Extension {
fn from_str(s: &str) -> Option<Extension> {
let splitted : Vec<&str> = s.splitn(1, ' ').collect();
match splitted.len() {
1 => match splitted[0] {
"8BITMIME" => Some(EightBitMime),
"SMTPUTF8" => Some(SmtpUtfEight),
"STARTTLS" => Some(StartTls),
_ => None,
},
2 => match (splitted[0], from_str::<uint>(splitted[1])) {
("SIZE", Some(size)) => Some(Size(size)),
_ => None,
},
_ => None,
}
}
}
impl Extension {
pub fn same_extension_as(&self, other: &Extension) -> bool {
if self == other {
return true;
}
match (self, other) {
(&Size(_), &Size(_)) => true,
_ => false,
}
}
pub fn parse_esmtp_response(message: &str) -> Option<Vec<Extension>> {
let mut esmtp_features = Vec::new();
for line in message.split_str(CRLF) {
if let Some(Response{code: 250, message}) = from_str::<Response>(line) {
if let Some(keyword) = from_str::<Extension>(message.unwrap().as_slice()) {
esmtp_features.push(keyword);
};
}
}
Some(esmtp_features)
}
pub fn client_mail_option(&self) -> Option<&str> {
match *self {
EightBitMime => Some("BODY=8BITMIME"),
_ => None,
}
}
}
#[cfg(test)]
mod test {
use super::Extension;
#[test]
fn test_fmt() {
assert_eq!(format!("{}", Extension::EightBitMime), "8BITMIME".to_string());
assert_eq!(format!("{}", Extension::Size(42)), "SIZE=42".to_string());
}
#[test]
fn test_from_str() {
assert_eq!(from_str::<Extension>("8BITMIME"), Some(Extension::EightBitMime));
assert_eq!(from_str::<Extension>("SIZE 42"), Some(Extension::Size(42)));
assert_eq!(from_str::<Extension>("SIZ 42"), None);
assert_eq!(from_str::<Extension>("SIZE 4a2"), None);
assert_eq!(from_str::<Extension>("SIZE 42 "), None);
}
#[test]
fn test_same_extension_as() {
assert_eq!(Extension::EightBitMime.same_extension_as(&Extension::EightBitMime), true);
assert_eq!(Extension::Size(42).same_extension_as(&Extension::Size(42)), true);
assert_eq!(Extension::Size(42).same_extension_as(&Extension::Size(43)), true);
assert_eq!(Extension::Size(42).same_extension_as(&Extension::EightBitMime), false);
assert_eq!(Extension::EightBitMime.same_extension_as(&Extension::SmtpUtfEight), false);
}
#[test]
fn test_parse_esmtp_response() {
assert_eq!(Extension::parse_esmtp_response("me\r\n250-8BITMIME\r\n250 SIZE 42"),
Some(vec![Extension::EightBitMime, Extension::Size(42)]));
assert_eq!(Extension::parse_esmtp_response("me\r\n250-8BITMIME\r\n250 UNKNON 42"),
Some(vec![Extension::EightBitMime]));
assert_eq!(Extension::parse_esmtp_response("me\r\n250-9BITMIME\r\n250 SIZE a"),
Some(vec![]));
assert_eq!(Extension::parse_esmtp_response("me\r\n250-SIZE 42\r\n250 SIZE 43"),
Some(vec![Extension::Size(42), Extension::Size(43)]));
assert_eq!(Extension::parse_esmtp_response(""),
Some(vec![]));
}
}