From 948ecd7dd72147f1ae455a3d800cabc5cc0224dc Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 19 Apr 2017 23:21:23 +0100 Subject: [PATCH] Add a body plugin. --- src/body.rs | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + src/ns.rs | 1 + 3 files changed, 73 insertions(+) create mode 100644 src/body.rs diff --git a/src/body.rs b/src/body.rs new file mode 100644 index 00000000..0bf2f498 --- /dev/null +++ b/src/body.rs @@ -0,0 +1,71 @@ +use minidom::Element; + +use error::Error; +use super::MessagePayload; + +// TODO: also support components and servers. +use ns::JABBER_CLIENT_NS; + +#[derive(Debug)] +pub struct Body { + body: String, +} + +impl MessagePayload for Body {} + +pub fn parse_body(root: &Element) -> Result { + if !root.is("body", JABBER_CLIENT_NS) { + return Err(Error::ParseError("Not a body element.")); + } + for _ in root.children() { + return Err(Error::ParseError("Unknown child in body element.")); + } + Ok(Body { body: root.text() }) +} + +#[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 f7c964b9..e0798c12 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ extern crate minidom; pub mod error; pub mod ns; +pub mod body; pub mod disco; pub mod data_forms; pub mod media_element; diff --git a/src/ns.rs b/src/ns.rs index 24b81133..4f18ccc8 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -1,3 +1,4 @@ +pub const JABBER_CLIENT_NS: &'static str = "jabber:client"; pub const DISCO_INFO_NS: &'static str = "http://jabber.org/protocol/disco#info"; pub const DATA_FORMS_NS: &'static str = "jabber:x:data"; pub const MEDIA_ELEMENT_NS: &'static str = "urn:xmpp:media-element";