diff --git a/Cargo.lock b/Cargo.lock index cf0a16b..cadcc92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -140,6 +140,26 @@ dependencies = [ "os_str_bytes", ] +[[package]] +name = "const_format" +version = "0.2.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "939dc9e2eb9077e0679d2ce32de1ded8531779360b003b4a972a7a39ec263495" +dependencies = [ + "const_format_proc_macros", +] + +[[package]] +name = "const_format_proc_macros" +version = "0.2.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef196d5d972878a48da7decb7686eded338b4858fbabeed513d63a7c98b2b82d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "core2" version = "0.4.0" @@ -666,6 +686,30 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43b2853a4d09f215c24cc5489c992ce46052d359b5109343cbafbf26bc62f8a3" +[[package]] +name = "sscanf" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ecdd7ea17bcadebf81d656db919f58f96c1d194d748cf0839a44a220123eedd" +dependencies = [ + "const_format", + "lazy_static", + "regex", + "sscanf_macro", +] + +[[package]] +name = "sscanf_macro" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe2309d255caf220c1ff9f380d89420a1377de1cabc1d57e0b308e53b0406bed" +dependencies = [ + "proc-macro2", + "quote", + "regex-syntax", + "syn", +] + [[package]] name = "static_assertions" version = "1.1.0" @@ -832,6 +876,7 @@ dependencies = [ "once_cell", "serde", "serde_json", + "sscanf", "waku-sys", ] diff --git a/waku/Cargo.toml b/waku/Cargo.toml index 0c23ead..07d8568 100644 --- a/waku/Cargo.toml +++ b/waku/Cargo.toml @@ -12,4 +12,5 @@ multiaddr = "0.14" once_cell = "1.15" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +sscanf = "0.3" waku-sys = { path = "../waku-sys" } diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs index d6de08b..3a90695 100644 --- a/waku/src/general/mod.rs +++ b/waku/src/general/mod.rs @@ -1,6 +1,9 @@ // std +use std::fmt::{Display, Formatter}; +use std::str::FromStr; // crates use serde::{Deserialize, Serialize}; +use sscanf::{scanf, RegexRepresentation}; // internal pub type PubsubTopic = String; @@ -132,3 +135,68 @@ pub struct MessageIndex { /// The pubsub topic of the message at this [`MessageIndex`] pubsub_topic: PubsubTopic, } + +pub enum Encoding { + Proto, + Rlp, + Rfc26, +} + +impl Display for Encoding { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + let s = match self { + Encoding::Proto => "proto", + Encoding::Rlp => "rlp", + Encoding::Rfc26 => "rfc26", + }; + f.write_str(s) + } +} + +impl FromStr for Encoding { + type Err = String; + + fn from_str(s: &str) -> std::result::Result { + match s.to_lowercase().as_str() { + "proto" => Ok(Self::Proto), + "rlp" => Ok(Self::Rlp), + "rfc26" => Ok(Self::Rfc26), + encoding => Err(format!("Unrecognized encoding: {}", encoding)), + } + } +} + +impl RegexRepresentation for Encoding { + const REGEX: &'static str = r"\w"; +} + +pub struct WakuPubsubTopic { + application_name: String, + version: usize, + content_topic_name: String, + encoding: Encoding, +} + +impl FromStr for WakuPubsubTopic { + type Err = String; + + fn from_str(s: &str) -> std::result::Result { + if let Ok((application_name, version, content_topic_name, encoding)) = + scanf!(s, "{}/{}/{}/{}", String, usize, String, Encoding) + { + Ok(WakuPubsubTopic { + application_name, + version, + content_topic_name, + encoding, + }) + } else { + Err( + format!( + "Wrong pub-sub topic format. Should be `/{{application-name}}/{{version-of-the-application}}/{{content-topic-name}}/{{encoding}}`. Got: {}", + s + ) + ) + } + } +} diff --git a/waku/src/node/peers.rs b/waku/src/node/peers.rs index 59188b4..9594dac 100644 --- a/waku/src/node/peers.rs +++ b/waku/src/node/peers.rs @@ -1,5 +1,5 @@ // std -use std::ffi::{c_char, CStr, CString}; +use std::ffi::{CStr, CString}; use std::time::Duration; // crates use multiaddr::Multiaddr;