generated from habedi/template-rust-project
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexceptions.rs
521 lines (445 loc) · 14 KB
/
exceptions.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/*!
# Custom Error Types
This module defines the custom error types for Graphina. These exceptions are used
throughout the library to show various failure conditions and provide specific
error information. Each exception implements the standard [`Error`](std::error::Error)
and [`Display`](std::fmt::Display) traits.
## Usage
Create an exception using the `new` method and inspect it via its display implementation:
```rust
use graphina::core::exceptions::GraphinaException;
let err = GraphinaException::new("A generic error occurred.");
println!("{}", err); // Prints: GraphinaException: A generic error occurred.
```
*/
use std::error::Error;
use std::fmt;
/// Base exception for Graphina.
///
/// This error type is used as a general exception that can be used for non-specific error cases.
#[derive(Debug)]
pub struct GraphinaException {
/// Detailed error message.
pub message: String,
}
impl GraphinaException {
/// Creates a new `GraphinaException` with the specified message.
///
/// # Examples
///
/// ```rust
/// use graphina::core::exceptions::GraphinaException;
/// let err = GraphinaException::new("Something went wrong.");
/// assert_eq!(format!("{}", err), "GraphinaException: Something went wrong.");
/// ```
pub fn new(message: &str) -> Self {
GraphinaException {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaException {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaException: {}", self.message)
}
}
impl Error for GraphinaException {}
/// Exception for serious errors.
///
/// This error is intended for critical issues that may require immediate attention.
#[derive(Debug)]
pub struct GraphinaError {
/// Detailed error message.
pub message: String,
}
impl GraphinaError {
/// Creates a new `GraphinaError` with the given message.
pub fn new(message: &str) -> Self {
GraphinaError {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaError: {}", self.message)
}
}
impl Error for GraphinaError {}
/// Exception raised when a graph is provided to an algorithm that cannot use it.
///
/// This error indicates that an algorithm received an invalid graph input like a null/empty graph.
#[derive(Debug)]
pub struct GraphinaPointlessConcept {
/// Detailed error message.
pub message: String,
}
impl GraphinaPointlessConcept {
/// Creates a new `GraphinaPointlessConcept` error.
pub fn new(message: &str) -> Self {
GraphinaPointlessConcept {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaPointlessConcept {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaPointlessConcept: {}", self.message)
}
}
impl Error for GraphinaPointlessConcept {}
/// Exception for unexpected termination of algorithms.
///
/// This error is used when an algorithm terminates unexpectedly.
#[derive(Debug)]
pub struct GraphinaAlgorithmError {
/// Detailed error message.
pub message: String,
}
impl GraphinaAlgorithmError {
/// Creates a new `GraphinaAlgorithmError` with the provided message.
pub fn new(message: &str) -> Self {
GraphinaAlgorithmError {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaAlgorithmError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaAlgorithmError: {}", self.message)
}
}
impl Error for GraphinaAlgorithmError {}
/// Exception raised when no feasible solution exists.
///
/// This error indicates that an algorithm failed to find a viable solution (e.g., optimization).
#[derive(Debug)]
pub struct GraphinaUnfeasible {
/// Detailed error message.
pub message: String,
}
impl GraphinaUnfeasible {
/// Creates a new `GraphinaUnfeasible` error.
pub fn new(message: &str) -> Self {
GraphinaUnfeasible {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaUnfeasible {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaUnfeasible: {}", self.message)
}
}
impl Error for GraphinaUnfeasible {}
/// Exception raised when no path exists between nodes.
///
/// This error is returned when an algorithm determines that no valid path can be found.
#[derive(Debug)]
pub struct GraphinaNoPath {
/// Detailed error message.
pub message: String,
}
impl GraphinaNoPath {
/// Creates a new `GraphinaNoPath` error.
pub fn new(message: &str) -> Self {
GraphinaNoPath {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaNoPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaNoPath: {}", self.message)
}
}
impl Error for GraphinaNoPath {}
/// Exception raised when no cycle exists in a graph.
///
/// This error is used when an algorithm expects a cycle but none is found in the graph.
#[derive(Debug)]
pub struct GraphinaNoCycle {
/// Detailed error message.
pub message: String,
}
impl GraphinaNoCycle {
/// Creates a new `GraphinaNoCycle` error.
pub fn new(message: &str) -> Self {
GraphinaNoCycle {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaNoCycle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaNoCycle: {}", self.message)
}
}
impl Error for GraphinaNoCycle {}
/// Exception raised if a requested node is not found.
///
/// This error is typically returned when an operation attempts to reference a non-existent node.
#[derive(Debug)]
pub struct NodeNotFound {
/// Detailed error message.
pub message: String,
}
impl NodeNotFound {
/// Creates a new `NodeNotFound` error.
pub fn new(message: &str) -> Self {
NodeNotFound {
message: message.to_string(),
}
}
}
impl fmt::Display for NodeNotFound {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "NodeNotFound: {}", self.message)
}
}
impl Error for NodeNotFound {}
/// Exception raised if a graph has a cycle when an acyclic structure is expected.
///
/// This error indicates that a cycle was found in a graph where it should not exist.
#[derive(Debug)]
pub struct HasACycle {
/// Detailed error message.
pub message: String,
}
impl HasACycle {
/// Creates a new `HasACycle` error.
pub fn new(message: &str) -> Self {
HasACycle {
message: message.to_string(),
}
}
}
impl fmt::Display for HasACycle {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "HasACycle: {}", self.message)
}
}
impl Error for HasACycle {}
/// Exception raised when an optimization problem is unbounded.
///
/// This error is used when an algorithm detects that the solution is unbounded (e.g., linear programming).
#[derive(Debug)]
pub struct GraphinaUnbounded {
/// Detailed error message.
pub message: String,
}
impl GraphinaUnbounded {
/// Creates a new `GraphinaUnbounded` error.
pub fn new(message: &str) -> Self {
GraphinaUnbounded {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaUnbounded {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaUnbounded: {}", self.message)
}
}
impl Error for GraphinaUnbounded {}
/// Exception raised for unimplemented algorithms for a given graph type.
///
/// This error indicates that a requested algorithm or feature is not yet available.
#[derive(Debug)]
pub struct GraphinaNotImplemented {
/// Detailed error message.
pub message: String,
}
impl GraphinaNotImplemented {
/// Creates a new `GraphinaNotImplemented` error.
pub fn new(message: &str) -> Self {
GraphinaNotImplemented {
message: message.to_string(),
}
}
}
impl fmt::Display for GraphinaNotImplemented {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "GraphinaNotImplemented: {}", self.message)
}
}
impl Error for GraphinaNotImplemented {}
/// Raised when more than one valid solution exists for an intermediary step.
///
/// This error is used when an algorithm encounters ambiguity during a computational step (e.g., optimization).
#[derive(Debug)]
pub struct AmbiguousSolution {
/// Detailed error message.
pub message: String,
}
impl AmbiguousSolution {
/// Creates a new `AmbiguousSolution` error.
pub fn new(message: &str) -> Self {
AmbiguousSolution {
message: message.to_string(),
}
}
}
impl fmt::Display for AmbiguousSolution {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "AmbiguousSolution: {}", self.message)
}
}
impl Error for AmbiguousSolution {}
/// Raised if a loop iterates too many times without convergence.
///
/// This error signals that an iterative algorithm has exceeded the allowed iteration limit.
#[derive(Debug)]
pub struct ExceededMaxIterations {
/// Detailed error message.
pub message: String,
}
impl ExceededMaxIterations {
/// Creates a new `ExceededMaxIterations` error.
pub fn new(message: &str) -> Self {
ExceededMaxIterations {
message: message.to_string(),
}
}
}
impl fmt::Display for ExceededMaxIterations {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ExceededMaxIterations: {}", self.message)
}
}
impl Error for ExceededMaxIterations {}
/// Raised when the power iteration method fails to converge within the iteration limit (e.g., PageRank).
///
/// This error includes the number of iterations attempted before failure.
#[derive(Debug)]
pub struct PowerIterationFailedConvergence {
/// The number of iterations performed.
pub num_iterations: usize,
/// Detailed error message.
pub message: String,
}
impl PowerIterationFailedConvergence {
/// Creates a new `PowerIterationFailedConvergence` error.
///
/// # Examples
///
/// ```rust
/// use graphina::core::exceptions::PowerIterationFailedConvergence;
/// let err = PowerIterationFailedConvergence::new(100, "Convergence not reached.");
/// assert_eq!(format!("{}", err), "PowerIterationFailedConvergence after 100 iterations: Convergence not reached.");
/// ```
pub fn new(num_iterations: usize, message: &str) -> Self {
PowerIterationFailedConvergence {
num_iterations,
message: message.to_string(),
}
}
}
impl fmt::Display for PowerIterationFailedConvergence {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"PowerIterationFailedConvergence after {} iterations: {}",
self.num_iterations, self.message
)
}
}
impl Error for PowerIterationFailedConvergence {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_graphina_exception_display() {
let err = GraphinaException::new("Generic error");
assert_eq!(format!("{}", err), "GraphinaException: Generic error");
}
#[test]
fn test_graphina_error_display() {
let err = GraphinaError::new("Serious error");
assert_eq!(format!("{}", err), "GraphinaError: Serious error");
}
#[test]
fn test_pointless_concept_display() {
let err = GraphinaPointlessConcept::new("Null graph provided");
assert_eq!(
format!("{}", err),
"GraphinaPointlessConcept: Null graph provided"
);
}
#[test]
fn test_algorithm_error_display() {
let err = GraphinaAlgorithmError::new("Unexpected termination");
assert_eq!(
format!("{}", err),
"GraphinaAlgorithmError: Unexpected termination"
);
}
#[test]
fn test_unfeasible_display() {
let err = GraphinaUnfeasible::new("No feasible solution");
assert_eq!(
format!("{}", err),
"GraphinaUnfeasible: No feasible solution"
);
}
#[test]
fn test_no_path_display() {
let err = GraphinaNoPath::new("No path exists");
assert_eq!(format!("{}", err), "GraphinaNoPath: No path exists");
}
#[test]
fn test_no_cycle_display() {
let err = GraphinaNoCycle::new("No cycle found");
assert_eq!(format!("{}", err), "GraphinaNoCycle: No cycle found");
}
#[test]
fn test_node_not_found_display() {
let err = NodeNotFound::new("Node missing");
assert_eq!(format!("{}", err), "NodeNotFound: Node missing");
}
#[test]
fn test_has_a_cycle_display() {
let err = HasACycle::new("Cycle detected");
assert_eq!(format!("{}", err), "HasACycle: Cycle detected");
}
#[test]
fn test_unbounded_display() {
let err = GraphinaUnbounded::new("Optimization unbounded");
assert_eq!(
format!("{}", err),
"GraphinaUnbounded: Optimization unbounded"
);
}
#[test]
fn test_not_implemented_display() {
let err = GraphinaNotImplemented::new("Feature not available");
assert_eq!(
format!("{}", err),
"GraphinaNotImplemented: Feature not available"
);
}
#[test]
fn test_ambiguous_solution_display() {
let err = AmbiguousSolution::new("Multiple solutions exist");
assert_eq!(
format!("{}", err),
"AmbiguousSolution: Multiple solutions exist"
);
}
#[test]
fn test_exceeded_max_iterations_display() {
let err = ExceededMaxIterations::new("Iteration limit exceeded");
assert_eq!(
format!("{}", err),
"ExceededMaxIterations: Iteration limit exceeded"
);
}
#[test]
fn test_power_iteration_failed_convergence_display() {
let err = PowerIterationFailedConvergence::new(150, "Failed to converge");
assert_eq!(
format!("{}", err),
"PowerIterationFailedConvergence after 150 iterations: Failed to converge"
);
}
}