Skip to main content

rpg_types/
errors.rs

1use std::io;
2use thiserror::Error;
3
4/// 统一的结果类型
5pub type Result<T> = std::result::Result<T, RpgError>;
6
7/// RPG 错误类型
8#[derive(Debug, Error)]
9pub enum RpgError {
10    /// IO 错误
11    #[error("IO error: {0}")]
12    Io(#[from] io::Error),
13    /// 编码错误
14    #[error("Encode error: {message} (format: {format})")]
15    Encode {
16        /// 错误消息
17        message: String,
18        /// 数据格式
19        format: &'static str,
20    },
21    /// 解码错误
22    #[error("Decode error: {message} (format: {format})")]
23    Decode {
24        /// 错误消息
25        message: String,
26        /// 数据格式
27        format: &'static str,
28    },
29}
30
31impl RpgError {
32    /// 创建一个新的解码错误
33    pub fn decode(format: &'static str, message: &str) -> Self {
34        Self::Decode { message: message.to_string(), format }
35    }
36
37    /// 创建一个新的编码错误
38    pub fn encode(format: &'static str, message: &str) -> Self {
39        Self::Encode { message: message.to_string(), format }
40    }
41}