use std::fmt;
use std::error::Error as StdError;
pub type Result<T> = std::result::Result<T, RusantaError>;
#[derive(Debug)]
pub enum RusantaError {
Internal {
message: String,
},
NotImplemented {
feature: &'static str,
},
ShapeMismatch {
expected: Vec<usize>,
found: Vec<usize>,
},
DimensionMismatch {
lhs: usize,
rhs: usize,
},
EmptyData {
context: &'static str,
},
IndexOutOfBounds {
index: usize,
len: usize,
},
ColumnNotFound {
name: String,
},
InvalidType {
expected: &'static str,
found: &'static str,
},
InvalidValue {
message: String,
},
ModelNotFitted,
InvalidHyperparameter {
name: &'static str,
reason: String,
},
Io(std::io::Error),
Parse {
message: String,
},
}
impl fmt::Display for RusantaError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
RusantaError::Internal { message } => {
write!(f, "Internal Rusanta error: {}", message)
}
RusantaError::NotImplemented { feature } => {
write!(f, "Feature not implemented: {}", feature)
}
RusantaError::ShapeMismatch { expected, found } => {
write!(
f,
"Shape mismatch: expected {:?}, found {:?}",
expected, found
)
}
RusantaError::DimensionMismatch { lhs, rhs } => {
write!(
f,
"Dimension mismatch: left has {}, right has {}",
lhs, rhs
)
}
RusantaError::EmptyData { context } => {
write!(f, "Empty data encountered ({})", context)
}
RusantaError::IndexOutOfBounds { index, len } => {
write!(
f,
"Index out of bounds: index {}, length {}",
index, len
)
}
RusantaError::ColumnNotFound { name } => {
write!(f, "Column not found: '{}'", name)
}
RusantaError::InvalidType { expected, found } => {
write!(
f,
"Invalid type: expected {}, found {}",
expected, found
)
}
RusantaError::InvalidValue { message } => {
write!(f, "Invalid value: {}", message)
}
RusantaError::ModelNotFitted => {
write!(f, "Model has not been fitted")
}
RusantaError::InvalidHyperparameter { name, reason } => {
write!(
f,
"Invalid hyperparameter '{}': {}",
name, reason
)
}
RusantaError::Io(err) => {
write!(f, "IO error: {}", err)
}
RusantaError::Parse { message } => {
write!(f, "Parse error: {}", message)
}
}
}
}
impl StdError for RusantaError {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
RusantaError::Io(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for RusantaError {
fn from(err: std::io::Error) -> Self {
RusantaError::Io(err)
}
}
impl From<&str> for RusantaError {
fn from(message: &str) -> Self {
RusantaError::Internal {
message: message.to_string(),
}
}
}
impl From<String> for RusantaError {
fn from(message: String) -> Self {
RusantaError::Internal { message }
}
}