diff --git a/waku-bindings/src/lib.rs b/waku-bindings/src/lib.rs index a43d3ab..1c525e0 100644 --- a/waku-bindings/src/lib.rs +++ b/waku-bindings/src/lib.rs @@ -5,6 +5,7 @@ mod decrypt; mod events; mod general; mod node; +mod utils; pub use node::{ waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic, waku_new, diff --git a/waku-bindings/src/utils.rs b/waku-bindings/src/utils.rs new file mode 100644 index 0000000..663e6bb --- /dev/null +++ b/waku-bindings/src/utils.rs @@ -0,0 +1,20 @@ +use crate::general::{JsonResponse, Result}; +use serde::de::DeserializeOwned; +use std::ffi::{c_char, CStr}; + +/// Safety: The caller is responsible for ensuring that the pointer is valid for the duration of the call. +/// This takes a pointer to a C string coming from the waku lib, that data is consumed and then freed using [`waku_sys::waku_utils_free`]. +pub fn decode_response(response_ptr: *mut c_char) -> Result { + let response = unsafe { CStr::from_ptr(response_ptr) } + .to_str() + .expect("Response should always succeed to load to a &str"); + + let response: JsonResponse = + serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); + + unsafe { + waku_sys::waku_utils_free(response_ptr); + } + + response.into() +}