diff --git a/waku/src/events/mod.rs b/waku/src/events/mod.rs index 39ffdd2..535e3e3 100644 --- a/waku/src/events/mod.rs +++ b/waku/src/events/mod.rs @@ -6,7 +6,7 @@ use std::sync::RwLock; use once_cell::sync::Lazy; use serde::{Deserialize, Serialize}; // internal -use crate::general::{PubsubTopic, WakuMessage}; +use crate::general::{WakuMessage, WakuPubSubTopic}; #[derive(Serialize, Deserialize)] pub struct Signal { @@ -26,7 +26,7 @@ pub enum Event { #[serde(rename_all = "camelCase")] pub struct WakuMessageEvent { /// The pubsub topic on which the message was received - pubsub_topic: PubsubTopic, + pubsub_topic: WakuPubSubTopic, /// The message id message_id: String, /// The message in [`WakuMessage`] format @@ -34,7 +34,7 @@ pub struct WakuMessageEvent { } impl WakuMessageEvent { - pub fn pubsub_topic(&self) -> &PubsubTopic { + pub fn pubsub_topic(&self) -> &WakuPubSubTopic { &self.pubsub_topic } diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs index 5a52b18..102b6a7 100644 --- a/waku/src/general/mod.rs +++ b/waku/src/general/mod.rs @@ -2,12 +2,10 @@ use std::fmt::{Display, Formatter}; use std::str::FromStr; // crates -use serde::{Deserialize, Serialize}; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; use sscanf::{scanf, RegexRepresentation}; // internal -pub type PubsubTopic = String; -pub type ContentTopic = String; pub type WakuMessageVersion = usize; /// Base58 encoded peer id pub type PeerId = String; @@ -43,7 +41,7 @@ impl From> for Result { pub struct WakuMessage { payload: Box<[u8]>, /// The content topic to be set on the message - content_topic: ContentTopic, + content_topic: WakuContentTopic, /// The Waku Message version number version: WakuMessageVersion, /// Unix timestamp in nanoseconds @@ -68,7 +66,7 @@ pub struct DecodedPayload { #[serde(rename_all = "camelCase")] pub struct ContentFilter { /// The content topic of a Waku message - content_topic: ContentTopic, + content_topic: WakuContentTopic, } /// The criteria to create subscription to a light node in JSON Format @@ -79,7 +77,7 @@ pub struct FilterSubscription { /// Array of [`ContentFilter`] being subscribed to / unsubscribed from content_filters: Vec, /// Optional pubsub topic - pubsub_topic: Option, + pubsub_topic: Option, } /// Criteria used to retrieve historical messages @@ -87,7 +85,7 @@ pub struct FilterSubscription { #[serde(rename_all = "camelCase")] pub struct StoreQuery { /// The pubsub topic on which messages are published - pubsub_topic: Option, + pubsub_topic: Option, /// Array of [`ContentFilter`] to query for historical messages content_filters: Vec, /// The inclusive lower bound on the timestamp of queried messages. @@ -134,7 +132,7 @@ pub struct MessageIndex { /// UNIX timestamp in nanoseconds at which the message is generated by its sender sender_time: usize, /// The pubsub topic of the message at this [`MessageIndex`] - pubsub_topic: PubsubTopic, + pubsub_topic: WakuPubSubTopic, } pub enum Encoding { @@ -212,6 +210,27 @@ impl Display for WakuContentTopic { } } +impl Serialize for WakuContentTopic { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + self.to_string().serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for WakuContentTopic { + fn deserialize(deserializer: D) -> std::result::Result + where + D: Deserializer<'de>, + { + let as_string: String = String::deserialize(deserializer)?; + as_string + .parse::() + .map_err(D::Error::custom) + } +} + pub struct WakuPubSubTopic { topic_name: String, encoding: Encoding, @@ -242,3 +261,24 @@ impl Display for WakuPubSubTopic { write!(f, "/waku/2/{}/{}", self.topic_name, self.encoding) } } + +impl Serialize for WakuPubSubTopic { + fn serialize(&self, serializer: S) -> std::result::Result + where + S: Serializer, + { + self.to_string().serialize(serializer) + } +} + +impl<'de> Deserialize<'de> for WakuPubSubTopic { + fn deserialize(deserializer: D) -> std::result::Result + where + D: Deserializer<'de>, + { + let as_string: String = String::deserialize(deserializer)?; + as_string + .parse::() + .map_err(D::Error::custom) + } +} diff --git a/waku/src/node/relay.rs b/waku/src/node/relay.rs index e8d7c26..e6229b5 100644 --- a/waku/src/node/relay.rs +++ b/waku/src/node/relay.rs @@ -9,7 +9,7 @@ use crate::general::{ Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic, }; -/// Create a content topic string according to [RFC 23](https://rfc.vac.dev/spec/23/) +/// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_content_topicchar-applicationname-unsigned-int-applicationversion-char-contenttopicname-char-encoding) pub fn waku_create_content_topic( application_name: &str, @@ -39,7 +39,7 @@ pub fn waku_create_content_topic( .expect("Content topic data should be always parseable") } -/// Create a pubsub topic string according to [RFC 23](https://rfc.vac.dev/spec/23/) +/// Create a pubsub topic according to [RFC 23](https://rfc.vac.dev/spec/23/) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_pubsub_topicchar-name-char-encoding) pub fn waku_create_pubsub_topic(topic_name: &str, enconding: Encoding) -> WakuPubSubTopic { unsafe {