presence: Make PresencePayload a trait, and implement it on every payload.
This commit is contained in:
parent
709666bb91
commit
e41de29d9d
7 changed files with 20 additions and 61 deletions
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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::*;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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::*;
|
||||
|
|
|
|||
|
|
@ -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::*;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue