use std::os::raw::c_char;
use std::ptr;
use super::conversions::c_str_to_rust;
use super::errors::set_last_error;
use super::types::{AnalyzerInternal, CAnalyzer};
#[no_mangle]
pub extern "C" fn wqa_from_config_file(path: *const c_char) -> *mut CAnalyzer {
if path.is_null() {
set_last_error("Path cannot be null".to_string());
return ptr::null_mut();
}
let path_str = unsafe {
match c_str_to_rust(path) {
Some(s) => s,
None => {
set_last_error("Invalid UTF-8 in path".to_string());
return ptr::null_mut();
}
}
};
let runtime = match tokio::runtime::Runtime::new() {
Ok(rt) => rt,
Err(e) => {
set_last_error(format!("Failed to create runtime: {}", e));
return ptr::null_mut();
}
};
let result = runtime.block_on(async { crate::from_config_file(path_str) });
match result {
Ok(analyzer) => {
let internal = Box::new(AnalyzerInternal { analyzer, runtime });
Box::into_raw(internal) as *mut CAnalyzer
}
Err(e) => {
set_last_error(format!("{}", e));
ptr::null_mut()
}
}
}