diff --git a/waku-bindings/src/node/management.rs b/waku-bindings/src/node/management.rs index 7459d5a..72f4242 100644 --- a/waku-bindings/src/node/management.rs +++ b/waku-bindings/src/node/management.rs @@ -2,11 +2,11 @@ // std use multiaddr::Multiaddr; -use std::ffi::{CStr, CString}; +use std::ffi::CString; // crates // internal use super::config::WakuNodeConfig; -use crate::general::{JsonResponse, PeerId, Result}; +use crate::general::{PeerId, Result}; use crate::utils::decode_and_free_response; /// Instantiates a Waku node @@ -27,72 +27,28 @@ pub fn waku_new(config: Option) -> Result { res }; - let result = unsafe { CStr::from_ptr(result_ptr) } - .to_str() - .expect("Response should always succeed to load to a &str"); - - let json_response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(result_ptr); - } - - json_response.into() + decode_and_free_response(result_ptr) } /// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation. /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start) pub fn waku_start() -> Result { let response_ptr = unsafe { waku_sys::waku_start() }; - 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_and_free_response(response_ptr) } /// Stops a Waku node /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop) pub fn waku_stop() -> Result { let response_ptr = unsafe { waku_sys::waku_stop() }; - 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_and_free_response(response_ptr) } /// If the execution is successful, the result is the peer ID as a string (base58 encoded) /// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop) pub fn waku_peer_id() -> Result { let response_ptr = unsafe { waku_sys::waku_peerid() }; - 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_and_free_response(response_ptr) } /// Get the multiaddresses the Waku node is listening to diff --git a/waku-bindings/src/node/peers.rs b/waku-bindings/src/node/peers.rs index 29c3823..ca463b7 100644 --- a/waku-bindings/src/node/peers.rs +++ b/waku-bindings/src/node/peers.rs @@ -1,13 +1,13 @@ //! Waku [peer handling and connection](https://rfc.vac.dev/spec/36/#connecting-to-peers) methods // std -use std::ffi::{CStr, CString}; +use std::ffi::CString; use std::time::Duration; // crates use multiaddr::Multiaddr; use serde::Deserialize; // internal -use crate::general::{JsonResponse, PeerId, ProtocolId, Result}; +use crate::general::{PeerId, ProtocolId, Result}; use crate::utils::decode_and_free_response; /// Add a node multiaddress and protocol to the waku node’s peerstore. @@ -27,18 +27,7 @@ pub fn waku_add_peers(address: &Multiaddr, protocol_id: ProtocolId) -> Result = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); - - unsafe { - waku_sys::waku_utils_free(response_ptr); - } - - result.into() + decode_and_free_response(response_ptr) } /// Dial peer using a multiaddress @@ -64,18 +53,7 @@ pub fn waku_connect_peer_with_address( res }; - 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::from(result).map(|_| ()) + decode_and_free_response::(response_ptr).map(|_| ()) } /// Dial peer using a peer id @@ -87,7 +65,7 @@ pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option) -> let peer_id_ptr = CString::new(peer_id.as_bytes()) .expect("CString should build properly from peer id") .into_raw(); - let response_ptr = unsafe { + let result_ptr = unsafe { let res = waku_sys::waku_connect_peerid( peer_id_ptr, timeout @@ -98,18 +76,7 @@ pub fn waku_connect_peer_with_id(peer_id: &PeerId, timeout: Option) -> res }; - 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::from(result).map(|_| ()) + decode_and_free_response::(result_ptr).map(|_| ()) } /// Disconnect a peer using its peer id @@ -124,37 +91,14 @@ pub fn waku_disconnect_peer_with_id(peer_id: &PeerId) -> Result<()> { drop(CString::from_raw(peer_id_ptr)); res }; - 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::from(result).map(|_| ()) + decode_and_free_response::(response_ptr).map(|_| ()) } /// Get number of connected peers /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_peer_count) pub fn waku_peer_count() -> Result { let response_ptr = unsafe { waku_sys::waku_peer_cnt() }; - - 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_and_free_response(response_ptr) } /// Waku peer supported protocol diff --git a/waku-bindings/src/node/relay.rs b/waku-bindings/src/node/relay.rs index f025850..8f5ef37 100644 --- a/waku-bindings/src/node/relay.rs +++ b/waku-bindings/src/node/relay.rs @@ -7,9 +7,7 @@ use std::time::Duration; use aes_gcm::{Aes256Gcm, Key}; use secp256k1::{PublicKey, SecretKey}; // internal -use crate::general::{ - Encoding, JsonResponse, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic, -}; +use crate::general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic}; use crate::utils::decode_and_free_response; /// Create a content topic according to [RFC 23](https://rfc.vac.dev/spec/23/) @@ -272,16 +270,7 @@ pub fn waku_enough_peers(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) }; - - enough_peers.into() + decode_and_free_response(result_ptr) } pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> {