Skip to content

Commit

Permalink
fix: pubsub topic is a string
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Nov 2, 2023
1 parent c04eb6b commit ba70616
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
run: git submodule update --init --recursive
- uses: actions/setup-go@v3 # we need go to build go-waku
with:
go-version: '1.19'
go-version: '1.20'
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
run: git submodule update --init --recursive
- uses: actions/setup-go@v3 # we need go to build go-waku
with:
go-version: '1.19'
go-version: '1.20'
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down Expand Up @@ -61,7 +61,7 @@ jobs:
run: git submodule update --init --recursive
- uses: actions/setup-go@v3 # we need go to build go-waku
with:
go-version: '1.19'
go-version: '1.20'
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand All @@ -88,7 +88,7 @@ jobs:
run: git submodule update --init --recursive
- uses: actions/setup-go@v3 # we need go to build go-waku
with:
go-version: '1.19'
go-version: '1.20'
- uses: actions-rs/toolchain@v1
with:
profile: minimal
Expand Down
4 changes: 2 additions & 2 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 2 additions & 69 deletions waku-bindings/src/general/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub type WakuMessageVersion = usize;
pub type PeerId = String;
/// Waku message id, hex encoded sha256 digest of the message
pub type MessageId = String;
/// Waku pubsub topic
pub type WakuPubSubTopic = String;

/// Protocol identifiers
#[non_exhaustive]
Expand Down Expand Up @@ -515,75 +517,6 @@ impl<'de> Deserialize<'de> for WakuContentTopic {
}
}

/// A waku pubsub topic in the form of `/waku/v2/{topic_name}/{encoding}`
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WakuPubSubTopic {
pub topic_name: Cow<'static, str>,
pub encoding: Encoding,
}

impl WakuPubSubTopic {
pub const fn new(topic_name: &'static str, encoding: Encoding) -> Self {
Self {
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,
}
}
}

impl FromStr for WakuPubSubTopic {
type Err = String;

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: Cow::Owned(topic_name),
encoding,
})
} else {
Err(
format!(
"Wrong pub-sub topic format. Should be `/waku/2/{{topic-name}}/{{encoding}}`. Got: {s}"
)
)
}
}
}

impl Display for WakuPubSubTopic {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
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)
}
}

mod base64_serde {
use base64::Engine;
use serde::de::Error;
Expand Down

0 comments on commit ba70616

Please sign in to comment.