use crate::JSONEval;
use std::ffi::CString;
use std::os::raw::c_char;
use std::ptr;
use crate::jsoneval::cancellation::CancellationToken;
pub struct JSONEvalHandle {
pub(super) inner: Box<JSONEval>,
pub(super) current_token: Option<CancellationToken>,
}
impl JSONEvalHandle {
pub(super) fn reset_token(&mut self) -> Option<CancellationToken> {
if let Some(token) = &self.current_token {
token.cancel();
}
let new_token = CancellationToken::new();
self.current_token = Some(new_token.clone());
Some(new_token)
}
}
#[repr(C)]
pub struct FFIResult {
pub success: bool,
pub data_ptr: *const u8,
pub data_len: usize,
pub error: *mut c_char,
pub(super) _owned_data: *mut Vec<u8>,
}
impl Default for FFIResult {
fn default() -> Self {
Self {
success: false,
data_ptr: ptr::null(),
data_len: 0,
error: ptr::null_mut(),
_owned_data: ptr::null_mut(),
}
}
}
impl FFIResult {
pub fn success(data: Vec<u8>) -> Self {
let boxed_data = Box::new(data);
let data_ptr = boxed_data.as_ptr();
let data_len = boxed_data.len();
Self {
success: true,
data_ptr,
data_len,
error: ptr::null_mut(),
_owned_data: Box::into_raw(boxed_data),
}
}
pub fn error(msg: String) -> Self {
Self {
success: false,
data_ptr: ptr::null(),
data_len: 0,
error: CString::new(msg)
.unwrap_or_else(|_| CString::new("Error message contains null byte").unwrap())
.into_raw(),
_owned_data: ptr::null_mut(),
}
}
}