use std::error::Error;
use std::fmt;
#[derive(Debug, PartialEq)]
pub enum MinarrowError {
ColumnLengthMismatch {
col: usize,
expected: usize,
found: usize,
},
Overflow {
value: String,
target: &'static str,
},
LossyCast {
value: String,
target: &'static str,
},
TypeError {
from: &'static str,
to: &'static str,
message: Option<String>,
},
NullError {
message: Option<String>,
},
IncompatibleTypeError {
from: &'static str,
to: &'static str,
message: Option<String>,
},
KernelError(Option<String>),
ShapeError {
message: String,
},
NotImplemented {
feature: String,
},
IndexError(String),
}
impl fmt::Display for MinarrowError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
MinarrowError::ColumnLengthMismatch {
col,
expected,
found,
} => {
write!(
f,
"Column length mismatch in column {}: expected {}, found {}.",
col, expected, found
)
}
MinarrowError::Overflow { value, target } => {
write!(
f,
"Overflow: value '{}' cannot be represented in type '{}'.",
value, target
)
}
MinarrowError::LossyCast { value, target } => {
write!(
f,
"Lossy cast: value '{}' loses precision or cannot be exactly represented as '{}'.",
value, target
)
}
MinarrowError::TypeError { from, to, message } => {
if let Some(msg) = message {
write!(
f,
"Type error: cannot cast from '{}' to '{}': {}",
from, to, msg
)
} else {
write!(f, "Type error: cannot cast from '{}' to '{}'.", from, to)
}
}
MinarrowError::NullError { message } => {
if let Some(msg) = message {
write!(f, "Null error: {}", msg)
} else {
write!(f, "Null error: nulls cannot be represented in target type.")
}
}
MinarrowError::IncompatibleTypeError { from, to, message } => {
if let Some(msg) = message {
write!(
f,
"Incompatible type error: cannot convert from '{}' to '{}': {}",
from, to, msg
)
} else {
write!(
f,
"Incompatible type error: cannot convert from '{}' to '{}'.",
from, to
)
}
}
MinarrowError::KernelError(message) => {
if let Some(msg) = message {
write!(f, "Kernel error: {}", msg)
} else {
write!(f, "Kernel error")
}
}
MinarrowError::ShapeError { message } => {
write!(f, "Shape error: {}", message)
}
MinarrowError::NotImplemented { feature } => {
write!(f, "Not implemented: {}", feature)
}
MinarrowError::IndexError(message) => {
write!(f, "Index error: {}", message)
}
}
}
}
impl Error for MinarrowError {}
#[derive(Debug, Clone)]
pub enum KernelError {
TypeMismatch(String),
LengthMismatch(String),
BroadcastingError(String),
OperatorMismatch(String),
UnsupportedType(String),
ColumnNotFound(String),
InvalidArguments(String),
Plan(String),
OutOfBounds(String),
DivideByZero(String),
}
impl fmt::Display for KernelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
KernelError::TypeMismatch(msg) => write!(f, "Type mismatch: {}", msg),
KernelError::LengthMismatch(msg) => write!(f, "Length mismatch: {}", msg),
KernelError::OperatorMismatch(msg) => write!(f, "Operator mismatch: {}", msg),
KernelError::BroadcastingError(msg) => write!(f, "Shape Error: {}", msg),
KernelError::UnsupportedType(msg) => write!(f, "Unsupported type: {}", msg),
KernelError::ColumnNotFound(msg) => write!(f, "Column not found: {}", msg),
KernelError::InvalidArguments(msg) => write!(f, "Invalid arguments: {}", msg),
KernelError::Plan(msg) => write!(f, "Planning error: {}", msg),
KernelError::OutOfBounds(msg) => write!(f, "Out of bounds: {}", msg),
KernelError::DivideByZero(msg) => write!(f, "Divide by Zero error: {}", msg),
}
}
}
impl Error for KernelError {}
impl From<KernelError> for MinarrowError {
fn from(err: KernelError) -> Self {
MinarrowError::KernelError(Some(err.to_string()))
}
}
pub fn log_length_mismatch(fname: String, lhs: usize, rhs: usize) -> String {
return format!("{} => Length mismatch: LHS {} RHS {}", fname, lhs, rhs);
}