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}
25
26impl core::fmt::Display for Error {
27    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
28        match self {
29            Self::FreeCategory(e) => write!(f, "free category error: {e}"),
30            Self::WireOutOfBounds {
31                wire_index,
32                allocated,
33            } => write!(
34                f,
35                "wire index {wire_index} out of bounds (allocated: {allocated})"
36            ),
37            Self::WireCountMismatch { expected, actual } => {
38                write!(f, "wire count mismatch: expected {expected}, got {actual}")
39            }
40        }
41    }
42}
43
44impl std::error::Error for Error {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            Self::FreeCategory(e) => Some(e),
48            Self::WireOutOfBounds { .. } | Self::WireCountMismatch { .. } => None,
49        }
50    }
51}
52
53impl From<FreeCategoryError> for Error {
54    fn from(e: FreeCategoryError) -> Self {
55        Self::FreeCategory(e)
56    }
57}