Skip to main content

route_engine_rs/
errors.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3
4#[derive(Debug, Clone, Eq, PartialEq)]
5pub enum GraphError {
6    GraphInvariantBroken {
7        node_count: usize,
8        adjacency_count: usize,
9    },
10    FromNodeOutOfBounds {
11        index: usize,
12        node_count: usize,
13    },
14    ToNodeOutOfBounds {
15        index: usize,
16        node_count: usize,
17    },
18}
19
20impl Display for GraphError {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Self::GraphInvariantBroken {
24                node_count,
25                adjacency_count,
26            } => write!(
27                f,
28                "graph invariant broken: nodes={}, adjacency_lists={}",
29                node_count, adjacency_count
30            ),
31            Self::FromNodeOutOfBounds { index, node_count } => {
32                write!(f, "from node out of bounds: {} >= {}", index, node_count)
33            }
34            Self::ToNodeOutOfBounds { index, node_count } => {
35                write!(f, "to node out of bounds: {} >= {}", index, node_count)
36            }
37        }
38    }
39}
40
41impl Error for GraphError {}
42
43#[derive(Debug, Clone, Eq, PartialEq)]
44pub enum DijkstraError {
45    SourceNodeOutOfBounds {
46        index: usize,
47        node_count: usize,
48    },
49    TargetNodeOutOfBounds {
50        index: usize,
51        node_count: usize,
52    },
53}
54
55impl Display for DijkstraError {
56    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
57        match self {
58            Self::SourceNodeOutOfBounds { index, node_count } => {
59                write!(f, "source node out of bounds: {} >= {}", index, node_count)
60            }
61            Self::TargetNodeOutOfBounds { index, node_count } => {
62                write!(f, "target node out of bounds: {} >= {}", index, node_count)
63            }
64        }
65    }
66}
67
68impl Error for DijkstraError {}