use std::error;
use std::fmt;
pub use std::error::Error as StdError;
#[derive(Debug)]
pub enum NeuralError {
InvalidArchitecture(String),
TrainingError(String),
InferenceError(String),
SerializationError(String),
DeserializationError(String),
ValidationError(String),
NotImplementedError(String),
IOError(String),
InvalidArgument(String),
ShapeMismatch(String),
ComputationError(String),
DimensionMismatch(String),
DistributedError(String),
ConfigError(String),
AllocationError(String),
DeviceError(String),
DeviceNotFound(String),
ResourceExhausted(String),
InvalidState(String),
NotImplemented(String),
MemoryError(String),
FeatureNotEnabled(String),
Other(String),
}
impl fmt::Display for NeuralError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
NeuralError::InvalidArchitecture(msg) => write!(f, "Invalid architecture: {msg}"),
NeuralError::TrainingError(msg) => write!(f, "Training error: {msg}"),
NeuralError::InferenceError(msg) => write!(f, "Inference error: {msg}"),
NeuralError::SerializationError(msg) => write!(f, "Serialization error: {msg}"),
NeuralError::DeserializationError(msg) => write!(f, "Deserialization error: {msg}"),
NeuralError::ValidationError(msg) => write!(f, "Validation error: {msg}"),
NeuralError::NotImplementedError(msg) => write!(f, "Not implemented: {msg}"),
NeuralError::IOError(msg) => write!(f, "IO error: {msg}"),
NeuralError::InvalidArgument(msg) => write!(f, "Invalid argument: {msg}"),
NeuralError::ShapeMismatch(msg) => write!(f, "Shape mismatch: {msg}"),
NeuralError::ComputationError(msg) => write!(f, "Computation error: {msg}"),
NeuralError::DimensionMismatch(msg) => write!(f, "Dimension mismatch: {msg}"),
NeuralError::DistributedError(msg) => write!(f, "Distributed training error: {msg}"),
NeuralError::ConfigError(msg) => write!(f, "Configuration error: {msg}"),
NeuralError::AllocationError(msg) => write!(f, "Allocation error: {msg}"),
NeuralError::DeviceError(msg) => write!(f, "Device error: {msg}"),
NeuralError::DeviceNotFound(msg) => write!(f, "Device not found: {msg}"),
NeuralError::ResourceExhausted(msg) => write!(f, "Resource exhausted: {msg}"),
NeuralError::InvalidState(msg) => write!(f, "Invalid state: {msg}"),
NeuralError::NotImplemented(msg) => write!(f, "Not implemented: {msg}"),
NeuralError::MemoryError(msg) => write!(f, "Memory error: {msg}"),
NeuralError::FeatureNotEnabled(msg) => write!(f, "Feature not enabled: {msg}"),
NeuralError::Other(msg) => write!(f, "Error: {msg}"),
}
}
}
impl error::Error for NeuralError {}
pub type Error = NeuralError;
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(not(feature = "gpu"))]
pub struct DummyGpuBackend;
impl From<std::io::Error> for NeuralError {
fn from(error: std::io::Error) -> Self {
NeuralError::IOError(error.to_string())
}
}
impl From<scirs2_core::ndarray::ShapeError> for NeuralError {
fn from(error: scirs2_core::ndarray::ShapeError) -> Self {
NeuralError::ShapeMismatch(format!("Shape error: {error}"))
}
}