use crate::dtype::DataType;
use std::fmt::{self, Display};
use std::error::Error;
#[derive(Debug)]
pub enum AxionError {
MismatchedLengths {
expected: usize,
found: usize,
name: String
},
DuplicateColumnName(String),
ColumnNotFound(String),
TypeError {
expected: String,
found: DataType,
name: String
},
TypeMismatch {
expected: DataType,
found: DataType,
name: String
},
NoColumnsProvided,
CastError(CastError),
JoinKeyTypeError {
side: String,
name: String,
expected: DataType,
found: DataType,
},
IndexOutOfBounds(usize, usize),
IndexOutOfRange(usize, usize),
ComputeError(String),
UnsupportedOperation(String),
InvalidArgument(String),
InternalError(String),
CsvError(String),
IoError(String),
Other(String),
}
#[derive(Debug)]
pub struct CastError(pub String);
impl Display for CastError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Cast error: {}", self.0)
}
}
impl Error for CastError {}
impl Display for AxionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
AxionError::MismatchedLengths { expected, found, name } => write!(
f,
"列长度不匹配: 列 '{}' 期望长度 {},但发现长度 {}",
name, expected, found
),
AxionError::DuplicateColumnName(name) => {
write!(f, "发现重复的列名: '{}'", name)
}
AxionError::ColumnNotFound(name) => write!(f, "未找到列: '{}'", name),
AxionError::TypeError { expected, found, name } => write!(
f,
"列 '{}' 类型错误: 期望 {},发现 {:?}",
name, expected, found
),
AxionError::TypeMismatch { expected, found, name } => write!(
f,
"列 '{}' 类型不匹配: 期望 {:?},发现 {:?}",
name, expected, found
),
AxionError::NoColumnsProvided => write!(f, "创建 DataFrame 时未提供任何列"),
AxionError::CastError(err) => write!(f, "{}", err),
AxionError::JoinKeyTypeError { side, name, expected, found } => write!(
f,
"{} 表的连接键列 '{}' 类型无效: 期望 {:?},发现 {:?}",
side, name, expected, found
),
AxionError::IndexOutOfBounds(index, len) => write!(
f,
"索引越界: 集合长度为 {},但访问索引为 {}",
len, index
),
AxionError::IndexOutOfRange(index, len) => write!(
f,
"索引超出范围: 集合长度为 {},但访问索引为 {}",
len, index
),
AxionError::ComputeError(msg) => write!(f, "计算错误: {}", msg),
AxionError::UnsupportedOperation(msg) => write!(f, "不支持的操作: {}", msg),
AxionError::InvalidArgument(msg) => write!(f, "无效参数: {}", msg),
AxionError::InternalError(msg) => write!(f, "内部错误: {},请报告此问题", msg),
AxionError::CsvError(msg) => write!(f, "CSV 错误: {}", msg),
AxionError::IoError(msg) => write!(f, "IO 错误: {}", msg),
AxionError::Other(msg) => write!(f, "Axion 错误: {}", msg),
}
}
}
impl Error for AxionError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
AxionError::CastError(err) => Some(err),
_ => None,
}
}
}
pub type AxionResult<T> = std::result::Result<T, AxionError>;
impl From<csv::Error> for AxionError {
fn from(err: csv::Error) -> Self {
AxionError::CsvError(format!("CSV 处理错误: {}", err))
}
}
impl From<std::string::FromUtf8Error> for AxionError {
fn from(err: std::string::FromUtf8Error) -> Self {
AxionError::IoError(format!("UTF-8 转换错误: {}", err))
}
}
impl From<std::io::Error> for AxionError {
fn from(err: std::io::Error) -> Self {
AxionError::IoError(err.to_string())
}
}