use super::types::{FFIResult, JSONEvalHandle};
use std::ffi::CStr;
use std::os::raw::c_char;
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_evaluated_schema(
handle: *mut JSONEvalHandle,
skip_layout: bool,
) -> FFIResult {
if handle.is_null() {
return FFIResult::error("Invalid handle pointer".to_string());
}
let eval = &mut (*handle).inner;
let result = eval.get_evaluated_schema(skip_layout);
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_evaluated_schema_msgpack(
handle: *mut JSONEvalHandle,
skip_layout: bool,
) -> FFIResult {
if handle.is_null() {
return FFIResult::error("Invalid handle pointer".to_string());
}
let eval = &mut (*handle).inner;
match eval.get_evaluated_schema_msgpack(skip_layout) {
Ok(msgpack_bytes) => FFIResult::success(msgpack_bytes),
Err(e) => FFIResult::error(e),
}
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_schema_value(handle: *mut JSONEvalHandle) -> FFIResult {
if handle.is_null() {
return FFIResult::error("Invalid handle pointer".to_string());
}
let eval = &mut (*handle).inner;
let result = eval.get_schema_value();
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_schema_value_array(
handle: *mut JSONEvalHandle,
) -> FFIResult {
if handle.is_null() {
return FFIResult::error("Invalid handle pointer".to_string());
}
let eval = &(*handle).inner;
let result = eval.get_schema_value_array();
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_schema_value_object(
handle: *mut JSONEvalHandle,
) -> FFIResult {
if handle.is_null() {
return FFIResult::error("Invalid handle pointer".to_string());
}
let eval = &(*handle).inner;
let result = eval.get_schema_value_object();
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_evaluated_schema_without_params(
handle: *mut JSONEvalHandle,
skip_layout: bool,
) -> FFIResult {
if handle.is_null() {
return FFIResult::error("Invalid handle pointer".to_string());
}
let eval = &mut (*handle).inner;
let result = eval.get_evaluated_schema_without_params(skip_layout);
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_evaluated_schema_by_path(
handle: *mut JSONEvalHandle,
path: *const c_char,
skip_layout: bool,
) -> FFIResult {
if handle.is_null() || path.is_null() {
return FFIResult::error("Invalid handle or path pointer".to_string());
}
let eval = &mut (*handle).inner;
let path_str = match CStr::from_ptr(path).to_str() {
Ok(s) => s,
Err(_) => return FFIResult::error("Invalid UTF-8 in path".to_string()),
};
match eval.get_evaluated_schema_by_path(path_str, skip_layout) {
Some(value) => {
let result_bytes = serde_json::to_vec(&value).unwrap_or_default();
FFIResult::success(result_bytes)
}
None => FFIResult::error("Path not found".to_string()),
}
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_evaluated_schema_by_paths(
handle: *mut JSONEvalHandle,
paths_json: *const c_char,
skip_layout: bool,
format: u8,
) -> FFIResult {
if handle.is_null() || paths_json.is_null() {
return FFIResult::error("Invalid handle or paths pointer".to_string());
}
let eval = &mut (*handle).inner;
let paths_str = match CStr::from_ptr(paths_json).to_str() {
Ok(s) => s,
Err(_) => return FFIResult::error("Invalid UTF-8 in paths".to_string()),
};
let paths: Vec<String> = match serde_json::from_str(paths_str) {
Ok(p) => p,
Err(e) => return FFIResult::error(format!("Failed to parse paths JSON: {}", e)),
};
let return_format = match format {
1 => crate::ReturnFormat::Flat,
2 => crate::ReturnFormat::Array,
_ => crate::ReturnFormat::Nested,
};
let result = eval.get_evaluated_schema_by_paths(&paths, skip_layout, Some(return_format));
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_schema_by_path(
handle: *mut JSONEvalHandle,
path: *const c_char,
) -> FFIResult {
if handle.is_null() || path.is_null() {
return FFIResult::error("Invalid handle or path pointer".to_string());
}
let eval = &(*handle).inner;
let path_str = match CStr::from_ptr(path).to_str() {
Ok(s) => s,
Err(_) => return FFIResult::error("Invalid UTF-8 in path".to_string()),
};
match eval.get_schema_by_path(path_str) {
Some(value) => {
let result_bytes = serde_json::to_vec(&value).unwrap_or_default();
FFIResult::success(result_bytes)
}
None => FFIResult::error("Path not found".to_string()),
}
}
#[no_mangle]
pub unsafe extern "C" fn json_eval_get_schema_by_paths(
handle: *mut JSONEvalHandle,
paths_json: *const c_char,
format: u8,
) -> FFIResult {
if handle.is_null() || paths_json.is_null() {
return FFIResult::error("Invalid handle or paths pointer".to_string());
}
let eval = &(*handle).inner;
let paths_str = match CStr::from_ptr(paths_json).to_str() {
Ok(s) => s,
Err(_) => return FFIResult::error("Invalid UTF-8 in paths".to_string()),
};
let paths: Vec<String> = match serde_json::from_str(paths_str) {
Ok(p) => p,
Err(e) => return FFIResult::error(format!("Failed to parse paths JSON: {}", e)),
};
let return_format = match format {
1 => crate::ReturnFormat::Flat,
2 => crate::ReturnFormat::Array,
_ => crate::ReturnFormat::Nested,
};
let result = eval.get_schema_by_paths(&paths, Some(return_format));
let result_bytes = serde_json::to_vec(&result).unwrap_or_default();
FFIResult::success(result_bytes)
}