Skip to main content

sheaf_coherence/
error.rs

1use std::fmt;
2
3/// Errors that can arise when building or using cellular sheaves.
4#[derive(Debug)]
5pub enum SheafError {
6    /// A restriction map dimension does not match its target stalk.
7    DimensionMismatch {
8        edge: (usize, usize),
9        expected_rows: usize,
10        expected_cols: usize,
11        got_rows: usize,
12        got_cols: usize,
13    },
14    /// A node index is out of range.
15    InvalidNode(usize),
16    /// An edge references an unknown node.
17    InvalidEdge(usize, usize),
18    /// Belief vector length does not match stalk dimension.
19    BeliefDimensionMismatch {
20        agent: String,
21        expected: usize,
22        got: usize,
23    },
24    /// Empty sheaf (no nodes).
25    EmptySheaf,
26    /// Linear algebra failure.
27    SingularMatrix,
28}
29
30impl fmt::Display for SheafError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            Self::DimensionMismatch { edge, expected_rows, expected_cols, got_rows, got_cols } => {
34                write!(f, "restriction map on edge {:?}: expected {}x{}, got {}x{}", edge, expected_rows, expected_cols, got_rows, got_cols)
35            }
36            Self::InvalidNode(i) => write!(f, "invalid node index {i}"),
37            Self::InvalidEdge(i, j) => write!(f, "invalid edge ({i}, {j})"),
38            Self::BeliefDimensionMismatch { agent, expected, got } => {
39                write!(f, "agent '{agent}': belief vector length {got}, expected {expected}")
40            }
41            Self::EmptySheaf => write!(f, "sheaf has no nodes"),
42            Self::SingularMatrix => write!(f, "singular matrix encountered"),
43        }
44    }
45}
46
47impl std::error::Error for SheafError {}