fix(ffi): report compact parse errors with file offsets
Format the parser error reason and absolute byte offset without dumping the remaining input bytes. Calculate offsets correctly for errors inside bounded selected-cell ranges.
This commit is contained in:
parent
e858ec83c9
commit
4108baf0a1
1 changed files with 87 additions and 4 deletions
18
src/lib.rs
18
src/lib.rs
|
|
@ -54,6 +54,18 @@ fn describe_panic(payload: Box<dyn Any + Send>) -> String {
|
|||
}
|
||||
|
||||
|
||||
fn describe_parse_error(input: &[u8], (remaining, reason): (&[u8], crate::basic::ErrType)) -> String {
|
||||
// Selected-cell errors may contain a bounded range rather than a file suffix.
|
||||
let offset = remaining.as_ptr() as usize - input.as_ptr() as usize;
|
||||
let message = match reason {
|
||||
crate::basic::ErrType::Failed(message) => message,
|
||||
crate::basic::ErrType::Incomplete(Some(size)) => format!("Unexpected end of input; need {size} bytes"),
|
||||
crate::basic::ErrType::Incomplete(None) => "Unexpected end of input".to_string(),
|
||||
};
|
||||
format!("at byte {offset}: {message}")
|
||||
}
|
||||
|
||||
|
||||
fn export_struct_array(
|
||||
op_name: &str,
|
||||
arr: *mut FFI_ArrowArray,
|
||||
|
|
@ -128,7 +140,7 @@ pub unsafe extern "C" fn read_path(
|
|||
let input = fs::read(Path::new(&path))
|
||||
.map_err(|err| format!("file read failed for {}: {err}", path.display()))?;
|
||||
let (_input, struct_arr) = read_library(&input)
|
||||
.map_err(|err| format!("read failed: {err:?}"))?;
|
||||
.map_err(|err| format!("read failed {}", describe_parse_error(&input, err)))?;
|
||||
Ok(struct_arr)
|
||||
})
|
||||
}
|
||||
|
|
@ -166,7 +178,7 @@ pub unsafe extern "C" fn scan_bytes(
|
|||
export_struct_array("scan_bytes", arr, schema, || {
|
||||
let input = unsafe { input_slice(data, size, "data") }?;
|
||||
let (_input, struct_arr) = scan_library(input)
|
||||
.map_err(|err| format!("scan failed: {err:?}"))?;
|
||||
.map_err(|err| format!("scan failed {}", describe_parse_error(input, err)))?;
|
||||
Ok(struct_arr)
|
||||
})
|
||||
}
|
||||
|
|
@ -198,7 +210,7 @@ pub unsafe extern "C" fn read_cells_bytes(
|
|||
.map(|chunk| (chunk[0], chunk[1]))
|
||||
.collect();
|
||||
let (_input, struct_arr) = read_selected_library(input, &cell_ranges)
|
||||
.map_err(|err| format!("selected-cell read failed: {err:?}"))?;
|
||||
.map_err(|err| format!("selected-cell read failed {}", describe_parse_error(input, err)))?;
|
||||
Ok(struct_arr)
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue