Add a Delayed Delivery parser.
This commit is contained in:
parent
a680ab194c
commit
cfadff3bc9
3 changed files with 75 additions and 0 deletions
66
src/delay.rs
Normal file
66
src/delay.rs
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
use minidom::Element;
|
||||||
|
|
||||||
|
use error::Error;
|
||||||
|
|
||||||
|
use ns;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Delay {
|
||||||
|
pub from: Option<String>,
|
||||||
|
pub stamp: String,
|
||||||
|
pub data: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn parse_delay(root: &Element) -> Result<Delay, Error> {
|
||||||
|
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 = "<delay xmlns='urn:xmpp:delay' from='capulet.com' stamp='2002-09-10T23:08:25Z'/>".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 = "<replace xmlns='urn:xmpp:message-correct:0'/>".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 = "<delay xmlns='urn:xmpp:delay'><coucou/></delay>".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.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -40,6 +40,9 @@ pub mod receipts;
|
||||||
/// XEP-0199: XMPP Ping
|
/// XEP-0199: XMPP Ping
|
||||||
pub mod ping;
|
pub mod ping;
|
||||||
|
|
||||||
|
/// XEP-0203: Delayed Delivery
|
||||||
|
pub mod delay;
|
||||||
|
|
||||||
/// XEP-0221: Data Forms Media Element
|
/// XEP-0221: Data Forms Media Element
|
||||||
pub mod media_element;
|
pub mod media_element;
|
||||||
|
|
||||||
|
|
@ -61,6 +64,7 @@ pub enum MessagePayload {
|
||||||
Body(body::Body),
|
Body(body::Body),
|
||||||
ChatState(chatstates::ChatState),
|
ChatState(chatstates::ChatState),
|
||||||
Receipt(receipts::Receipt),
|
Receipt(receipts::Receipt),
|
||||||
|
Delay(delay::Delay),
|
||||||
Attention(attention::Attention),
|
Attention(attention::Attention),
|
||||||
MessageCorrect(message_correct::MessageCorrect),
|
MessageCorrect(message_correct::MessageCorrect),
|
||||||
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
|
||||||
|
|
@ -75,6 +79,8 @@ pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
|
||||||
Some(MessagePayload::ChatState(chatstate))
|
Some(MessagePayload::ChatState(chatstate))
|
||||||
} else if let Ok(receipt) = receipts::parse_receipt(elem) {
|
} else if let Ok(receipt) = receipts::parse_receipt(elem) {
|
||||||
Some(MessagePayload::Receipt(receipt))
|
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) {
|
} else if let Ok(attention) = attention::parse_attention(elem) {
|
||||||
Some(MessagePayload::Attention(attention))
|
Some(MessagePayload::Attention(attention))
|
||||||
} else if let Ok(replace) = message_correct::parse_message_correct(elem) {
|
} else if let Ok(replace) = message_correct::parse_message_correct(elem) {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,9 @@ pub const RECEIPTS: &'static str = "urn:xmpp:receipts";
|
||||||
/// XEP-0199: XMPP Ping
|
/// XEP-0199: XMPP Ping
|
||||||
pub const PING: &'static str = "urn: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
|
/// XEP-0221: Data Forms Media Element
|
||||||
pub const MEDIA_ELEMENT: &'static str = "urn:xmpp:media-element";
|
pub const MEDIA_ELEMENT: &'static str = "urn:xmpp:media-element";
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue