diff --git a/src/avatar.rs b/src/avatar.rs index 0f2e096..48893e4 100644 --- a/src/avatar.rs +++ b/src/avatar.rs @@ -5,6 +5,7 @@ // file, You can obtain one at http://mozilla.org/MPL/2.0/. use crate::hashes::Sha1HexAttribute; +use crate::pubsub::PubSubPayload; use crate::util::helpers::WhitespaceAwareBase64; generate_element!( @@ -16,6 +17,8 @@ generate_element!( ] ); +impl PubSubPayload for Metadata {} + generate_element!( /// Communicates avatar metadata. Info, "info", AVATAR_METADATA, @@ -49,6 +52,8 @@ generate_element!( ) ); +impl PubSubPayload for Data {} + #[cfg(test)] mod tests { use super::*; diff --git a/src/pubsub/event.rs b/src/pubsub/event.rs index 4e8b931..5e53acb 100644 --- a/src/pubsub/event.rs +++ b/src/pubsub/event.rs @@ -8,7 +8,7 @@ use crate::data_forms::DataForm; use crate::date::DateTime; use crate::util::error::Error; use crate::ns; -use crate::pubsub::{ItemId, NodeName, Subscription, SubscriptionId}; +use crate::pubsub::{ItemId, NodeName, Subscription, SubscriptionId, PubSubPayload}; use jid::Jid; use minidom::Element; use try_from::TryFrom; @@ -58,6 +58,17 @@ impl From for Element { } } +impl Item { + /// Create a new item event, accepting only payloads implementing `PubSubPayload`. + pub fn new(id: Option, publisher: Option, payload: Option

) -> Item { + Item { + id, + publisher, + payload: payload.map(|payload| payload.into()), + } + } +} + /// Represents an event happening to a PubSub node. #[derive(Debug, Clone)] pub enum PubSubEvent { diff --git a/src/pubsub/mod.rs b/src/pubsub/mod.rs index a8af5c5..dd39ea1 100644 --- a/src/pubsub/mod.rs +++ b/src/pubsub/mod.rs @@ -45,3 +45,6 @@ generate_attribute!( Unconfigured => "unconfigured", }, Default = None ); + +/// This trait should be implemented on any element which can be included as a PubSub payload. +pub trait PubSubPayload: crate::TryFrom + Into {}