bloqade_lanes_bytecode/ffi/
error.rs

1use std::cell::RefCell;
2use std::ffi::CString;
3use std::os::raw::c_char;
4
5/// Status codes returned by all fallible C-API functions.
6#[repr(C)]
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum BlqdStatus {
9    Ok = 0,
10    ErrParse = 1,
11    ErrDecode = 2,
12    ErrValidation = 3,
13    ErrIo = 4,
14    ErrNullPtr = 5,
15    ErrJson = 6,
16}
17
18thread_local! {
19    static LAST_ERROR: RefCell<Option<CString>> = const { RefCell::new(None) };
20}
21
22pub(crate) fn set_last_error(msg: impl Into<Vec<u8>>) {
23    LAST_ERROR.with(|cell| {
24        *cell.borrow_mut() = CString::new(msg.into()).ok();
25    });
26}
27
28pub(crate) fn clear_last_error() {
29    LAST_ERROR.with(|cell| {
30        *cell.borrow_mut() = None;
31    });
32}
33
34/// Returns a pointer to the last error message, or NULL if no error.
35/// Valid until the next C-API call on the same thread.
36#[unsafe(no_mangle)]
37pub extern "C" fn blqd_last_error() -> *const c_char {
38    LAST_ERROR.with(|cell| {
39        let borrow = cell.borrow();
40        match &*borrow {
41            Some(cstr) => cstr.as_ptr(),
42            None => std::ptr::null(),
43        }
44    })
45}