Skip to main content

xlsx_rs/
error.rs

1//! 统一错误类型。
2//!
3//! 本 crate 的所有公共 API 都返回 [`Result<T, Error>`](crate::Result)。
4//! 错误来源主要有三类:
5//!
6//! - **OPC 容器层错误**:来自 [`ooxml_core::Error`],通过 `From` 转换;
7//! - **XLSX schema 错误**:解析 `.xlsx` 内部 XML 时遇到不符合 OOXML 规范的结构;
8//! - **用户 API 错误**:如无效的单元格引用(`"A1X"`)、越界索引等。
9
10use thiserror::Error;
11
12/// XLSX 读写过程中可能发生的错误。
13#[derive(Debug, Error)]
14pub enum Error {
15    /// IO 错误(文件读写)。
16    #[error("io: {0}")]
17    Io(#[from] std::io::Error),
18
19    /// zip 解压/压缩错误。
20    #[error("zip: {0}")]
21    Zip(#[from] zip::result::ZipError),
22
23    /// ooxml-core 公共基座错误(OPC / XML 解析 / 单位换算等)。
24    #[error("ooxml-core: {0}")]
25    OoxmlCore(#[from] ooxml_core::Error),
26
27    /// XML 解析错误(quick-xml)。
28    #[error("xml: {0}")]
29    Xml(String),
30
31    /// XLSX schema 错误:解析到的 XML 不符合 OOXML SpreadsheetML 规范。
32    #[error("xlsx schema: {0}")]
33    Schema(String),
34
35    /// 无效的单元格引用(如 `"A1X"` / `""` / `"0A"`)。
36    #[error("invalid cell reference: {0}")]
37    InvalidCellRef(String),
38
39    /// 无效的工作表名称(含非法字符或长度超限)。
40    #[error("invalid sheet name: {0}")]
41    InvalidSheetName(String),
42
43    /// 索引越界(如访问不存在的 sheet/cell)。
44    #[error("index out of range: {0}")]
45    IndexOutOfRange(usize),
46
47    /// 功能尚未实现(路线图中的 TODO 项)。
48    #[error("not implemented: {0}")]
49    NotImplemented(&'static str),
50
51    /// 其他未分类错误。
52    #[error("{0}")]
53    Other(String),
54}
55
56/// XLSX 专用 Result 别名。
57pub type Result<T> = std::result::Result<T, Error>;