From fdc76eca3cc5ecc9079754dc933b3d15b293d9cc Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Wed, 19 Apr 2017 18:59:07 +0100 Subject: [PATCH] Add a ping parser. --- src/lib.rs | 1 + src/ns.rs | 1 + src/ping.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 src/ping.rs diff --git a/src/lib.rs b/src/lib.rs index 56d17f4..d8a3f8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,3 +8,4 @@ pub mod data_forms; pub mod media_element; pub mod ecaps2; pub mod jingle; +pub mod ping; diff --git a/src/ns.rs b/src/ns.rs index 55da2eb..7c0903a 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -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"; diff --git a/src/ping.rs b/src/ping.rs new file mode 100644 index 0000000..7176fa4 --- /dev/null +++ b/src/ping.rs @@ -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 { + 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 = "".parse().unwrap(); + ping::parse_ping(&elem).unwrap(); + } + + #[test] + fn test_invalid() { + let elem: Element = "".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 = "".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."); + } +}