From b0ea59a29dce6e890505f7a45bd0fae350b9981b Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Tue, 14 Feb 2023 16:19:14 +0100 Subject: [PATCH] Use decode response --- waku-bindings/src/node/discovery.rs | 14 ++----- waku-bindings/src/node/filter.rs | 24 +++--------- waku-bindings/src/node/lightpush.rs | 42 +++------------------ waku-bindings/src/node/management.rs | 14 +------ waku-bindings/src/node/peers.rs | 14 +------ waku-bindings/src/node/relay.rs | 56 +++------------------------- waku-bindings/src/node/store.rs | 16 ++------ 7 files changed, 28 insertions(+), 152 deletions(-) diff --git a/waku-bindings/src/node/discovery.rs b/waku-bindings/src/node/discovery.rs index 937097c..19acacd 100644 --- a/waku-bindings/src/node/discovery.rs +++ b/waku-bindings/src/node/discovery.rs @@ -1,11 +1,11 @@ // std -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::time::Duration; // crates use multiaddr::Multiaddr; use url::{Host, Url}; // internal -use crate::general::JsonResponse; +use crate::utils::decode_response; use crate::Result; /// RetrieveNodes returns a list of multiaddress given a url to a DNS discoverable ENR tree. @@ -43,14 +43,6 @@ pub fn waku_dns_discovery( drop(CString::from_raw(server)); res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - let response: JsonResponse> = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - response.into() + decode_response(result_ptr) } diff --git a/waku-bindings/src/node/filter.rs b/waku-bindings/src/node/filter.rs index 839a64f..5708bcb 100644 --- a/waku-bindings/src/node/filter.rs +++ b/waku-bindings/src/node/filter.rs @@ -1,13 +1,14 @@ //! Waku [filter](https://rfc.vac.dev/spec/36/#waku-filter) protocol related methods // std -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::time::Duration; // crates // internal use crate::general::Result; -use crate::general::{FilterSubscription, JsonResponse, PeerId}; +use crate::general::{FilterSubscription, PeerId}; +use crate::utils::decode_response; /// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_subscribechar-filterjson-char-peerid-int-timeoutms) @@ -39,16 +40,7 @@ pub fn waku_filter_subscribe( drop(CString::from_raw(peer_id_ptr)); result_ptr }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - Result::from(response).map(|_| ()) + decode_response::(result_ptr).map(|_| ()) } /// Removes subscriptions in a light node matching a content filter and, optionally, a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`) @@ -74,12 +66,6 @@ pub fn waku_filter_unsubscribe( drop(CString::from_raw(filter_subscription_ptr)); res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - let response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - unsafe { waku_sys::waku_utils_free(result_ptr) }; - Result::from(response).map(|_| ()) + decode_response::(result_ptr).map(|_| ()) } diff --git a/waku-bindings/src/node/lightpush.rs b/waku-bindings/src/node/lightpush.rs index f6ddf8e..8e15659 100644 --- a/waku-bindings/src/node/lightpush.rs +++ b/waku-bindings/src/node/lightpush.rs @@ -1,14 +1,15 @@ //! Waku [lightpush](https://rfc.vac.dev/spec/36/#waku-lightpush) protocol related methods // std -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::time::Duration; // crates use aes_gcm::{Aes256Gcm, Key}; use secp256k1::{PublicKey, SecretKey}; // internal -use crate::general::{JsonResponse, MessageId, PeerId, Result, WakuMessage, WakuPubSubTopic}; +use crate::general::{MessageId, PeerId, Result, WakuMessage, WakuPubSubTopic}; use crate::node::waku_dafault_pubsub_topic; +use crate::utils::decode_response; /// Publish a message using Waku Lightpush /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publishchar-messagejson-char-topic-char-peerid-int-timeoutms) @@ -53,18 +54,7 @@ pub fn waku_lightpush_publish( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - - response.into() + decode_response(result_ptr) } /// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Lightpush @@ -127,16 +117,7 @@ pub fn waku_lightpush_publish_encrypt_asymmetric( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let message_id: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - message_id.into() + decode_response(result_ptr) } /// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Lightpush @@ -198,16 +179,5 @@ pub fn waku_lightpush_publish_encrypt_symmetric( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let message_id: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - - message_id.into() + decode_response(result_ptr) } diff --git a/waku-bindings/src/node/management.rs b/waku-bindings/src/node/management.rs index 1feb1a5..063ac40 100644 --- a/waku-bindings/src/node/management.rs +++ b/waku-bindings/src/node/management.rs @@ -7,6 +7,7 @@ use std::ffi::{CStr, CString}; // internal use super::config::WakuNodeConfig; use crate::general::{JsonResponse, PeerId, Result}; +use crate::utils::decode_response; /// Instantiates a Waku node /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig) @@ -98,18 +99,7 @@ pub fn waku_peer_id() -> Result { /// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses) pub fn waku_listen_addresses() -> Result> { let response_ptr = unsafe { waku_sys::waku_listen_addresses() }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let json_response: JsonResponse> = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - json_response.into() + decode_response(response_ptr) } #[cfg(test)] diff --git a/waku-bindings/src/node/peers.rs b/waku-bindings/src/node/peers.rs index 34aff30..75cd9d3 100644 --- a/waku-bindings/src/node/peers.rs +++ b/waku-bindings/src/node/peers.rs @@ -8,6 +8,7 @@ use multiaddr::Multiaddr; use serde::Deserialize; // internal use crate::general::{JsonResponse, PeerId, ProtocolId, Result}; +use crate::utils::decode_response; /// Add a node multiaddress and protocol to the waku node’s peerstore. /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_add_peerchar-address-char-protocolid) @@ -205,18 +206,7 @@ pub type WakuPeers = Vec; /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peers) pub fn waku_peers() -> Result { let response_ptr = unsafe { waku_sys::waku_peers() }; - let response = unsafe { CStr::from_ptr(response_ptr) } - .to_str() - .expect("&str should build properly from the returning response"); - - let result: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - result.into() + decode_response(response_ptr) } #[cfg(test)] diff --git a/waku-bindings/src/node/relay.rs b/waku-bindings/src/node/relay.rs index 52c5d0d..974c0e6 100644 --- a/waku-bindings/src/node/relay.rs +++ b/waku-bindings/src/node/relay.rs @@ -10,6 +10,7 @@ use secp256k1::{PublicKey, SecretKey}; use crate::general::{ Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic, }; +use crate::utils::decode_response; /// 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) @@ -137,16 +138,7 @@ pub fn waku_relay_publish_message( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("&str from result should always be extracted"); - - let message_id: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - message_id.into() + decode_response(result_ptr) } /// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay @@ -204,16 +196,7 @@ pub fn waku_relay_publish_encrypt_asymmetric( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("&str from result should always be extracted"); - - let message_id: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - message_id.into() + decode_response(result_ptr) } /// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Relay @@ -271,16 +254,7 @@ pub fn waku_relay_publish_encrypt_symmetric( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("&str from result should always be extracted"); - - let message_id: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - message_id.into() + decode_response(result_ptr) } pub fn waku_enough_peers(pubsub_topic: Option) -> Result { @@ -325,16 +299,7 @@ pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("&str from result should always be extracted"); - - let enough_peers: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - Result::from(enough_peers).map(|_| ()) + decode_response::(result_ptr).map(|_| ()) } pub fn waku_relay_unsubscribe(pubsub_topic: Option) -> Result<()> { @@ -352,14 +317,5 @@ pub fn waku_relay_unsubscribe(pubsub_topic: Option) -> Result<( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("&str from result should always be extracted"); - - let enough_peers: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - Result::from(enough_peers).map(|_| ()) + decode_response::(result_ptr).map(|_| ()) } diff --git a/waku-bindings/src/node/store.rs b/waku-bindings/src/node/store.rs index 76202c9..e5fcf04 100644 --- a/waku-bindings/src/node/store.rs +++ b/waku-bindings/src/node/store.rs @@ -1,11 +1,12 @@ //! Waku [store](https://rfc.vac.dev/spec/36/#waku-store) handling methods // std -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::time::Duration; // crates // internal -use crate::general::{JsonResponse, PeerId, Result, StoreQuery, StoreResponse}; +use crate::general::{PeerId, Result, StoreQuery, StoreResponse}; +use crate::utils::decode_response; /// Retrieves historical messages on specific content topics. This method may be called with [`PagingOptions`](`crate::general::PagingOptions`), /// to retrieve historical messages on a per-page basis. If the request included [`PagingOptions`](`crate::general::PagingOptions`), @@ -43,14 +44,5 @@ pub fn waku_store_query( res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { waku_sys::waku_utils_free(result_ptr) }; - - response.into() + decode_response(result_ptr) }