dijkstra_suite/error.rs
1//! customized library-wide error handling
2//!
3//! ## The`DijkstraError` type
4//!
5//! The `DijkstraError` enum is a centralized error type, every library
6//! component which may return an error uses [`DijkstraError`]
7//!
8//! This type implements the [`Error`] trait manually, so to avoid introducing
9//! a dependency
10//!
11//! ```rust
12//! use dijkstra_suite::error::DijkstraError;
13//! use dijkstra_suite::compute::DistanceFromSource;
14//!
15//! let mut distances = DistanceFromSource::default();
16//!
17//! distances.set_distance((0, 0), 0, None);
18//! let node_error = DijkstraError::ComputePathError("end node not found".into());
19//!
20//! let result = distances.compute_path((0, 2));
21//!
22//! assert_eq!(result, Err(node_error));
23//! ```
24
25use std::{error::Error, fmt::Display};
26
27/// centralized error type
28///
29/// ## The `DijkstraError` error type
30///
31/// This enum is a centralized error type used throughout the library
32/// It implements the [`Error`] trait manually so to avoid an external
33/// dependency
34///
35/// ## Example
36///
37/// Main function [`dijkstra_path()`](crate::dijkstra::dijkstra_path)
38/// as well as internals like [`DistanceFromSource::compute_path()`](crate::compute::DistanceFromSource::compute_path)
39/// uses `DijkstraError`:
40///
41/// ```rust
42/// use dijkstra_suite::error::DijkstraError;
43/// use dijkstra_suite::compute::DistanceFromSource;
44///
45/// let mut distances = DistanceFromSource::default();
46///
47/// distances.set_distance((0, 0), 0, None);
48/// let node_error = DijkstraError::ComputePathError("end node not found".into());
49///
50/// let result = distances.compute_path((0, 2));
51///
52/// assert_eq!(result, Err(node_error));
53/// ```
54#[non_exhaustive]
55#[derive(Debug, PartialEq)]
56pub enum DijkstraError {
57 /// error while generating the generic zero
58 GenericZeroError(String),
59 DistanceEntryNotFound(String),
60 ComputePathError(String),
61 GraphNodeNotFound(String),
62 StrategyError(String),
63}
64
65impl Display for DijkstraError {
66 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67 match self {
68 DijkstraError::GenericZeroError(e) => {
69 write!(f, "error retrieving generic zero: {}", e)
70 }
71 DijkstraError::DistanceEntryNotFound(e) => {
72 write!(f, "entry not found in current distances list. error: {}", e)
73 }
74 DijkstraError::ComputePathError(e) => {
75 write!(f, "an error occurred computing the path: {}", e)
76 }
77 DijkstraError::GraphNodeNotFound(e) => {
78 write!(f, "node not found in graph: {}", e)
79 }
80 DijkstraError::StrategyError(e) => {
81 write!(f, "StrategyError: {}", e)
82 }
83 }
84 }
85}
86
87impl Error for DijkstraError {
88 fn source(&self) -> Option<&(dyn Error + 'static)> {
89 match self {
90 // this forwards an eventual Error type property of variants, for eaxmple
91 // if variant would be DijkstraError::ComputePathError(std::io::Error)
92 // DijkstraError::ComputePathError(e) => Some(e),
93 _ => None,
94 }
95 }
96}