Skip to main content

kanban_core/graph/
error.rs

1use thiserror::Error;
2
3/// Errors that can arise from graph mutations.
4///
5/// Implementations of [`super::Graph`] choose which of these can occur:
6/// a DAG rejects `Cycle`, `SelfReference`, and `Duplicate`; an
7/// undirected graph rejects `SelfReference` and `Duplicate` (treated
8/// as duplicate in either ordering). `EdgeNotFound` is universal.
9#[derive(Error, Debug, PartialEq, Eq, Clone)]
10pub enum GraphError {
11    #[error("operation would create a cycle")]
12    Cycle,
13    #[error("self-reference is not allowed")]
14    SelfReference,
15    #[error("edge not found")]
16    EdgeNotFound,
17    /// An active edge with the same endpoints already exists.
18    /// Directed graphs reject the same `source -> target` ordering;
19    /// undirected graphs reject either `{a, b}` ordering. Archived
20    /// edges don't count against this check, so re-adding after
21    /// archive succeeds.
22    #[error("an active edge with the same endpoints already exists")]
23    Duplicate,
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    #[test]
31    fn test_graph_error_cycle_displays_expected_message() {
32        assert_eq!(
33            GraphError::Cycle.to_string(),
34            "operation would create a cycle"
35        );
36    }
37
38    #[test]
39    fn test_graph_error_self_reference_displays_expected_message() {
40        assert_eq!(
41            GraphError::SelfReference.to_string(),
42            "self-reference is not allowed"
43        );
44    }
45
46    #[test]
47    fn test_graph_error_edge_not_found_displays_expected_message() {
48        assert_eq!(GraphError::EdgeNotFound.to_string(), "edge not found");
49    }
50
51    #[test]
52    fn test_graph_error_supports_equality() {
53        assert_eq!(GraphError::Cycle, GraphError::Cycle);
54        assert_ne!(GraphError::Cycle, GraphError::SelfReference);
55    }
56}