use minidom::Element; use error::Error; use ns; #[derive(Debug)] pub enum ChatState { Active, Composing, Gone, Inactive, Paused, } pub fn parse_chatstate(root: &Element) -> Result { for _ in root.children() { return Err(Error::ParseError("Unknown child in chatstate element.")); } if root.is("active", ns::CHATSTATES) { Ok(ChatState::Active) } else if root.is("composing", ns::CHATSTATES) { Ok(ChatState::Composing) } else if root.is("gone", ns::CHATSTATES) { Ok(ChatState::Gone) } else if root.is("inactive", ns::CHATSTATES) { Ok(ChatState::Inactive) } else if root.is("paused", ns::CHATSTATES) { Ok(ChatState::Paused) } else { Err(Error::ParseError("This is not a chatstate element.")) } } #[cfg(test)] mod tests { use minidom::Element; use error::Error; use chatstates; #[test] fn test_simple() { let elem: Element = "".parse().unwrap(); chatstates::parse_chatstate(&elem).unwrap(); } #[test] fn test_invalid() { let elem: Element = "".parse().unwrap(); let error = chatstates::parse_chatstate(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "This is not a chatstate element."); } #[test] fn test_invalid_child() { let elem: Element = "".parse().unwrap(); let error = chatstates::parse_chatstate(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "Unknown child in chatstate element."); } #[test] #[ignore] fn test_invalid_attribute() { let elem: Element = "".parse().unwrap(); let error = chatstates::parse_chatstate(&elem).unwrap_err(); let message = match error { Error::ParseError(string) => string, _ => panic!(), }; assert_eq!(message, "Unknown attribute in chatstate element."); } }