Add a ping parser.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-19 18:59:07 +01:00
parent 87cd047a46
commit fdc76eca3c
3 changed files with 55 additions and 0 deletions

View file

@ -8,3 +8,4 @@ pub mod data_forms;
pub mod media_element;
pub mod ecaps2;
pub mod jingle;
pub mod ping;

View file

@ -2,3 +2,4 @@ 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";
pub const JINGLE_NS: &'static str = "urn:xmpp:jingle:1";
pub const PING_NS: &'static str = "urn:xmpp:ping";

53
src/ping.rs Normal file
View file

@ -0,0 +1,53 @@
use minidom::Element;
use error::Error;
use ns::PING_NS;
#[derive(Debug)]
pub struct Ping {
}
pub fn parse_ping(root: &Element) -> Result<Ping, Error> {
assert!(root.is("ping", PING_NS));
for _ in root.children() {
return Err(Error::ParseError("Unknown child in ping element."));
}
Ok(Ping { })
}
#[cfg(test)]
mod tests {
use minidom::Element;
use error::Error;
use ping;
#[test]
fn test_simple() {
let elem: Element = "<ping xmlns='urn:xmpp:ping'/>".parse().unwrap();
ping::parse_ping(&elem).unwrap();
}
#[test]
fn test_invalid() {
let elem: Element = "<ping xmlns='urn:xmpp:ping'><coucou/></ping>".parse().unwrap();
let error = ping::parse_ping(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in ping element.");
}
#[test]
#[ignore]
fn test_invalid_attribute() {
let elem: Element = "<ping xmlns='urn:xmpp:ping' coucou=''/>".parse().unwrap();
let error = ping::parse_ping(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown attribute in ping element.");
}
}