plonkish-cat 0.2.0

PLONKish circuit system built on comp-cat-rs: circuits as morphisms in a free category
Documentation
//! Project-wide error type.

use comp_cat_rs::collapse::free_category::FreeCategoryError;

/// All errors that can arise in plonkish-cat.
#[derive(Debug)]
pub enum Error {
    /// An error from the free category infrastructure.
    FreeCategory(FreeCategoryError),
    /// Wire index out of bounds during allocation or constraint generation.
    WireOutOfBounds {
        /// The wire index that was accessed.
        wire_index: usize,
        /// The total number of allocated wires.
        allocated: usize,
    },
    /// Gate applied to a vertex with wrong wire count.
    WireCountMismatch {
        /// The expected wire count.
        expected: usize,
        /// The actual wire count.
        actual: usize,
    },
}

impl core::fmt::Display for Error {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::FreeCategory(e) => write!(f, "free category error: {e}"),
            Self::WireOutOfBounds {
                wire_index,
                allocated,
            } => write!(
                f,
                "wire index {wire_index} out of bounds (allocated: {allocated})"
            ),
            Self::WireCountMismatch { expected, actual } => {
                write!(f, "wire count mismatch: expected {expected}, got {actual}")
            }
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::FreeCategory(e) => Some(e),
            Self::WireOutOfBounds { .. } | Self::WireCountMismatch { .. } => None,
        }
    }
}

impl From<FreeCategoryError> for Error {
    fn from(e: FreeCategoryError) -> Self {
        Self::FreeCategory(e)
    }
}