use std::fmt;
use std::fmt::{Show, Formatter};
use extension::Extension;
#[deriving(Clone)]
pub struct ServerInfo {
pub name: String,
pub esmtp_features: Option<Vec<Extension>>,
}
impl Show for ServerInfo {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
f.write(
format!("{} with {}",
self.name,
match self.esmtp_features {
Some(ref features) => features.to_string(),
None => "no supported features".to_string(),
}
).as_bytes()
)
}
}
impl ServerInfo {
pub fn supports_feature(&self, keyword: Extension) -> Option<Extension> {
match self.esmtp_features {
Some(ref esmtp_features) => {
for feature in esmtp_features.iter() {
if keyword.same_extension_as(feature) {
return Some(*feature);
}
}
None
},
None => None,
}
}
}
#[cfg(test)]
mod test {
use super::ServerInfo;
use extension::Extension;
#[test]
fn test_fmt() {
assert_eq!(format!("{}", ServerInfo{
name: "name".to_string(),
esmtp_features: Some(vec![Extension::EightBitMime])
}), "name with [8BITMIME]".to_string());
assert_eq!(format!("{}", ServerInfo{
name: "name".to_string(),
esmtp_features: Some(vec![Extension::EightBitMime, Extension::Size(42)])
}), "name with [8BITMIME, SIZE=42]".to_string());
assert_eq!(format!("{}", ServerInfo{
name: "name".to_string(),
esmtp_features: None
}), "name with no supported features".to_string());
}
#[test]
fn test_supports_feature() {
assert_eq!(ServerInfo{
name: "name".to_string(),
esmtp_features: Some(vec![Extension::EightBitMime])
}.supports_feature(Extension::EightBitMime), Some(Extension::EightBitMime));
assert_eq!(ServerInfo{
name: "name".to_string(),
esmtp_features: Some(vec![Extension::Size(42), Extension::EightBitMime])
}.supports_feature(Extension::EightBitMime), Some(Extension::EightBitMime));
assert_eq!(ServerInfo{
name: "name".to_string(),
esmtp_features: Some(vec![Extension::Size(42), Extension::EightBitMime])
}.supports_feature(Extension::Size(0)), Some(Extension::Size(42)));
assert!(ServerInfo{
name: "name".to_string(),
esmtp_features: Some(vec![Extension::EightBitMime])
}.supports_feature(Extension::Size(42)).is_none());
}
}