message: Make MessagePayload a trait, and implement it on every payload.
This commit is contained in:
parent
e41de29d9d
commit
07cccad356
10 changed files with 36 additions and 89 deletions
|
@ -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::*;
|
||||
|
|
|
@ -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::*;
|
||||
|
|
|
@ -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)]
|
||||
|
|
|
@ -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 `<encryption xmlns='urn:xmpp:eme:0'/>` element.
|
||||
ExplicitMessageEncryption, "encryption", EME,
|
||||
|
@ -16,6 +18,8 @@ attributes: [
|
|||
name: Option<String> = "name" => optional,
|
||||
]);
|
||||
|
||||
impl MessagePayload for ExplicitMessageEncryption {}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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 `<message/>`.
|
||||
#[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<Element> for MessagePayload {
|
||||
type Err = Error;
|
||||
|
||||
fn try_from(elem: Element) -> Result<MessagePayload, Error> {
|
||||
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<MessagePayload> 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 `<message/>`.
|
||||
pub trait MessagePayload: TryFrom<Element> + Into<Element> {}
|
||||
|
||||
generate_attribute!(
|
||||
/// The type of a message.
|
||||
|
|
|
@ -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::*;
|
||||
|
|
|
@ -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::*;
|
||||
|
|
|
@ -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<Element>,
|
||||
}
|
||||
|
||||
impl MessagePayload for StanzaError {}
|
||||
impl PresencePayload for StanzaError {}
|
||||
|
||||
impl TryFrom<Element> for StanzaError {
|
||||
|
|
|
@ -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::*;
|
||||
|
|
Loading…
Reference in a new issue