diff --git a/src/attention.rs b/src/attention.rs index 7b4226fb..398b2bfd 100644 --- a/src/attention.rs +++ b/src/attention.rs @@ -4,11 +4,15 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; + generate_empty_element!( /// Requests the attention of the recipient. Attention, "attention", ATTENTION ); +impl MessagePayload for Attention {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/chatstates.rs b/src/chatstates.rs index 339ed2b5..c9fabd52 100644 --- a/src/chatstates.rs +++ b/src/chatstates.rs @@ -4,6 +4,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; + generate_element_enum!( /// Enum representing chatstate elements part of the /// `http://jabber.org/protocol/chatstates` namespace. @@ -25,6 +27,8 @@ generate_element_enum!( } ); +impl MessagePayload for ChatState {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/delay.rs b/src/delay.rs index cd0a85f2..581e72e2 100644 --- a/src/delay.rs +++ b/src/delay.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; use presence::PresencePayload; use date::DateTime; @@ -27,6 +28,7 @@ generate_element!( ) ); +impl MessagePayload for Delay {} impl PresencePayload for Delay {} #[cfg(test)] diff --git a/src/eme.rs b/src/eme.rs index a276ba25..f596dc8f 100644 --- a/src/eme.rs +++ b/src/eme.rs @@ -4,6 +4,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; + generate_element!( /// Structure representing an `` element. ExplicitMessageEncryption, "encryption", EME, @@ -16,6 +18,8 @@ attributes: [ name: Option = "name" => optional, ]); +impl MessagePayload for ExplicitMessageEncryption {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/mam.rs b/src/mam.rs index ad4eb92e..11850a29 100644 --- a/src/mam.rs +++ b/src/mam.rs @@ -11,6 +11,7 @@ use jid::Jid; use error::Error; +use message::MessagePayload; use iq::{IqGetPayload, IqSetPayload, IqResultPayload}; use data_forms::DataForm; use rsm::{SetQuery, SetResult}; @@ -65,6 +66,8 @@ generate_element!( ] ); +impl MessagePayload for Result_ {} + generate_attribute!( /// True when the end of a MAM query has been reached. Complete, "complete", bool diff --git a/src/message.rs b/src/message.rs index 7600d104..dab55a28 100644 --- a/src/message.rs +++ b/src/message.rs @@ -17,95 +17,8 @@ use error::Error; use ns; -use stanza_error::StanzaError; -use chatstates::ChatState; -use receipts::{Request as ReceiptRequest, Received as ReceiptReceived}; -use delay::Delay; -use attention::Attention; -use message_correct::Replace; -use eme::ExplicitMessageEncryption; -use stanza_id::{StanzaId, OriginId}; -use mam::Result_ as MamResult; - -/// Lists every known payload of a ``. -#[derive(Debug, Clone)] -pub enum MessagePayload { - StanzaError(StanzaError), - ChatState(ChatState), - ReceiptRequest(ReceiptRequest), - ReceiptReceived(ReceiptReceived), - Delay(Delay), - Attention(Attention), - MessageCorrect(Replace), - ExplicitMessageEncryption(ExplicitMessageEncryption), - StanzaId(StanzaId), - OriginId(OriginId), - MamResult(MamResult), - - Unknown(Element), -} - -impl TryFrom for MessagePayload { - type Err = Error; - - fn try_from(elem: Element) -> Result { - Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) { - ("error", ns::DEFAULT_NS) => MessagePayload::StanzaError(StanzaError::try_from(elem)?), - - // XEP-0085 - ("active", ns::CHATSTATES) - | ("inactive", ns::CHATSTATES) - | ("composing", ns::CHATSTATES) - | ("paused", ns::CHATSTATES) - | ("gone", ns::CHATSTATES) => MessagePayload::ChatState(ChatState::try_from(elem)?), - - // XEP-0184 - ("request", ns::RECEIPTS) => MessagePayload::ReceiptRequest(ReceiptRequest::try_from(elem)?), - ("received", ns::RECEIPTS) => MessagePayload::ReceiptReceived(ReceiptReceived::try_from(elem)?), - - // XEP-0203 - ("delay", ns::DELAY) => MessagePayload::Delay(Delay::try_from(elem)?), - - // XEP-0224 - ("attention", ns::ATTENTION) => MessagePayload::Attention(Attention::try_from(elem)?), - - // XEP-0308 - ("replace", ns::MESSAGE_CORRECT) => MessagePayload::MessageCorrect(Replace::try_from(elem)?), - - // XEP-0313 - ("result", ns::MAM) => MessagePayload::MamResult(MamResult::try_from(elem)?), - - // XEP-0359 - ("stanza-id", ns::SID) => MessagePayload::StanzaId(StanzaId::try_from(elem)?), - ("origin-id", ns::SID) => MessagePayload::OriginId(OriginId::try_from(elem)?), - - // XEP-0380 - ("encryption", ns::EME) => MessagePayload::ExplicitMessageEncryption(ExplicitMessageEncryption::try_from(elem)?), - - _ => MessagePayload::Unknown(elem), - }) - } -} - -impl From for Element { - fn from(payload: MessagePayload) -> Element { - match payload { - MessagePayload::StanzaError(stanza_error) => stanza_error.into(), - MessagePayload::Attention(attention) => attention.into(), - MessagePayload::ChatState(chatstate) => chatstate.into(), - MessagePayload::ReceiptRequest(request) => request.into(), - MessagePayload::ReceiptReceived(received) => received.into(), - MessagePayload::Delay(delay) => delay.into(), - MessagePayload::MessageCorrect(replace) => replace.into(), - MessagePayload::ExplicitMessageEncryption(eme) => eme.into(), - MessagePayload::StanzaId(stanza_id) => stanza_id.into(), - MessagePayload::OriginId(origin_id) => origin_id.into(), - MessagePayload::MamResult(result) => result.into(), - - MessagePayload::Unknown(elem) => elem, - } - } -} +/// Should be implemented on every known payload of a ``. +pub trait MessagePayload: TryFrom + Into {} generate_attribute!( /// The type of a message. diff --git a/src/message_correct.rs b/src/message_correct.rs index 6ff06bc3..04834518 100644 --- a/src/message_correct.rs +++ b/src/message_correct.rs @@ -4,6 +4,8 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; + generate_element!( /// Defines that the message containing this payload should replace a /// previous message, identified by the id. @@ -14,6 +16,8 @@ generate_element!( ] ); +impl MessagePayload for Replace {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/receipts.rs b/src/receipts.rs index 10c0e51c..898a5dd6 100644 --- a/src/receipts.rs +++ b/src/receipts.rs @@ -4,12 +4,16 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; + generate_empty_element!( /// Requests that this message is acked by the final recipient once /// received. Request, "request", RECEIPTS ); +impl MessagePayload for Request {} + generate_element!( /// Notes that a previous message has correctly been received, it is /// referenced by its 'id' attribute. @@ -20,6 +24,8 @@ generate_element!( ] ); +impl MessagePayload for Received {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/stanza_error.rs b/src/stanza_error.rs index be276614..37e71be0 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -9,6 +9,7 @@ use std::collections::BTreeMap; use minidom::Element; +use message::MessagePayload; use presence::PresencePayload; use error::Error; use jid::Jid; @@ -213,6 +214,7 @@ pub struct StanzaError { pub other: Option, } +impl MessagePayload for StanzaError {} impl PresencePayload for StanzaError {} impl TryFrom for StanzaError { diff --git a/src/stanza_id.rs b/src/stanza_id.rs index 07815107..07e947a3 100644 --- a/src/stanza_id.rs +++ b/src/stanza_id.rs @@ -4,6 +4,7 @@ // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. +use message::MessagePayload; use jid::Jid; generate_element!( @@ -19,6 +20,8 @@ generate_element!( ] ); +impl MessagePayload for StanzaId {} + generate_element!( /// A hack for MUC before version 1.31 to track a message which may have /// its 'id' attribute changed. @@ -29,6 +32,8 @@ generate_element!( ] ); +impl MessagePayload for OriginId {} + #[cfg(test)] mod tests { use super::*;