use thiserror::Error;
pub type NnResult<T> = Result<T, NnError>;
#[derive(Error, Debug)]
pub enum NnError {
#[error("Configuration error: {0}")]
Config(String),
#[error("Failed to load model: {0}")]
ModelLoad(String),
#[error("Inference failed: {0}")]
Inference(String),
#[error("Shape mismatch: expected {expected:?}, got {actual:?}")]
ShapeMismatch {
expected: Vec<usize>,
actual: Vec<usize>,
},
#[error("Invalid input: {0}")]
InvalidInput(String),
#[error("Backend not available: {0}")]
BackendUnavailable(String),
#[cfg(feature = "onnx")]
#[error("ONNX Runtime error: {0}")]
OnnxRuntime(#[from] ort::Error),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Tensor operation error: {0}")]
TensorOp(String),
#[error("Unsupported operation: {0}")]
Unsupported(String),
}
impl NnError {
pub fn config<S: Into<String>>(msg: S) -> Self {
NnError::Config(msg.into())
}
pub fn model_load<S: Into<String>>(msg: S) -> Self {
NnError::ModelLoad(msg.into())
}
pub fn inference<S: Into<String>>(msg: S) -> Self {
NnError::Inference(msg.into())
}
pub fn shape_mismatch(expected: Vec<usize>, actual: Vec<usize>) -> Self {
NnError::ShapeMismatch { expected, actual }
}
pub fn invalid_input<S: Into<String>>(msg: S) -> Self {
NnError::InvalidInput(msg.into())
}
pub fn tensor_op<S: Into<String>>(msg: S) -> Self {
NnError::TensorOp(msg.into())
}
}