use std::ffi::NulError;
use std::io;
use std::num::ParseIntError;
use thiserror::Error;
use zip::result::ZipError;
#[derive(Error, Debug)]
pub enum TchError {
#[error("conversion error: {0}")]
Convert(String),
#[error("invalid file format: {0}")]
FileFormat(String),
#[error("cannot find the tensor named {0} in {1}")]
TensorNameNotFound(String, String),
#[error(transparent)]
Io(#[from] io::Error),
#[error("tensor kind error: {0}")]
Kind(String),
#[error("no image found in {0}")]
MissingImage(String),
#[error(transparent)]
Nul(#[from] NulError),
#[error(transparent)]
ParseInt(#[from] ParseIntError),
#[error("invalid shape: {0}")]
Shape(String),
#[error("unknown kind: {0}")]
UnknownKind(libc::c_int),
#[error("Internal torch error: {0}")]
Torch(String),
#[error(transparent)]
Zip(#[from] ZipError),
#[error(transparent)]
NdArray(#[from] ndarray::ShapeError),
#[error("safetensors error {path}: {err}")]
SafeTensorError { path: String, err: safetensors::SafeTensorError },
}
impl TchError {
pub fn path_context(&self, path_name: &str) -> Self {
match self {
TchError::Torch(error) => TchError::Torch(format!("{path_name}: {error}")),
_ => unimplemented!(),
}
}
}