Add a message parser, along with a dependency on jid.

This commit is contained in:
Emmanuel Gil Peyrot 2017-04-23 15:13:03 +01:00
commit 5e7ad720c3
3 changed files with 241 additions and 37 deletions

View file

@ -1,13 +1,14 @@
//! A crate parsing common XMPP elements into Rust structures.
//!
//! The main entrypoint is `parse_message_payload`, it takes a minidom
//! `Element` reference and optionally returns `Some(MessagePayload)` if the
//! parsing succeeded.
//! Each module implements a `parse` function, which takes a minidom
//! `Element` reference and returns `Some(MessagePayload)` if the parsing
//! succeeded, None otherwise.
//!
//! Parsed structs can then be manipulated internally, and serialised back
//! before being sent over the wire.
extern crate minidom;
extern crate jid;
extern crate base64;
use minidom::Element;
@ -16,6 +17,8 @@ pub mod error;
/// XML namespace definitions used through XMPP.
pub mod ns;
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub mod message;
/// RFC 6120: Extensible Messaging and Presence Protocol (XMPP): Core
pub mod body;
@ -66,37 +69,3 @@ pub mod eme;
/// XEP-0390: Entity Capabilities 2.0
pub mod ecaps2;
/// Lists every known payload of a `<message/>`.
#[derive(Debug)]
pub enum MessagePayload {
Body(body::Body),
ChatState(chatstates::ChatState),
Receipt(receipts::Receipt),
Delay(delay::Delay),
Attention(attention::Attention),
MessageCorrect(message_correct::Replace),
ExplicitMessageEncryption(eme::ExplicitMessageEncryption),
}
/// Parse one of the payloads of a `<message/>` element, and return `Some` of a
/// `MessagePayload` if parsing it succeeded, `None` otherwise.
pub fn parse_message_payload(elem: &Element) -> Option<MessagePayload> {
if let Ok(body) = body::parse_body(elem) {
Some(MessagePayload::Body(body))
} else if let Ok(chatstate) = chatstates::parse_chatstate(elem) {
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_replace(elem) {
Some(MessagePayload::MessageCorrect(replace))
} else if let Ok(eme) = eme::parse_explicit_message_encryption(elem) {
Some(MessagePayload::ExplicitMessageEncryption(eme))
} else {
None
}
}