use core::fmt::{Display, Formatter};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum ErrorKind {
InvalidTensorIndex( usize, usize),
InvalidTensorDataCount( usize, usize),
FailedToResizeInputTensor( usize),
FailedToResizeNamedInputTensor,
AllocateTensorsRequired,
InvalidTensorDataType,
FailedToAllocateTensors,
FailedToCopyDataToInputTensor,
FailedToLoadModel,
FailedToCreateInterpreter,
ReadTensorError,
InvokeInterpreterRequired,
InvalidSignatureRunner,
InvalidTensorName,
}
impl ErrorKind {
pub(crate) fn as_string(&self) -> String {
match *self {
ErrorKind::InvalidTensorIndex(index, max_index) => {
format!("invalid tensor index {index}, max index is {max_index}")
}
ErrorKind::InvalidTensorDataCount(provided, required) => {
format!("provided data count {provided} must match the required count {required}")
}
ErrorKind::InvalidTensorDataType => {
"tensor data type is unsupported or could not be determined due to a model error"
.to_string()
}
ErrorKind::FailedToResizeInputTensor(index) => {
format!("failed to resize input tensor at index {index}")
}
ErrorKind::FailedToResizeNamedInputTensor => {
"failed to resize tensor for the given name".to_string()
}
ErrorKind::AllocateTensorsRequired => "must call allocate_tensors()".to_string(),
ErrorKind::FailedToAllocateTensors => {
"failed to allocate memory for input tensors".to_string()
}
ErrorKind::FailedToCopyDataToInputTensor => {
"failed to copy data to input tensor".to_string()
}
ErrorKind::FailedToLoadModel => "failed to load the given model".to_string(),
ErrorKind::FailedToCreateInterpreter => "failed to create the interpreter".to_string(),
ErrorKind::ReadTensorError => "failed to read tensor".to_string(),
ErrorKind::InvokeInterpreterRequired => "must call invoke()".to_string(),
ErrorKind::InvalidSignatureRunner => {
"failed to get signature runner for the given key".to_string()
}
ErrorKind::InvalidTensorName => "failed to get tensor for the given name".to_string(),
}
}
}
impl Display for ErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.as_string())
}
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub struct Error {
kind: ErrorKind,
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.kind)
}
}
impl std::error::Error for Error {}
impl Error {
pub(crate) fn new(kind: ErrorKind) -> Error {
Error { kind }
}
pub fn kind(&self) -> ErrorKind {
self.kind
}
}
pub type Result<T> = std::result::Result<T, Error>;