use std::ffi::c_void;
use arrow::array::{RecordBatch, StructArray};
use arrow::ffi::{from_ffi, FFI_ArrowArray, FFI_ArrowSchema};
use crate::workbook::{build_specs, check, ExcelMapper, Workbook};
use crate::{Error, XL_ERROR};
pub fn parse_arrow<T: ExcelMapper>(
workbook: &mut Workbook,
header_row: i32,
) -> Result<RecordBatch, Error> {
let arena = build_specs::<T>();
let mut array = FFI_ArrowArray::empty();
let mut schema = FFI_ArrowSchema::empty();
check(unsafe {
crate::xl_parse_arrow(
workbook.handle(),
arena.specs.as_ptr(),
arena.specs.len() as i32,
header_row,
&mut array as *mut FFI_ArrowArray as *mut c_void,
&mut schema as *mut FFI_ArrowSchema as *mut c_void,
)
})?;
let data = unsafe { from_ffi(array, &schema) }.map_err(|e| {
Error::from_status(
XL_ERROR,
format!("importing the native Arrow array failed: {e}"),
)
})?;
Ok(RecordBatch::from(StructArray::from(data)))
}