2023-02-14 18:30:08 +01:00
|
|
|
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_and_free_response<T: DeserializeOwned>(response_ptr: *mut c_char) -> Result<T> {
|
|
|
|
|
let response = unsafe { CStr::from_ptr(response_ptr) }
|
|
|
|
|
.to_str()
|
2023-09-27 19:19:59 -04:00
|
|
|
.map_err(|err| {
|
|
|
|
|
format!(
|
|
|
|
|
"could not retrieve response from pointer returned by waku: {}",
|
|
|
|
|
err
|
|
|
|
|
)
|
|
|
|
|
})?;
|
2023-02-14 18:30:08 +01:00
|
|
|
|
2023-09-27 19:19:59 -04:00
|
|
|
let response: JsonResponse<T> = serde_json::from_str(response)
|
|
|
|
|
.map_err(|err| format!("could not deserialize waku JsonResponse: {}", err))?;
|
2023-02-14 18:30:08 +01:00
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
waku_sys::waku_utils_free(response_ptr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response.into()
|
|
|
|
|
}
|