use litert_sys::{self as sys, LiteRtStatus};
use crate::ElementType;
pub type Result<T> = std::result::Result<T, Error>;
#[allow(missing_docs)]
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("LiteRT status {code}: {message}")]
Status { code: LiteRtStatus, message: String },
#[error("null pointer returned from the LiteRT C API")]
NullPointer,
#[error("tensor type mismatch: expected {expected:?}, got {actual:?}")]
TypeMismatch {
expected: ElementType,
actual: ElementType,
},
#[error("path contains non-UTF-8 characters: {0:?}")]
InvalidPath(std::path::PathBuf),
#[error(
"tensor buffer size {size} is not a whole number of {type_name} elements \
(size {element_size})"
)]
UnalignedBufferSize {
size: usize,
element_size: usize,
type_name: &'static str,
},
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("unsupported: {0}")]
Unsupported(&'static str),
}
pub(crate) fn check(status: LiteRtStatus) -> Result<()> {
if status == sys::kLiteRtStatusOk {
return Ok(());
}
Err(Error::Status {
code: status,
message: status_message(status).to_string(),
})
}
fn status_message(status: LiteRtStatus) -> &'static str {
match status {
sys::kLiteRtStatusErrorInvalidArgument => "invalid argument",
sys::kLiteRtStatusErrorMemoryAllocationFailure => "memory allocation failure",
sys::kLiteRtStatusErrorRuntimeFailure => "runtime failure",
sys::kLiteRtStatusErrorMissingInputTensor => "missing input tensor",
sys::kLiteRtStatusErrorUnsupported => "unsupported",
sys::kLiteRtStatusErrorNotFound => "not found",
sys::kLiteRtStatusErrorTimeoutExpired => "timeout expired",
sys::kLiteRtStatusErrorWrongVersion => "wrong version",
sys::kLiteRtStatusErrorUnknown => "unknown error",
sys::kLiteRtStatusErrorAlreadyExists => "already exists",
sys::kLiteRtStatusCancelled => "cancelled",
sys::kLiteRtStatusErrorFileIO => "file I/O error",
sys::kLiteRtStatusErrorInvalidFlatbuffer => "invalid flatbuffer",
sys::kLiteRtStatusErrorDynamicLoading => "dynamic loading error",
sys::kLiteRtStatusErrorSerialization => "serialization error",
sys::kLiteRtStatusErrorCompilation => "compilation error",
sys::kLiteRtStatusErrorIndexOOB => "index out of bounds",
sys::kLiteRtStatusErrorInvalidIrType => "invalid IR type",
sys::kLiteRtStatusErrorInvalidGraphInvariant => "invalid graph invariant",
sys::kLiteRtStatusErrorGraphModification => "graph modification error",
sys::kLiteRtStatusErrorInvalidToolConfig => "invalid tool config",
sys::kLiteRtStatusLegalizeNoMatch => "legalization: no match",
sys::kLiteRtStatusErrorInvalidLegalization => "invalid legalization",
sys::kLiteRtStatusPatternNoMatch => "pattern: no match",
sys::kLiteRtStatusInvalidTransformation => "invalid transformation",
sys::kLiteRtStatusErrorUnsupportedRuntimeVersion => "unsupported runtime version",
sys::kLiteRtStatusErrorUnsupportedCompilerVersion => "unsupported compiler version",
sys::kLiteRtStatusErrorIncompatibleByteCodeVersion => "incompatible bytecode version",
sys::kLiteRtStatusErrorUnsupportedOpShapeInferer => "unsupported op shape inferer",
sys::kLiteRtStatusErrorShapeInferenceFailed => "shape inference failed",
_ => "unrecognized status code",
}
}