Added decode_response method

This commit is contained in:
Daniel Sanchez Quiros 2023-02-14 15:44:46 +01:00
parent 412e742eaf
commit 6e972a76c1
2 changed files with 21 additions and 0 deletions

View File

@ -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,

View File

@ -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<T: DeserializeOwned>(response_ptr: *mut c_char) -> Result<T> {
let response = unsafe { CStr::from_ptr(response_ptr) }
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<T> =
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
unsafe {
waku_sys::waku_utils_free(response_ptr);
}
response.into()
}