From 69f1405b9c6c396f1c136c41f091ac99d7cdb0ae Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 19 Apr 2017 19:15:57 +0100 Subject: [PATCH] Add a chatstate parser. --- src/chatstates.rs | 80 +++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/ns.rs | 1 + 3 files changed, 82 insertions(+) create mode 100644 src/chatstates.rs diff --git a/src/chatstates.rs b/src/chatstates.rs new file mode 100644 index 00000000..6ec2e22d --- /dev/null +++ b/src/chatstates.rs @@ -0,0 +1,80 @@ +use minidom::Element; + +use error::Error; + +use ns::CHATSTATES_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", CHATSTATES_NS) { + Ok(ChatState::Active) + } else if root.is("composing", CHATSTATES_NS) { + Ok(ChatState::Composing) + } else if root.is("gone", CHATSTATES_NS) { + Ok(ChatState::Gone) + } else if root.is("inactive", CHATSTATES_NS) { + Ok(ChatState::Inactive) + } else if root.is("paused", CHATSTATES_NS) { + Ok(ChatState::Paused) + } else { + Err(Error::ParseError("Unknown 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, "Unknown 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."); + } +} diff --git a/src/lib.rs b/src/lib.rs index d8a3f8cc..62996e9c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,3 +9,4 @@ pub mod media_element; pub mod ecaps2; pub mod jingle; pub mod ping; +pub mod chatstates; diff --git a/src/ns.rs b/src/ns.rs index 7c0903aa..84ca86e9 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -3,3 +3,4 @@ pub const DATA_FORMS_NS: &'static str = "jabber:x:data"; pub const MEDIA_ELEMENT_NS: &'static str = "urn:xmpp:media-element"; pub const JINGLE_NS: &'static str = "urn:xmpp:jingle:1"; pub const PING_NS: &'static str = "urn:xmpp:ping"; +pub const CHATSTATES_NS: &'static str = "http://jabber.org/protocol/chatstates";