use crate::sys;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("{op} failed: {msg} (status {status})")]
Nrt { op: &'static str, status: sys::NRT_STATUS, msg: String },
#[error("the neuron runtime is already initialized in this process")]
AlreadyInitialized,
#[error("size mismatch in {op}: tensor region has {expected} bytes, host buffer has {got}")]
SizeMismatch { op: &'static str, expected: usize, got: usize },
#[error("interior nul byte in string: {0}")]
NulError(#[from] std::ffi::NulError),
}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn check(status: sys::NRT_STATUS, op: &'static str) -> Result<()> {
if status == sys::NRT_SUCCESS {
return Ok(());
}
let msg = unsafe { sys::nrt_get_status_as_str(status) };
let msg = if msg.is_null() {
"unknown status".to_string()
} else {
unsafe { std::ffi::CStr::from_ptr(msg) }.to_string_lossy().into_owned()
};
Err(Error::Nrt { op, status, msg })
}