2023-11-02 13:59:41 -04:00
|
|
|
use crate::general::Result;
|
|
|
|
|
use core::str::FromStr;
|
2023-02-14 18:30:08 +01:00
|
|
|
use serde::de::DeserializeOwned;
|
2023-11-02 13:59:41 -04:00
|
|
|
use std::ffi::CStr;
|
|
|
|
|
use waku_sys::WakuCallBack;
|
|
|
|
|
use waku_sys::{RET_ERR, RET_MISSING_CALLBACK, RET_OK};
|
|
|
|
|
pub fn decode<T: DeserializeOwned>(input: &str) -> Result<T> {
|
|
|
|
|
serde_json::from_str(input)
|
|
|
|
|
.map_err(|err| format!("could not deserialize waku response: {}", err))
|
|
|
|
|
}
|
2023-02-14 18:30:08 +01:00
|
|
|
|
2023-11-02 13:59:41 -04:00
|
|
|
unsafe extern "C" fn trampoline<F>(
|
2023-11-07 15:50:35 -04:00
|
|
|
_ret_code: ::std::os::raw::c_int,
|
2023-11-02 13:59:41 -04:00
|
|
|
data: *const ::std::os::raw::c_char,
|
|
|
|
|
user_data: *mut ::std::os::raw::c_void,
|
|
|
|
|
) where
|
|
|
|
|
F: FnMut(&str),
|
|
|
|
|
{
|
|
|
|
|
let user_data = &mut *(user_data as *mut F);
|
2023-11-07 15:50:35 -04:00
|
|
|
|
|
|
|
|
let response = if data.is_null() {
|
|
|
|
|
""
|
|
|
|
|
} else {
|
|
|
|
|
unsafe { CStr::from_ptr(data) }
|
|
|
|
|
.to_str()
|
|
|
|
|
.map_err(|err| {
|
|
|
|
|
format!(
|
|
|
|
|
"could not retrieve response from pointer returned by waku: {}",
|
|
|
|
|
err
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.expect("could not retrieve response")
|
|
|
|
|
};
|
2023-02-14 18:30:08 +01:00
|
|
|
|
2023-11-02 13:59:41 -04:00
|
|
|
user_data(response);
|
|
|
|
|
}
|
2023-02-14 18:30:08 +01:00
|
|
|
|
2023-11-02 13:59:41 -04:00
|
|
|
pub fn get_trampoline<F>(_closure: &F) -> WakuCallBack
|
|
|
|
|
where
|
|
|
|
|
F: FnMut(&str),
|
|
|
|
|
{
|
|
|
|
|
Some(trampoline::<F>)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn handle_no_response(code: i32, error: &str) -> Result<()> {
|
2024-02-08 17:16:22 -04:00
|
|
|
match code as u32 {
|
2023-11-02 13:59:41 -04:00
|
|
|
RET_OK => Ok(()),
|
|
|
|
|
RET_ERR => Err(format!("waku error: {}", error)),
|
|
|
|
|
RET_MISSING_CALLBACK => Err("missing callback".to_string()),
|
|
|
|
|
_ => Err(format!("undefined return code {}", code)),
|
2023-02-14 18:30:08 +01:00
|
|
|
}
|
2023-11-02 13:59:41 -04:00
|
|
|
}
|
2023-02-14 18:30:08 +01:00
|
|
|
|
2023-11-02 13:59:41 -04:00
|
|
|
pub fn handle_json_response<F: DeserializeOwned>(code: i32, result: &str) -> Result<F> {
|
2024-02-08 17:16:22 -04:00
|
|
|
match code as u32 {
|
2023-11-02 13:59:41 -04:00
|
|
|
RET_OK => decode(result),
|
|
|
|
|
RET_ERR => Err(format!("waku error: {}", result)),
|
|
|
|
|
RET_MISSING_CALLBACK => Err("missing callback".to_string()),
|
|
|
|
|
_ => Err(format!("undefined return code {}", code)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn handle_response<F: FromStr>(code: i32, result: &str) -> Result<F> {
|
2024-02-08 17:16:22 -04:00
|
|
|
match code as u32 {
|
2023-11-02 13:59:41 -04:00
|
|
|
RET_OK => result
|
|
|
|
|
.parse()
|
|
|
|
|
.map_err(|_| format!("could not parse value: {}", result)),
|
|
|
|
|
RET_ERR => Err(format!("waku error: {}", result)),
|
|
|
|
|
RET_MISSING_CALLBACK => Err("missing callback".to_string()),
|
|
|
|
|
_ => Err(format!("undefined return code {}", code)),
|
|
|
|
|
}
|
2023-02-14 18:30:08 +01:00
|
|
|
}
|