From cfadff3bc9f13c9e007a35088f70ff3cdf08f5b0 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Fri, 21 Apr 2017 03:57:34 +0100 Subject: [PATCH] Add a Delayed Delivery parser. --- src/delay.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 6 +++++ src/ns.rs | 3 +++ 3 files changed, 75 insertions(+) create mode 100644 src/delay.rs diff --git a/src/delay.rs b/src/delay.rs new file mode 100644 index 00000000..f4f59356 --- /dev/null +++ b/src/delay.rs @@ -0,0 +1,66 @@ +use minidom::Element; + +use error::Error; + +use ns; + +#[derive(Debug, Clone)] +pub struct Delay { + pub from: Option, + pub stamp: String, + pub data: Option, +} + +pub fn parse_delay(root: &Element) -> Result { + if !root.is("delay", ns::DELAY) { + return Err(Error::ParseError("This is not a delay element.")); + } + for _ in root.children() { + return Err(Error::ParseError("Unknown child in delay element.")); + } + let from = root.attr("from").and_then(|value| value.parse().ok()); + let stamp = root.attr("stamp").ok_or(Error::ParseError("Mandatory argument 'stamp' not present in delay element."))?.to_owned(); + Ok(Delay { + from: from, + stamp: stamp, + data: None, + }) +} + +#[cfg(test)] +mod tests { + use minidom::Element; + use error::Error; + use delay; + + #[test] + fn test_simple() { + let elem: Element = "".parse().unwrap(); + let delay = delay::parse_delay(&elem).unwrap(); + assert_eq!(delay.from, Some(String::from("capulet.com"))); + assert_eq!(delay.stamp, "2002-09-10T23:08:25Z"); + assert_eq!(delay.data, None); + } + + #[test] + fn test_unknown() { + let elem: Element = "".parse().unwrap(); + let error = delay::parse_delay(&elem).unwrap_err(); + let message = match error { + Error::ParseError(string) => string, + _ => panic!(), + }; + assert_eq!(message, "This is not a delay element."); + } + + #[test] + fn test_invalid_child() { + let elem: Element = "".parse().unwrap(); + let error = delay::parse_delay(&elem).unwrap_err(); + let message = match error { + Error::ParseError(string) => string, + _ => panic!(), + }; + assert_eq!(message, "Unknown child in delay element."); + } +} diff --git a/src/lib.rs b/src/lib.rs index 167f58ec..e00ccb52 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,6 +40,9 @@ pub mod receipts; /// XEP-0199: XMPP Ping pub mod ping; +/// XEP-0203: Delayed Delivery +pub mod delay; + /// XEP-0221: Data Forms Media Element pub mod media_element; @@ -61,6 +64,7 @@ pub enum MessagePayload { Body(body::Body), ChatState(chatstates::ChatState), Receipt(receipts::Receipt), + Delay(delay::Delay), Attention(attention::Attention), MessageCorrect(message_correct::MessageCorrect), ExplicitMessageEncryption(eme::ExplicitMessageEncryption), @@ -75,6 +79,8 @@ pub fn parse_message_payload(elem: &Element) -> Option { Some(MessagePayload::ChatState(chatstate)) } else if let Ok(receipt) = receipts::parse_receipt(elem) { Some(MessagePayload::Receipt(receipt)) + } else if let Ok(delay) = delay::parse_delay(elem) { + Some(MessagePayload::Delay(delay)) } else if let Ok(attention) = attention::parse_attention(elem) { Some(MessagePayload::Attention(attention)) } else if let Ok(replace) = message_correct::parse_message_correct(elem) { diff --git a/src/ns.rs b/src/ns.rs index 6aa1d3f7..8aefa2ee 100644 --- a/src/ns.rs +++ b/src/ns.rs @@ -22,6 +22,9 @@ pub const RECEIPTS: &'static str = "urn:xmpp:receipts"; /// XEP-0199: XMPP Ping pub const PING: &'static str = "urn:xmpp:ping"; +/// XEP-0203: Delayed Delivery +pub const DELAY: &'static str = "urn:xmpp:delay"; + /// XEP-0221: Data Forms Media Element pub const MEDIA_ELEMENT: &'static str = "urn:xmpp:media-element";