use std::str::FromStr;
use std::fmt::{Display, Formatter, Result};
use std::result::Result as RResult;
use self::Severity::*;
use self::Category::*;
#[derive(PartialEq,Eq,Copy,Clone,Debug)]
pub enum Severity {
PositiveCompletion,
PositiveIntermediate,
TransientNegativeCompletion,
PermanentNegativeCompletion,
}
impl FromStr for Severity {
type Err = &'static str;
fn from_str(s: &str) -> RResult<Severity, &'static str> {
match s {
"2" => Ok(PositiveCompletion),
"3" => Ok(PositiveIntermediate),
"4" => Ok(TransientNegativeCompletion),
"5" => Ok(PermanentNegativeCompletion),
_ => Err("First digit must be between 2 and 5"),
}
}
}
impl Display for Severity {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}",
match *self {
PositiveCompletion => 2,
PositiveIntermediate => 3,
TransientNegativeCompletion => 4,
PermanentNegativeCompletion => 5,
}
)
}
}
#[derive(PartialEq,Eq,Copy,Clone,Debug)]
pub enum Category {
Syntax,
Information,
Connections,
Unspecified3,
Unspecified4,
MailSystem,
}
impl FromStr for Category {
type Err = &'static str;
fn from_str(s: &str) -> RResult<Category, &'static str> {
match s {
"0" => Ok(Syntax),
"1" => Ok(Information),
"2" => Ok(Connections),
"3" => Ok(Unspecified3),
"4" => Ok(Unspecified4),
"5" => Ok(MailSystem),
_ => Err("Second digit must be between 0 and 5"),
}
}
}
impl Display for Category {
fn fmt(&self, f: &mut Formatter) -> Result {
write!(f, "{}",
match *self {
Syntax => 0,
Information => 1,
Connections => 2,
Unspecified3 => 3,
Unspecified4 => 4,
MailSystem => 5,
}
)
}
}
#[derive(PartialEq,Eq,Clone,Debug)]
pub struct Response {
severity: Severity,
category: Category,
detail: u8,
message: Vec<String>
}
impl Response {
pub fn new(severity: Severity, category: Category, detail: u8, message: Vec<String>) -> Response {
Response {
severity: severity,
category: category,
detail: detail,
message: message
}
}
pub fn is_positive(&self) -> bool {
match self.severity {
PositiveCompletion => true,
PositiveIntermediate => true,
_ => false,
}
}
pub fn message(&self) -> Vec<String> {
self.message.clone()
}
pub fn severity(&self) -> Severity {
self.severity
}
pub fn category(&self) -> Category {
self.category
}
pub fn detail(&self) -> u8 {
self.detail
}
fn code(&self) -> String {
format!("{}{}{}", self.severity, self.category, self.detail)
}
pub fn has_code(&self, code: u16) -> bool {
self.code() == format!("{}", code)
}
pub fn first_word(&self) -> Option<String> {
match self.message.is_empty() {
true => None,
false => Some(self.message[0].split(" ").next().unwrap().to_string()),
}
}
}
#[cfg(test)]
mod test {
use super::{Severity, Category, Response};
#[test]
fn test_severity_from_str() {
assert_eq!("2".parse::<Severity>(), Ok(Severity::PositiveCompletion));
assert_eq!("4".parse::<Severity>(), Ok(Severity::TransientNegativeCompletion));
assert!("1".parse::<Severity>().is_err());
}
#[test]
fn test_severity_fmt() {
assert_eq!(format!("{}", Severity::PositiveCompletion), "2");
}
#[test]
fn test_category_from_str() {
assert_eq!("2".parse::<Category>(), Ok(Category::Connections));
assert_eq!("4".parse::<Category>(), Ok(Category::Unspecified4));
assert!("6".parse::<Category>().is_err());
}
#[test]
fn test_category_fmt() {
assert_eq!(format!("{}", Category::Unspecified4), "4");
}
#[test]
fn test_response_new() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
), Response {
severity: Severity::PositiveCompletion,
category: Category::Unspecified4,
detail: 1,
message: vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()],
});
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec![]
), Response {
severity: Severity::PositiveCompletion,
category: Category::Unspecified4,
detail: 1,
message: vec![],
});
}
#[test]
fn test_response_is_positive() {
assert!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).is_positive());
assert!(! Response::new(
"4".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).is_positive());
}
#[test]
fn test_response_message() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).message(), vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]);
let empty_message: Vec<String> = vec![];
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec![]
).message(), empty_message);
}
#[test]
fn test_response_severity() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).severity(), Severity::PositiveCompletion);
}
#[test]
fn test_response_category() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).category(), Category::Unspecified4);
}
#[test]
fn test_response_detail() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).detail(), 1);
}
#[test]
fn test_response_code() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).code(), "241");
}
#[test]
fn test_response_has_code() {
assert!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).has_code(241));
assert!(! Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).has_code(251));
}
#[test]
fn test_response_first_word() {
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).first_word(), Some("me".to_string()));
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec!["me mo".to_string(), "8BITMIME".to_string(), "SIZE 42".to_string()]
).first_word(), Some("me".to_string()));
assert_eq!(Response::new(
"2".parse::<Severity>().unwrap(),
"4".parse::<Category>().unwrap(),
1,
vec![]
).first_word(), None);
}
}