Skip to main content

gaia_types/errors/
display.rs

1use super::*;
2
3/// 为GaiaError实现标准库Error trait
4///
5/// 这使得GaiaError可以作为标准错误类型使用
6impl Error for GaiaError {}
7
8/// 为GaiaError实现Debug trait
9///
10/// 使用Debug格式输出时,会委托给内部的GaiaErrorKind
11impl Debug for GaiaError {
12    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
13        Debug::fmt(&self.kind, f)
14    }
15}
16
17/// 为GaiaError实现Display trait
18///
19/// 使用Display格式输出时,会委托给内部的GaiaErrorKind
20impl Display for GaiaError {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        Display::fmt(&self.kind, f)
23    }
24}
25
26/// 为GaiaErrorKind实现Display trait
27///
28/// 提供用户友好的错误信息输出格式
29impl Display for GaiaErrorKind {
30    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
31        match self {
32            GaiaErrorKind::InvalidInstruction { instruction, architecture } => {
33                write!(f, "无效指令 '{}' 在架构 '{}'", instruction, architecture)?;
34            }
35            GaiaErrorKind::UnsupportedArchitecture { architecture } => {
36                write!(f, "不支持的架构: {}", architecture)?;
37            }
38            GaiaErrorKind::InvalidRange { length, expect } => {
39                write!(f, "无效范围: 实际长度 {},期望长度 {}", length, expect)?;
40            }
41            GaiaErrorKind::SyntaxError { message, location } => {
42                write!(f, "语法错误在 {:?}: {}", location.url, message)?;
43            }
44            GaiaErrorKind::IoError { io_error, url } => {
45                if let Some(url) = url {
46                    write!(f, "IO错误在 {}: {}", url, io_error)?;
47                }
48                else {
49                    write!(f, "IO错误: {}", io_error)?;
50                }
51            }
52            GaiaErrorKind::StageError { location } => {
53                write!(f, "阶段错误在 {:?}", location)?;
54            }
55            GaiaErrorKind::NotImplemented { feature } => {
56                write!(f, "功能未实现: {}", feature)?;
57            }
58            GaiaErrorKind::CustomError { message } => {
59                write!(f, "自定义错误: {}", message)?;
60            }
61            GaiaErrorKind::AdapterError { adapter_name, message, source } => {
62                write!(f, "适配器错误 [{}]: {}", adapter_name, message)?;
63                if let Some(source) = source {
64                    write!(f, " (源错误: {})", source)?;
65                }
66            }
67            GaiaErrorKind::PlatformUnsupported { platform, operation } => {
68                write!(f, "平台 '{}' 不支持操作: {}", platform, operation)?;
69            }
70            GaiaErrorKind::ConfigError { config_path, message } => {
71                if let Some(path) = config_path {
72                    write!(f, "配置错误在 '{}': {}", path, message)?;
73                }
74                else {
75                    write!(f, "配置错误: {}", message)?;
76                }
77            }
78            GaiaErrorKind::UnsupportedTarget { target } => {
79                write!(f, "不支持的编译目标: {:?}", target)?;
80            }
81            GaiaErrorKind::CompilationFailed { target, message } => {
82                write!(f, "编译失败 (目标: {:?}): {}", target, message)?;
83            }
84            GaiaErrorKind::SaveError { format, message } => {
85                write!(f, "保存错误 (格式: {}): {}", format, message)?;
86            }
87            GaiaErrorKind::UnsupportedFeature { feature, location } => {
88                write!(f, "不支持的功能 '{}' 在位置 {:?}", feature, location)?;
89            }
90            GaiaErrorKind::UnreachableError { location } => {
91                write!(f, "不可达错误: {}", location)?;
92            }
93        }
94        Ok(())
95    }
96}