1use crate::dtype::DataType;
2use std::fmt::{self, Display};
3use std::error::Error;
4
5#[derive(Debug)]
10pub enum AxionError {
11 MismatchedLengths {
15 expected: usize,
17 found: usize,
19 name: String
21 },
22
23 DuplicateColumnName(String),
27
28 ColumnNotFound(String),
32
33 TypeError {
37 expected: String,
39 found: DataType,
41 name: String
43 },
44
45 TypeMismatch {
49 expected: DataType,
51 found: DataType,
53 name: String
55 },
56
57 NoColumnsProvided,
61
62 CastError(CastError),
64
65 JoinKeyTypeError {
67 side: String,
69 name: String,
71 expected: DataType,
73 found: DataType,
75 },
76
77 IndexOutOfBounds(usize, usize), IndexOutOfRange(usize, usize), ComputeError(String),
89
90 UnsupportedOperation(String),
94
95 InvalidArgument(String),
99
100 InternalError(String),
104
105 CsvError(String),
109
110 IoError(String),
114
115 Other(String),
117}
118
119#[derive(Debug)]
123pub struct CastError(pub String);
124
125impl Display for CastError {
126 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127 write!(f, "Cast error: {}", self.0)
128 }
129}
130
131impl Error for CastError {}
132
133impl Display for AxionError {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 match self {
136 AxionError::MismatchedLengths { expected, found, name } => write!(
137 f,
138 "列长度不匹配: 列 '{}' 期望长度 {},但发现长度 {}",
139 name, expected, found
140 ),
141 AxionError::DuplicateColumnName(name) => {
142 write!(f, "发现重复的列名: '{}'", name)
143 }
144 AxionError::ColumnNotFound(name) => write!(f, "未找到列: '{}'", name),
145 AxionError::TypeError { expected, found, name } => write!(
146 f,
147 "列 '{}' 类型错误: 期望 {},发现 {:?}",
148 name, expected, found
149 ),
150 AxionError::TypeMismatch { expected, found, name } => write!(
151 f,
152 "列 '{}' 类型不匹配: 期望 {:?},发现 {:?}",
153 name, expected, found
154 ),
155 AxionError::NoColumnsProvided => write!(f, "创建 DataFrame 时未提供任何列"),
156 AxionError::CastError(err) => write!(f, "{}", err),
157 AxionError::JoinKeyTypeError { side, name, expected, found } => write!(
158 f,
159 "{} 表的连接键列 '{}' 类型无效: 期望 {:?},发现 {:?}",
160 side, name, expected, found
161 ),
162 AxionError::IndexOutOfBounds(index, len) => write!(
163 f,
164 "索引越界: 集合长度为 {},但访问索引为 {}",
165 len, index
166 ),
167 AxionError::IndexOutOfRange(index, len) => write!(
168 f,
169 "索引超出范围: 集合长度为 {},但访问索引为 {}",
170 len, index
171 ),
172 AxionError::ComputeError(msg) => write!(f, "计算错误: {}", msg),
173 AxionError::UnsupportedOperation(msg) => write!(f, "不支持的操作: {}", msg),
174 AxionError::InvalidArgument(msg) => write!(f, "无效参数: {}", msg),
175 AxionError::InternalError(msg) => write!(f, "内部错误: {},请报告此问题", msg),
176 AxionError::CsvError(msg) => write!(f, "CSV 错误: {}", msg),
177 AxionError::IoError(msg) => write!(f, "IO 错误: {}", msg),
178 AxionError::Other(msg) => write!(f, "Axion 错误: {}", msg),
179 }
180 }
181}
182
183impl Error for AxionError {
184 fn source(&self) -> Option<&(dyn Error + 'static)> {
185 match self {
186 AxionError::CastError(err) => Some(err),
187 _ => None,
188 }
189 }
190}
191
192pub type AxionResult<T> = std::result::Result<T, AxionError>;
205
206impl From<csv::Error> for AxionError {
207 fn from(err: csv::Error) -> Self {
208 AxionError::CsvError(format!("CSV 处理错误: {}", err))
209 }
210}
211
212impl From<std::string::FromUtf8Error> for AxionError {
213 fn from(err: std::string::FromUtf8Error) -> Self {
214 AxionError::IoError(format!("UTF-8 转换错误: {}", err))
215 }
216}
217
218impl From<std::io::Error> for AxionError {
219 fn from(err: std::io::Error) -> Self {
220 AxionError::IoError(err.to_string())
221 }
222}