Add a receipts parser.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-20 00:43:33 +01:00
parent 861c933c56
commit f3c9a58862
3 changed files with 49 additions and 0 deletions

View file

@ -12,6 +12,7 @@ pub mod jingle;
pub mod ping; pub mod ping;
pub mod chatstates; pub mod chatstates;
pub mod ibb; pub mod ibb;
pub mod receipts;
use minidom::Element; use minidom::Element;
@ -19,6 +20,7 @@ use minidom::Element;
pub enum MessagePayload { pub enum MessagePayload {
Body(body::Body), Body(body::Body),
ChatState(chatstates::ChatState), ChatState(chatstates::ChatState),
Receipt(receipts::Receipt),
} }
pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> { pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
@ -26,6 +28,8 @@ pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
Some(MessagePayload::Body(body)) Some(MessagePayload::Body(body))
} else if let Ok(chatstate) = chatstates::parse_chatstate(elem) { } else if let Ok(chatstate) = chatstates::parse_chatstate(elem) {
Some(MessagePayload::ChatState(chatstate)) Some(MessagePayload::ChatState(chatstate))
} else if let Ok(receipt) = receipts::parse_receipt(elem) {
Some(MessagePayload::Receipt(receipt))
} else { } else {
None None
} }

View file

@ -6,3 +6,4 @@ pub const JINGLE_NS: &'static str = "urn:xmpp:jingle:1";
pub const PING_NS: &'static str = "urn:xmpp:ping"; pub const PING_NS: &'static str = "urn:xmpp:ping";
pub const CHATSTATES_NS: &'static str = "http://jabber.org/protocol/chatstates"; pub const CHATSTATES_NS: &'static str = "http://jabber.org/protocol/chatstates";
pub const IBB_NS: &'static str = "http://jabber.org/protocol/ibb"; pub const IBB_NS: &'static str = "http://jabber.org/protocol/ibb";
pub const RECEIPTS_NS: &'static str = "urn:xmpp:receipts";

44
src/receipts.rs Normal file
View file

@ -0,0 +1,44 @@
use minidom::Element;
use error::Error;
use ns::RECEIPTS_NS;
#[derive(Debug)]
pub enum Receipt {
Request,
Received(String),
}
pub fn parse_receipt(root: &Element) -> Result<Receipt, Error> {
for _ in root.children() {
return Err(Error::ParseError("Unknown child in receipt element."));
}
if root.is("request", RECEIPTS_NS) {
Ok(Receipt::Request)
} else if root.is("received", RECEIPTS_NS) {
let id = root.attr("id").unwrap_or("").to_owned();
Ok(Receipt::Received(id))
} else {
Err(Error::ParseError("This is not a receipt element."))
}
}
#[cfg(test)]
mod tests {
use minidom::Element;
//use error::Error;
use receipts;
#[test]
fn test_simple() {
let elem: Element = "<request xmlns='urn:xmpp:receipts'/>".parse().unwrap();
receipts::parse_receipt(&elem).unwrap();
let elem: Element = "<received xmlns='urn:xmpp:receipts'/>".parse().unwrap();
receipts::parse_receipt(&elem).unwrap();
let elem: Element = "<received xmlns='urn:xmpp:receipts' id='coucou'/>".parse().unwrap();
receipts::parse_receipt(&elem).unwrap();
}
}