Impl Serialize/Deserialize for Content/Pubsub topic

This commit is contained in:
Daniel Sanchez Quiros 2022-10-05 10:24:02 +02:00
parent 85f6c1c987
commit ba777424f3
3 changed files with 53 additions and 13 deletions

View File

@ -6,7 +6,7 @@ use std::sync::RwLock;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
// internal // internal
use crate::general::{PubsubTopic, WakuMessage}; use crate::general::{WakuMessage, WakuPubSubTopic};
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct Signal { pub struct Signal {
@ -26,7 +26,7 @@ pub enum Event {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct WakuMessageEvent { pub struct WakuMessageEvent {
/// The pubsub topic on which the message was received /// The pubsub topic on which the message was received
pubsub_topic: PubsubTopic, pubsub_topic: WakuPubSubTopic,
/// The message id /// The message id
message_id: String, message_id: String,
/// The message in [`WakuMessage`] format /// The message in [`WakuMessage`] format
@ -34,7 +34,7 @@ pub struct WakuMessageEvent {
} }
impl WakuMessageEvent { impl WakuMessageEvent {
pub fn pubsub_topic(&self) -> &PubsubTopic { pub fn pubsub_topic(&self) -> &WakuPubSubTopic {
&self.pubsub_topic &self.pubsub_topic
} }

View File

@ -2,12 +2,10 @@
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
// crates // crates
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sscanf::{scanf, RegexRepresentation}; use sscanf::{scanf, RegexRepresentation};
// internal // internal
pub type PubsubTopic = String;
pub type ContentTopic = String;
pub type WakuMessageVersion = usize; pub type WakuMessageVersion = usize;
/// Base58 encoded peer id /// Base58 encoded peer id
pub type PeerId = String; pub type PeerId = String;
@ -43,7 +41,7 @@ impl<T> From<JsonResponse<T>> for Result<T> {
pub struct WakuMessage { pub struct WakuMessage {
payload: Box<[u8]>, payload: Box<[u8]>,
/// The content topic to be set on the message /// The content topic to be set on the message
content_topic: ContentTopic, content_topic: WakuContentTopic,
/// The Waku Message version number /// The Waku Message version number
version: WakuMessageVersion, version: WakuMessageVersion,
/// Unix timestamp in nanoseconds /// Unix timestamp in nanoseconds
@ -68,7 +66,7 @@ pub struct DecodedPayload {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ContentFilter { pub struct ContentFilter {
/// The content topic of a Waku message /// 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 /// 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 /// Array of [`ContentFilter`] being subscribed to / unsubscribed from
content_filters: Vec<ContentFilter>, content_filters: Vec<ContentFilter>,
/// Optional pubsub topic /// Optional pubsub topic
pubsub_topic: Option<PubsubTopic>, pubsub_topic: Option<WakuPubSubTopic>,
} }
/// Criteria used to retrieve historical messages /// Criteria used to retrieve historical messages
@ -87,7 +85,7 @@ pub struct FilterSubscription {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct StoreQuery { pub struct StoreQuery {
/// The pubsub topic on which messages are published /// The pubsub topic on which messages are published
pubsub_topic: Option<PubsubTopic>, pubsub_topic: Option<WakuPubSubTopic>,
/// Array of [`ContentFilter`] to query for historical messages /// Array of [`ContentFilter`] to query for historical messages
content_filters: Vec<ContentFilter>, content_filters: Vec<ContentFilter>,
/// The inclusive lower bound on the timestamp of queried messages. /// 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 /// UNIX timestamp in nanoseconds at which the message is generated by its sender
sender_time: usize, sender_time: usize,
/// The pubsub topic of the message at this [`MessageIndex`] /// The pubsub topic of the message at this [`MessageIndex`]
pubsub_topic: PubsubTopic, pubsub_topic: WakuPubSubTopic,
} }
pub enum Encoding { pub enum Encoding {
@ -212,6 +210,27 @@ impl Display for WakuContentTopic {
} }
} }
impl Serialize for WakuContentTopic {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
self.to_string().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for WakuContentTopic {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let as_string: String = String::deserialize(deserializer)?;
as_string
.parse::<WakuContentTopic>()
.map_err(D::Error::custom)
}
}
pub struct WakuPubSubTopic { pub struct WakuPubSubTopic {
topic_name: String, topic_name: String,
encoding: Encoding, encoding: Encoding,
@ -242,3 +261,24 @@ impl Display for WakuPubSubTopic {
write!(f, "/waku/2/{}/{}", self.topic_name, self.encoding) write!(f, "/waku/2/{}/{}", self.topic_name, self.encoding)
} }
} }
impl Serialize for WakuPubSubTopic {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: Serializer,
{
self.to_string().serialize(serializer)
}
}
impl<'de> Deserialize<'de> for WakuPubSubTopic {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let as_string: String = String::deserialize(deserializer)?;
as_string
.parse::<WakuPubSubTopic>()
.map_err(D::Error::custom)
}
}

View File

@ -9,7 +9,7 @@ use crate::general::{
Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic, 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) /// 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( pub fn waku_create_content_topic(
application_name: &str, application_name: &str,
@ -39,7 +39,7 @@ pub fn waku_create_content_topic(
.expect("Content topic data should be always parseable") .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) /// 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 { pub fn waku_create_pubsub_topic(topic_name: &str, enconding: Encoding) -> WakuPubSubTopic {
unsafe { unsafe {