Skip to main content

oxgraph_algo/bfs/
error.rs

1//! Error types shared by indexed BFS implementations.
2
3use core::fmt;
4
5/// Error returned when caller-provided BFS scratch cannot support traversal.
6///
7/// # Performance
8///
9/// `perf: unspecified`; errors are returned only before traversal starts.
10#[derive(Clone, Copy, Debug, Eq, PartialEq)]
11pub enum BfsError {
12    /// The start element is not valid and visible in the topology view.
13    StartElementNotContained,
14    /// The visited scratch slice is smaller than `graph.element_bound()`.
15    VisitedTooSmall {
16        /// Required visited entries.
17        needed: usize,
18        /// Provided visited entries.
19        actual: usize,
20    },
21    /// The queue scratch slice is smaller than `graph.element_bound()`.
22    QueueTooSmall {
23        /// Required queue entries.
24        needed: usize,
25        /// Provided queue entries.
26        actual: usize,
27    },
28    /// The start element maps outside `graph.element_bound()`.
29    StartIndexOutOfBounds {
30        /// Dense index returned for the start element.
31        index: usize,
32        /// Exclusive element index bound for the topology view.
33        bound: usize,
34    },
35    /// Traversal observed a successor or predecessor element whose dense index
36    /// is at or past `graph.element_bound()`. Indicates the topology view
37    /// violated its
38    /// [`ElementSuccessors`](oxgraph_topology::ElementSuccessors) or
39    /// [`ElementPredecessors`](oxgraph_topology::ElementPredecessors)
40    /// contract: an expanded element ID must map below the element bound that
41    /// was in effect when traversal started.
42    NeighborIndexOutOfBounds {
43        /// Dense index returned for the offending element.
44        index: usize,
45        /// Exclusive element index bound that was in effect for this traversal.
46        bound: usize,
47    },
48}
49
50impl fmt::Display for BfsError {
51    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
52        match self {
53            Self::StartElementNotContained => {
54                formatter.write_str("start element is not contained in the topology view")
55            }
56            Self::VisitedTooSmall { needed, actual } => write!(
57                formatter,
58                "visited scratch is too small: needed {needed}, got {actual}"
59            ),
60            Self::QueueTooSmall { needed, actual } => write!(
61                formatter,
62                "queue scratch is too small: needed {needed}, got {actual}"
63            ),
64            Self::StartIndexOutOfBounds { index, bound } => write!(
65                formatter,
66                "start element index {index} is outside element index bound {bound}"
67            ),
68            Self::NeighborIndexOutOfBounds { index, bound } => write!(
69                formatter,
70                "expanded element index {index} is outside element index bound {bound}"
71            ),
72        }
73    }
74}
75
76impl core::error::Error for BfsError {}