Skip to main content

plonkish_cat/
error.rs

1//! Project-wide error type.
2
3use comp_cat_rs::collapse::free_category::FreeCategoryError;
4
5/// All errors that can arise in plonkish-cat.
6#[derive(Debug)]
7pub enum Error {
8    /// An error from the free category infrastructure.
9    FreeCategory(FreeCategoryError),
10    /// Wire index out of bounds during allocation or constraint generation.
11    WireOutOfBounds {
12        /// The wire index that was accessed.
13        wire_index: usize,
14        /// The total number of allocated wires.
15        allocated: usize,
16    },
17    /// Gate applied to a vertex with wrong wire count.
18    WireCountMismatch {
19        /// The expected wire count.
20        expected: usize,
21        /// The actual wire count.
22        actual: usize,
23    },
24    /// Attempted to invert zero in the field.
25    DivisionByZero,
26}
27
28impl core::fmt::Display for Error {
29    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30        match self {
31            Self::FreeCategory(e) => write!(f, "free category error: {e}"),
32            Self::WireOutOfBounds {
33                wire_index,
34                allocated,
35            } => write!(
36                f,
37                "wire index {wire_index} out of bounds (allocated: {allocated})"
38            ),
39            Self::WireCountMismatch { expected, actual } => {
40                write!(f, "wire count mismatch: expected {expected}, got {actual}")
41            }
42            Self::DivisionByZero => write!(f, "division by zero"),
43        }
44    }
45}
46
47impl std::error::Error for Error {
48    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
49        match self {
50            Self::FreeCategory(e) => Some(e),
51            Self::WireOutOfBounds { .. }
52            | Self::WireCountMismatch { .. }
53            | Self::DivisionByZero => None,
54        }
55    }
56}
57
58impl From<FreeCategoryError> for Error {
59    fn from(e: FreeCategoryError) -> Self {
60        Self::FreeCategory(e)
61    }
62}