image_renderer/
error.rs

1//! 错误处理模块
2//! 
3//! 提供统一的错误类型定义和处理机制
4
5use thiserror::Error;
6
7/// 图片处理库的错误类型
8#[derive(Error, Debug)]
9pub enum ImageError {
10    /// 图片加载错误
11    #[error("Failed to load image: {0}")]
12    LoadError(String),
13
14    /// 图片保存错误
15    #[error("Failed to save image: {0}")]
16    SaveError(String),
17
18    /// 图片格式不支持
19    #[error("Unsupported image format: {0}")]
20    UnsupportedFormat(String),
21
22    /// 图片尺寸无效
23    #[error("Invalid image dimensions: width={width}, height={height}")]
24    InvalidDimensions { width: u32, height: u32 },
25
26    /// 图片处理错误
27    #[error("Image processing error: {0}")]
28    ProcessingError(String),
29
30    /// IO 错误
31    #[error("IO error: {0}")]
32    IoError(#[from] std::io::Error),
33
34    /// 图片库错误
35    #[error("Image library error: {0}")]
36    ImageLibError(#[from] image::ImageError),
37}
38
39/// 字体处理错误类型
40#[derive(Error, Debug)]
41pub enum FontError {
42    /// 字体加载错误
43    #[error("Failed to load font: {0}")]
44    LoadError(String),
45
46    /// 字体不存在
47    #[error("Font not found: {0}")]
48    NotFound(String),
49
50    /// 字体格式不支持
51    #[error("Unsupported font format: {0}")]
52    UnsupportedFormat(String),
53
54    /// 字体渲染错误
55    #[error("Font rendering error: {0}")]
56    RenderError(String),
57
58    /// 字体大小无效
59    #[error("Invalid font size: {0}")]
60    InvalidSize(f32),
61
62    /// Font-kit 错误
63    #[error("Font-kit error: {0}")]
64    FontKitError(String),
65}
66
67/// 绘制错误类型
68#[derive(Error, Debug)]
69pub enum DrawError {
70    /// 无效的颜色值
71    #[error("Invalid color value: {0}")]
72    InvalidColor(String),
73
74    /// 无效的坐标
75    #[error("Invalid coordinates: x={x}, y={y}")]
76    InvalidCoordinates { x: i32, y: i32 },
77
78    /// 无效的尺寸
79    #[error("Invalid size: width={width}, height={height}")]
80    InvalidSize { width: u32, height: u32 },
81
82    /// 绘制操作失败
83    #[error("Draw operation failed: {0}")]
84    OperationFailed(String),
85
86    /// 图片错误
87    #[error(transparent)]
88    ImageError(#[from] ImageError),
89
90    /// 字体错误
91    #[error(transparent)]
92    FontError(#[from] FontError),
93}
94
95/// 库的统一结果类型
96pub type Result<T> = std::result::Result<T, DrawError>;
97
98/// 图片处理结果类型
99pub type ImageResult<T> = std::result::Result<T, ImageError>;
100
101/// 字体处理结果类型
102pub type FontResult<T> = std::result::Result<T, FontError>;