From d60feffc22de32d0828e1a5b85e95d4873f94e19 Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 27 Jan 2019 17:40:46 +0100 Subject: [PATCH] pubsub: Add a PubSubPayload trait. --- src/avatar.rs | 5 +++++ src/pubsub/event.rs | 13 ++++++++++++- src/pubsub/mod.rs | 3 +++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/avatar.rs b/src/avatar.rs index 0f2e0962..48893e4c 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 4e8b9311..5e53acb8 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 a8af5c5a..dd39ea1d 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 {}