Add a body plugin.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-19 23:21:23 +01:00
parent 4f31727a1a
commit 948ecd7dd7
3 changed files with 73 additions and 0 deletions

71
src/body.rs Normal file
View file

@ -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<Body, Error> {
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 = "<active xmlns='http://jabber.org/protocol/chatstates'/>".parse().unwrap();
chatstates::parse_chatstate(&elem).unwrap();
}
#[test]
fn test_invalid() {
let elem: Element = "<coucou xmlns='http://jabber.org/protocol/chatstates'/>".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 = "<gone xmlns='http://jabber.org/protocol/chatstates'><coucou/></gone>".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 = "<inactive xmlns='http://jabber.org/protocol/chatstates' coucou=''/>".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.");
}
}

View file

@ -3,6 +3,7 @@ extern crate minidom;
pub mod error; pub mod error;
pub mod ns; pub mod ns;
pub mod body;
pub mod disco; pub mod disco;
pub mod data_forms; pub mod data_forms;
pub mod media_element; pub mod media_element;

View file

@ -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 DISCO_INFO_NS: &'static str = "http://jabber.org/protocol/disco#info";
pub const DATA_FORMS_NS: &'static str = "jabber:x:data"; pub const DATA_FORMS_NS: &'static str = "jabber:x:data";
pub const MEDIA_ELEMENT_NS: &'static str = "urn:xmpp:media-element"; pub const MEDIA_ELEMENT_NS: &'static str = "urn:xmpp:media-element";