docx_handlebars/
error.rs

1use thiserror::Error;
2
3/// docx-handlebars 库的错误类型
4#[derive(Error, Debug)]
5pub enum DocxHandlebarsError {
6    #[error("IO 错误: {0}")]
7    Io(#[from] std::io::Error),
8
9    #[error("ZIP 格式错误: {0}")]
10    Zip(#[from] zip::result::ZipError),
11
12    #[error("XML 解析错误: {0}")]
13    Xml(#[from] quick_xml::Error),
14
15    #[error("Handlebars 模板错误: {0}")]
16    Template(#[from] handlebars::TemplateError),
17
18    #[error("Handlebars 渲染错误: {0}")]
19    Render(#[from] handlebars::RenderError),
20
21    #[error("JSON 序列化/反序列化错误: {0}")]
22    Json(#[from] serde_json::Error),
23
24    #[error("UTF-8 编码错误: {0}")]
25    Utf8(#[from] std::str::Utf8Error),
26
27    #[error("文档格式错误: {0}")]
28    DocumentFormat(String),
29
30    #[error("模板未加载")]
31    TemplateNotLoaded,
32
33    #[error("不支持的功能: {0}")]
34    UnsupportedFeature(String),
35
36    #[error("自定义错误: {0}")]
37    Custom(String),
38}
39
40/// 便利类型别名
41pub type Result<T> = std::result::Result<T, DocxHandlebarsError>;
42
43impl DocxHandlebarsError {
44    /// 创建自定义错误
45    pub fn custom<S: Into<String>>(message: S) -> Self {
46        Self::Custom(message.into())
47    }
48
49    /// 创建文档格式错误
50    pub fn document_format<S: Into<String>>(message: S) -> Self {
51        Self::DocumentFormat(message.into())
52    }
53
54    /// 创建不支持的功能错误
55    pub fn unsupported_feature<S: Into<String>>(feature: S) -> Self {
56        Self::UnsupportedFeature(feature.into())
57    }
58}