pubsub: Add a PubSubPayload trait.

This commit is contained in:
Emmanuel Gil Peyrot 2019-01-27 17:40:46 +01:00
parent b936ce595f
commit d60feffc22
3 changed files with 20 additions and 1 deletions

View file

@ -5,6 +5,7 @@
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
use crate::hashes::Sha1HexAttribute; use crate::hashes::Sha1HexAttribute;
use crate::pubsub::PubSubPayload;
use crate::util::helpers::WhitespaceAwareBase64; use crate::util::helpers::WhitespaceAwareBase64;
generate_element!( generate_element!(
@ -16,6 +17,8 @@ generate_element!(
] ]
); );
impl PubSubPayload for Metadata {}
generate_element!( generate_element!(
/// Communicates avatar metadata. /// Communicates avatar metadata.
Info, "info", AVATAR_METADATA, Info, "info", AVATAR_METADATA,
@ -49,6 +52,8 @@ generate_element!(
) )
); );
impl PubSubPayload for Data {}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;

View file

@ -8,7 +8,7 @@ use crate::data_forms::DataForm;
use crate::date::DateTime; use crate::date::DateTime;
use crate::util::error::Error; use crate::util::error::Error;
use crate::ns; use crate::ns;
use crate::pubsub::{ItemId, NodeName, Subscription, SubscriptionId}; use crate::pubsub::{ItemId, NodeName, Subscription, SubscriptionId, PubSubPayload};
use jid::Jid; use jid::Jid;
use minidom::Element; use minidom::Element;
use try_from::TryFrom; use try_from::TryFrom;
@ -58,6 +58,17 @@ impl From<Item> for Element {
} }
} }
impl Item {
/// Create a new item event, accepting only payloads implementing `PubSubPayload`.
pub fn new<P: PubSubPayload>(id: Option<ItemId>, publisher: Option<Jid>, payload: Option<P>) -> Item {
Item {
id,
publisher,
payload: payload.map(|payload| payload.into()),
}
}
}
/// Represents an event happening to a PubSub node. /// Represents an event happening to a PubSub node.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum PubSubEvent { pub enum PubSubEvent {

View file

@ -45,3 +45,6 @@ generate_attribute!(
Unconfigured => "unconfigured", Unconfigured => "unconfigured",
}, Default = None }, Default = None
); );
/// This trait should be implemented on any element which can be included as a PubSub payload.
pub trait PubSubPayload: crate::TryFrom<crate::Element> + Into<crate::Element> {}