Add a Last Message Correction parser.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-21 02:07:44 +01:00
parent 9410b9682a
commit 8fba5cf966
3 changed files with 54 additions and 0 deletions

View file

@ -45,6 +45,9 @@ pub mod media_element;
/// XEP-0224: Attention
pub mod attention;
/// XEP-0308: Last Message Correction
pub mod message_correct;
/// XEP-0390: Entity Capabilities 2.0
pub mod ecaps2;
@ -55,6 +58,7 @@ pub enum MessagePayload {
ChatState(chatstates::ChatState),
Receipt(receipts::Receipt),
Attention(attention::Attention),
MessageCorrect(message_correct::MessageCorrect),
}
/// Parse one of the payloads of a `<message/>` element, and return `Some` of a
@ -68,6 +72,8 @@ pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
Some(MessagePayload::Receipt(receipt))
} else if let Ok(attention) = attention::parse_attention(elem) {
Some(MessagePayload::Attention(attention))
} else if let Ok(replace) = message_correct::parse_message_correct(elem) {
Some(MessagePayload::MessageCorrect(replace))
} else {
None
}

45
src/message_correct.rs Normal file
View file

@ -0,0 +1,45 @@
use minidom::Element;
use error::Error;
use ns;
#[derive(Debug, Clone)]
pub enum MessageCorrect {
Replace(String),
}
pub fn parse_message_correct(root: &Element) -> Result<MessageCorrect, Error> {
if !root.is("replace", ns::MESSAGE_CORRECT) {
return Err(Error::ParseError("This is not a replace element."));
}
for _ in root.children() {
return Err(Error::ParseError("Unknown child in replace element."));
}
let id = root.attr("id").unwrap_or("").to_owned();
Ok(MessageCorrect::Replace(id))
}
#[cfg(test)]
mod tests {
use minidom::Element;
use error::Error;
use message_correct;
#[test]
fn test_simple() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'/>".parse().unwrap();
message_correct::parse_message_correct(&elem).unwrap();
}
#[test]
fn test_invalid_child() {
let elem: Element = "<replace xmlns='urn:xmpp:message-correct:0'><coucou/></replace>".parse().unwrap();
let error = message_correct::parse_message_correct(&elem).unwrap_err();
let message = match error {
Error::ParseError(string) => string,
_ => panic!(),
};
assert_eq!(message, "Unknown child in replace element.");
}
}

View file

@ -28,6 +28,9 @@ pub const MEDIA_ELEMENT: &'static str = "urn:xmpp:media-element";
/// XEP-0224: Attention
pub const ATTENTION: &'static str = "urn:xmpp:attention:0";
/// XEP-0308: Last Message Correction
pub const MESSAGE_CORRECT: &'static str = "urn:xmpp:message-correct:0";
/// XEP-0390: Entity Capabilities 2.0
pub const ECAPS2: &'static str = "urn:xmpp:caps";
/// XEP-0390: Entity Capabilities 2.0