Skip to main content

sim_lib_discrete_graph/
error.rs

1//! Error type for discrete graph algorithms.
2
3use sim_lib_discrete_algebra::AlgebraError;
4
5/// Errors raised by graph construction, algorithms, and verifiers.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7pub enum GraphError {
8    /// The graph is disconnected, so the requested result does not exist.
9    #[error("graph is disconnected")]
10    Disconnected,
11    /// A negative-weight cycle makes shortest paths undefined.
12    #[error("graph has a negative-weight cycle")]
13    NegativeCycle,
14    /// A negative edge weight was supplied to an algorithm that forbids it.
15    #[error("negative edge weight is not allowed here")]
16    NegativeWeight,
17    /// The algorithm was called on the wrong kind of graph.
18    #[error("wrong graph kind: {0}")]
19    WrongGraphKind(String),
20    /// An edge referenced a node index outside the node range.
21    #[error("invalid endpoint on edge {edge}: node {node} >= node count {len}")]
22    InvalidEndpoint {
23        /// The offending edge id.
24        edge: usize,
25        /// The out-of-range node index.
26        node: usize,
27        /// The number of nodes.
28        len: usize,
29    },
30    /// A node index passed to an algorithm was outside the node range.
31    #[error("node {node} out of range: node count {count}")]
32    NodeOutOfRange {
33        /// The out-of-range node index.
34        node: usize,
35        /// The number of nodes.
36        count: usize,
37    },
38    /// A submitted certificate failed verification.
39    #[error("certificate invalid: {0}")]
40    CertificateInvalid(String),
41    /// A staged feature is not yet implemented.
42    #[error("unsupported: {0}")]
43    Unsupported(String),
44}
45
46impl From<AlgebraError> for GraphError {
47    fn from(err: AlgebraError) -> Self {
48        match err {
49            // For a min-plus adjacency matrix, a divergent closure star means a
50            // negative-weight cycle.
51            AlgebraError::NoStar => GraphError::NegativeCycle,
52            other => GraphError::Unsupported(other.to_string()),
53        }
54    }
55}