Make topics const initializable (#28)

* Make topics const initializable

* Fix test

* Derive Eq, PartialEq for topics
This commit is contained in:
Daniel Sanchez 2022-12-05 18:42:55 +01:00 committed by GitHub
parent 3f85a09b88
commit aa06fcc969
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 38 additions and 26 deletions

1
Cargo.lock generated
View File

@ -1094,7 +1094,6 @@ version = "0.1.0"
dependencies = [
"chrono",
"crossterm",
"once_cell",
"prost",
"tui",
"unicode-width",

View File

@ -13,5 +13,4 @@ tui = "0.19"
crossterm = "0.25"
unicode-width = "0.1"
prost = "0.11"
once_cell = "1.15"
chrono = "0.4"

View File

@ -1,14 +1,9 @@
use chrono::{DateTime, LocalResult, TimeZone, Utc};
use once_cell::sync::Lazy;
use prost::Message;
use waku_bindings::{Encoding, WakuContentTopic};
pub static TOY_CHAT_CONTENT_TOPIC: Lazy<WakuContentTopic> = Lazy::new(|| WakuContentTopic {
application_name: "toy-chat".into(),
version: 2,
content_topic_name: "huilong".into(),
encoding: Encoding::Proto,
});
pub static TOY_CHAT_CONTENT_TOPIC: WakuContentTopic =
WakuContentTopic::new("toy-chat", 2, "huilong", Encoding::Proto);
#[derive(Clone, Message)]
pub struct Chat2Message {

View File

@ -1,5 +1,6 @@
//! Waku [general](https://rfc.vac.dev/spec/36/#general) types
use std::borrow::Cow;
// std
use std::fmt::{Display, Formatter};
use std::str::FromStr;
@ -285,7 +286,7 @@ pub struct MessageIndex {
}
/// WakuMessage encoding scheme
#[derive(Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Encoding {
Proto,
Rlp,
@ -321,14 +322,30 @@ impl RegexRepresentation for Encoding {
}
/// A waku content topic `/{application_name}/{version}/{content_topic_name}/{encdoing}`
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WakuContentTopic {
pub application_name: String,
pub application_name: Cow<'static, str>,
pub version: usize,
pub content_topic_name: String,
pub content_topic_name: Cow<'static, str>,
pub encoding: Encoding,
}
impl WakuContentTopic {
pub const fn new(
application_name: &'static str,
version: usize,
content_topic_name: &'static str,
encoding: Encoding,
) -> Self {
Self {
application_name: Cow::Borrowed(application_name),
version,
content_topic_name: Cow::Borrowed(content_topic_name),
encoding,
}
}
}
impl FromStr for WakuContentTopic {
type Err = String;
@ -337,9 +354,9 @@ impl FromStr for WakuContentTopic {
scanf!(s, "/{}/{}/{}/{:/.+?/}", String, usize, String, Encoding)
{
Ok(WakuContentTopic {
application_name,
application_name: Cow::Owned(application_name),
version,
content_topic_name,
content_topic_name: Cow::Owned(content_topic_name),
encoding,
})
} else {
@ -385,16 +402,23 @@ impl<'de> Deserialize<'de> for WakuContentTopic {
}
/// A waku pubsub topic in the form of `/waku/v2/{topic_name}/{encoding}`
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WakuPubSubTopic {
pub topic_name: String,
pub topic_name: Cow<'static, str>,
pub encoding: Encoding,
}
impl WakuPubSubTopic {
pub fn new(topic_name: String, encoding: Encoding) -> Self {
pub const fn new(topic_name: &'static str, encoding: Encoding) -> Self {
Self {
topic_name,
topic_name: Cow::Borrowed(topic_name),
encoding,
}
}
pub fn with_topic_name(topic_name: String, encoding: Encoding) -> Self {
Self {
topic_name: Cow::Owned(topic_name),
encoding,
}
}
@ -406,7 +430,7 @@ impl FromStr for WakuPubSubTopic {
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if let Ok((topic_name, encoding)) = scanf!(s, "/waku/2/{}/{:/.+?/}", String, Encoding) {
Ok(WakuPubSubTopic {
topic_name,
topic_name: Cow::Owned(topic_name),
encoding,
})
} else {

View File

@ -83,12 +83,7 @@ pub fn main() -> Result<(), String> {
// subscribe to default channel
node.relay_subscribe(None)?;
let content_topic = WakuContentTopic {
application_name: "toychat".to_string(),
version: 2,
content_topic_name: "huilong".to_string(),
encoding: Encoding::Proto,
};
let content_topic = WakuContentTopic::new("toychat", 2, "huilong", Encoding::Proto);
let message = WakuMessage::new(
content,