Implemented filter methods wrappers

This commit is contained in:
Daniel Sanchez Quiros 2022-10-07 11:26:55 +02:00
parent a404342acd
commit 15dbe6284e
2 changed files with 71 additions and 0 deletions

70
waku/src/node/filter.rs Normal file
View File

@ -0,0 +1,70 @@
// std
use std::ffi::{CStr, CString};
use std::time::Duration;
// crates
// internal
use crate::general::Result;
use crate::general::{FilterSubscription, JsonResponse, MessageId, PeerId};
/// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`PubSubTopic`]
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_subscribechar-filterjson-char-peerid-int-timeoutms)
pub fn waku_filter_subscribe(
filter_subscription: &FilterSubscription,
peer_id: PeerId,
timeout: Duration,
) -> Result<()> {
let result = unsafe {
CStr::from_ptr(waku_sys::waku_filter_subscribe(
CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always be able to be serialized"),
)
.expect("CString should build properly from the serialized filter subscription")
.into_raw(),
CString::new(peer_id)
.expect("CString should build properly from peer id")
.into_raw(),
timeout
.as_millis()
.try_into()
.expect("Duration as milliseconds should fit in a i32"),
))
}
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
Result::from(response).map(|_| ())
}
/// Removes subscriptions in a light node matching a content filter and, optionally, a [`PubSubTopic`]
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_unsubscribechar-filterjson-int-timeoutms)
pub fn waku_filter_unsubscribe(
filter_subscription: &FilterSubscription,
timeout: Duration,
) -> Result<()> {
let result = unsafe {
CStr::from_ptr(waku_sys::waku_filter_unsubscribe(
CString::new(
serde_json::to_string(filter_subscription)
.expect("FilterSubscription should always be able to be serialized"),
)
.expect("CString should build properly from the serialized filter subscription")
.into_raw(),
timeout
.as_millis()
.try_into()
.expect("Duration as milliseconds should fit in a i32"),
))
}
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<bool> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
Result::from(response).map(|_| ())
}

View File

@ -1,4 +1,5 @@
mod config;
mod filter;
mod lightpush;
mod management;
mod peers;