diff --git a/src/caps.rs b/src/caps.rs index a63ab7f7..77901ba1 100644 --- a/src/caps.rs +++ b/src/caps.rs @@ -6,6 +6,7 @@ use try_from::TryFrom; +use presence::PresencePayload; use disco::{Feature, Identity, DiscoInfoResult, DiscoInfoQuery}; use data_forms::DataForm; use hashes::{Hash, Algo}; @@ -39,6 +40,8 @@ pub struct Caps { pub hash: Hash, } +impl PresencePayload for Caps {} + impl TryFrom for Caps { type Err = Error; diff --git a/src/delay.rs b/src/delay.rs index 21450da0..cd0a85f2 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 presence::PresencePayload; use date::DateTime; use jid::Jid; @@ -26,6 +27,8 @@ generate_element!( ) ); +impl PresencePayload for Delay {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/ecaps2.rs b/src/ecaps2.rs index 4e40484d..82ab1c4a 100644 --- a/src/ecaps2.rs +++ b/src/ecaps2.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 presence::PresencePayload; use disco::{Feature, Identity, DiscoInfoResult, DiscoInfoQuery}; use data_forms::DataForm; use hashes::{Hash, Algo}; @@ -27,6 +28,8 @@ generate_element!( ] ); +impl PresencePayload for ECaps2 {} + fn compute_item(field: &str) -> Vec { let mut bytes = field.as_bytes().to_vec(); bytes.push(0x1f); diff --git a/src/idle.rs b/src/idle.rs index f96dadb5..27046547 100644 --- a/src/idle.rs +++ b/src/idle.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 presence::PresencePayload; use date::DateTime; generate_element!( @@ -15,6 +16,8 @@ generate_element!( ] ); +impl PresencePayload for Idle {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/muc/muc.rs b/src/muc/muc.rs index 4d81b85c..c363107a 100644 --- a/src/muc/muc.rs +++ b/src/muc/muc.rs @@ -5,6 +5,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 presence::PresencePayload; use date::DateTime; generate_element!( @@ -36,6 +37,8 @@ generate_element!( ] ); +impl PresencePayload for Muc {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/presence.rs b/src/presence.rs index 639bf046..e81358fb 100644 --- a/src/presence.rs +++ b/src/presence.rs @@ -19,12 +19,8 @@ use error::Error; use ns; -use stanza_error::StanzaError; -use muc::Muc; -use caps::Caps; -use delay::Delay; -use idle::Idle; -use ecaps2::ECaps2; +/// Should be implemented on every known payload of a ``. +pub trait PresencePayload: TryFrom + Into {} #[derive(Debug, Clone, PartialEq)] pub enum Show { @@ -79,61 +75,6 @@ pub type Status = String; pub type Priority = i8; -/// Lists every known payload of a ``. -#[derive(Debug, Clone)] -pub enum PresencePayload { - StanzaError(StanzaError), - Muc(Muc), - Caps(Caps), - Delay(Delay), - Idle(Idle), - ECaps2(ECaps2), - - Unknown(Element), -} - -impl TryFrom for PresencePayload { - type Err = Error; - - fn try_from(elem: Element) -> Result { - Ok(match (elem.name().as_ref(), elem.ns().unwrap().as_ref()) { - ("error", ns::DEFAULT_NS) => PresencePayload::StanzaError(StanzaError::try_from(elem)?), - - // XEP-0045 - ("x", ns::MUC) => PresencePayload::Muc(Muc::try_from(elem)?), - - // XEP-0115 - ("c", ns::CAPS) => PresencePayload::Caps(Caps::try_from(elem)?), - - // XEP-0203 - ("delay", ns::DELAY) => PresencePayload::Delay(Delay::try_from(elem)?), - - // XEP-0319 - ("idle", ns::IDLE) => PresencePayload::Idle(Idle::try_from(elem)?), - - // XEP-0390 - ("c", ns::ECAPS2) => PresencePayload::ECaps2(ECaps2::try_from(elem)?), - - _ => PresencePayload::Unknown(elem), - }) - } -} - -impl From for Element { - fn from(payload: PresencePayload) -> Element { - match payload { - PresencePayload::StanzaError(stanza_error) => stanza_error.into(), - PresencePayload::Muc(muc) => muc.into(), - PresencePayload::Caps(caps) => caps.into(), - PresencePayload::Delay(delay) => delay.into(), - PresencePayload::Idle(idle) => idle.into(), - PresencePayload::ECaps2(ecaps2) => ecaps2.into(), - - PresencePayload::Unknown(elem) => elem, - } - } -} - #[derive(Debug, Clone, PartialEq)] pub enum Type { /// This value is not an acceptable 'type' attribute, it is only used diff --git a/src/stanza_error.rs b/src/stanza_error.rs index edec4d5b..be276614 100644 --- a/src/stanza_error.rs +++ b/src/stanza_error.rs @@ -9,6 +9,7 @@ use std::collections::BTreeMap; use minidom::Element; +use presence::PresencePayload; use error::Error; use jid::Jid; use ns; @@ -212,6 +213,8 @@ pub struct StanzaError { pub other: Option, } +impl PresencePayload for StanzaError {} + impl TryFrom for StanzaError { type Err = Error;