1use core::fmt;
6use core::fmt::{Display, Formatter};
7
8pub type GraphResult<T> = Result<T, GraphError>;
10
11#[derive(Debug, Clone, PartialEq)]
13pub enum GraphError {
14 NodeNotFound {
16 index: usize,
18 },
19
20 NodeDeleted {
22 index: usize,
24 provided: u32,
26 current: u32,
28 },
29
30 EdgeNotFound {
32 index: usize,
34 },
35
36 EdgeDeleted {
38 index: usize,
40 provided: u32,
42 current: u32,
44 },
45
46 EdgeAlreadyExists {
48 from: usize,
50 to: usize,
52 },
53
54 SelfLoopNotAllowed {
56 node: usize,
58 },
59
60 GraphCapacityExceeded {
62 current: usize,
64 max: usize,
66 },
67
68 MemoryAllocationFailed {
70 requested: usize,
72 },
73
74 InvalidDirection,
76
77 GraphNotDirected,
79
80 GraphNotUndirected,
82
83 GraphHasCycle,
85
86 AlgorithmNotConverged {
88 iterations: usize,
90 error: f64,
92 },
93
94 NegativeCycle,
96
97 NegativeWeight {
99 from: usize,
101 to: usize,
103 weight: f64,
105 },
106
107 IndexOutOfBounds {
109 index: usize,
111 bound: usize,
113 },
114
115 NotFound(String),
117
118 InvalidFormat(String),
120
121 IoError(String),
123}
124
125impl Display for GraphError {
126 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
127 match self {
128 GraphError::NodeNotFound { index } => {
129 write!(f, "节点不存在:索引 {} 无效或超出范围", index)
130 }
131 GraphError::NodeDeleted {
132 index,
133 provided,
134 current,
135 } => {
136 write!(
137 f,
138 "节点已删除:索引 {} 的 generation 不匹配 (provided: {}, current: {})",
139 index, provided, current
140 )
141 }
142 GraphError::EdgeNotFound { index } => {
143 write!(f, "边不存在:索引 {} 无效或超出范围", index)
144 }
145 GraphError::EdgeDeleted {
146 index,
147 provided,
148 current,
149 } => {
150 write!(
151 f,
152 "边已删除:索引 {} 的 generation 不匹配 (provided: {}, current: {})",
153 index, provided, current
154 )
155 }
156 GraphError::EdgeAlreadyExists { from, to } => {
157 write!(f, "边已存在:从节点 {} 到节点 {} 的边已存在", from, to)
158 }
159 GraphError::SelfLoopNotAllowed { node } => {
160 write!(f, "不允许自环:节点 {} 不能作为自身的边端点", node)
161 }
162 GraphError::GraphCapacityExceeded { current, max } => {
163 write!(f, "超过最大容量:当前 {},最大 {}", current, max)
164 }
165 GraphError::MemoryAllocationFailed { requested } => {
166 write!(f, "内存分配失败:请求 {} 字节", requested)
167 }
168 GraphError::InvalidDirection => {
169 write!(f, "无效的图方向操作")
170 }
171 GraphError::GraphNotDirected => {
172 write!(f, "图不是有向图")
173 }
174 GraphError::GraphNotUndirected => {
175 write!(f, "图不是无向图")
176 }
177 GraphError::GraphHasCycle => {
178 write!(f, "图中存在环")
179 }
180 GraphError::AlgorithmNotConverged { iterations, error } => {
181 write!(
182 f,
183 "算法不收敛:执行 {} 次迭代后误差为 {}",
184 iterations, error
185 )
186 }
187 GraphError::NegativeCycle => {
188 write!(f, "检测到负权环")
189 }
190 GraphError::NegativeWeight { from, to, weight } => {
191 write!(
192 f,
193 "检测到负权重边:节点 {} 到 {} 的权重为 {}(Dijkstra 不支持负权重,建议使用 Bellman-Ford 算法)",
194 from, to, weight
195 )
196 }
197 GraphError::IndexOutOfBounds { index, bound } => {
198 write!(f, "索引越界:索引 {} 超出边界 {}", index, bound)
199 }
200 GraphError::NotFound(resource) => {
201 write!(f, "未找到资源:{}", resource)
202 }
203 GraphError::InvalidFormat(msg) => {
204 write!(f, "无效格式:{}", msg)
205 }
206 GraphError::IoError(err) => {
207 write!(f, "IO 错误:{}", err)
208 }
209 }
210 }
211}
212
213#[cfg(feature = "std")]
214impl std::error::Error for GraphError {}
215
216impl From<GraphError> for String {
217 fn from(err: GraphError) -> Self {
218 err.to_string()
219 }
220}
221
222#[cfg(test)]
223mod tests {
224 use super::*;
225
226 #[test]
227 fn test_error_display() {
228 let err = GraphError::NodeNotFound { index: 42 };
229 assert!(err.to_string().contains("42"));
230
231 let err = GraphError::EdgeAlreadyExists { from: 1, to: 2 };
232 assert!(err.to_string().contains("1"));
233 assert!(err.to_string().contains("2"));
234 }
235}