presence: Make PresencePayload a trait, and implement it on every payload.

Emmanuel Gil Peyrot created

Change summary

src/caps.rs         |  3 ++
src/delay.rs        |  3 ++
src/ecaps2.rs       |  3 ++
src/idle.rs         |  3 ++
src/muc/muc.rs      |  3 ++
src/presence.rs     | 63 +---------------------------------------------
src/stanza_error.rs |  3 ++
7 files changed, 20 insertions(+), 61 deletions(-)

Detailed changes

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<Element> for Caps {
     type Err = Error;
 

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::*;

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<u8> {
     let mut bytes = field.as_bytes().to_vec();
     bytes.push(0x1f);

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::*;

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::*;

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 `<presence/>`.
+pub trait PresencePayload: TryFrom<Element> + Into<Element> {}
 
 #[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 `<presence/>`.
-#[derive(Debug, Clone)]
-pub enum PresencePayload {
-    StanzaError(StanzaError),
-    Muc(Muc),
-    Caps(Caps),
-    Delay(Delay),
-    Idle(Idle),
-    ECaps2(ECaps2),
-
-    Unknown(Element),
-}
-
-impl TryFrom<Element> for PresencePayload {
-    type Err = Error;
-
-    fn try_from(elem: Element) -> Result<PresencePayload, Error> {
-        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<PresencePayload> 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

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<Element>,
 }
 
+impl PresencePayload for StanzaError {}
+
 impl TryFrom<Element> for StanzaError {
     type Err = Error;