rust_zw3d/
error.rs

1//! ZW3D 错误类型定义
2//!
3//! 本模块提供符合 Rust 习惯的错误类型封装。
4
5use std::fmt;
6
7/// ZW3D API 错误
8///
9/// 封装 ZW3D C API 返回的错误码。
10#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub struct ZwError {
12    /// 原始错误码
13    code: i32,
14}
15
16impl ZwError {
17    /// 从原始错误码创建错误
18    #[inline]
19    pub const fn from_code(code: i32) -> Self {
20        Self { code }
21    }
22
23    /// 获取原始错误码
24    #[inline]
25    pub const fn code(&self) -> i32 {
26        self.code
27    }
28
29    /// 判断是否为成功(无错误)
30    #[inline]
31    pub const fn is_success(&self) -> bool {
32        self.code == 0
33    }
34
35    /// 判断是否为用户取消操作
36    #[inline]
37    pub const fn is_user_cancelled(&self) -> bool {
38        // ZW3D 中用户取消通常返回非零值
39        // 具体的取消错误码需要根据实际 API 文档确定
40        self.code != 0
41    }
42}
43
44impl fmt::Display for ZwError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self.code {
47            0 => write!(f, "Success"),
48            -10000 => write!(f, "General error"),
49            -31 => write!(f, "Invalid input"),
50            -32 => write!(f, "Input error"),
51            -33 => write!(f, "Input type error"),
52            _ => write!(f, "ZW3D error code: {}", self.code),
53        }
54    }
55}
56
57impl std::error::Error for ZwError {}
58
59/// ZW3D 操作的结果类型
60pub type ZwResult<T> = Result<T, ZwError>;
61
62// 常用错误码常量
63impl ZwError {
64    /// 无错误(成功)
65    pub const NO_ERROR: Self = Self { code: 0 };
66    
67    /// 通用错误
68    pub const GENERAL_ERROR: Self = Self { code: -10000 };
69    
70    /// 无效输入
71    pub const INVALID_INPUT: Self = Self { code: -31 };
72    
73    /// 输入错误
74    pub const INPUT_ERROR: Self = Self { code: -32 };
75    
76    /// 输入类型错误
77    pub const INPUT_TYPE_ERROR: Self = Self { code: -33 };
78}
79