diff --git a/src/attention.rs b/src/attention.rs index 7b4226fb974e1b57e63c3278b01eabd5d0741ce5..398b2bfd2f3424189dca01d8875bfe0ea8025e7e 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 339ed2b534a094d5aa7e8bbdc527ea577c7e6008..c9fabd5264285d188ce3752cab97777bdd1d4454 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 cd0a85f2b9616bbe1e60511eab28283023995f8b..581e72e26184428f4320bb80429fa4f55f481d43 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 a276ba25e13a6fd683d5b29d66127d613e790301..f596dc8f8b4c17751121a364bc09d5ccc1cc84e7 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 ad4eb92e7ad28334ab429da45073b007fc9deb00..11850a29177e557c2a00d5c0c6ca11bdf237cb8b 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 7600d1049ad81567b76c027dfa35f970a645c42c..dab55a28a12c4ee5ec7cec0a12d9116ad8f9da37 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 6ff06bc3f1242b138cb71268624005015d3f39d7..04834518d8cb01f37ed43a8bcc014d0e82408888 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 10c0e51c64b23b2b4753fdc1ca2133d4b6bf01fe..898a5dd65cc351801f9c828d90f8764c2b1f291f 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 be2766144081942a90d7ffce153e2e457423b34e..37e71be0209127d278183f57cd60cfb8d45cdac0 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 07815107c6c9738920e238a0c888cea8a9fe557c..07e947a3e403e1fba0cc2130e4e1755d1f9fc860 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::*;