Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/kleene

crates/kleene-sparse/src/error.rs

use std::fmt;

/// Why a sparse object could not be built or an operation could not run.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SparseError {
    /// Offsets that do not start at 0, fall, or do not end at the number of entries.
    BadOffsets {
        /// What was being built.
        what: &'static str,
        /// The rule the offsets break.
        why: String,
    },
    /// An index at or past the dimension it indexes.
    IndexOutOfRange {
        /// What was being built.
        what: &'static str,
        /// The index.
        index: u64,
        /// The dimension.
        dim: u64,
    },
    /// Indices of a vector that are not strictly rising.
    NotSorted {
        /// What was being built.
        what: &'static str,
    },
    /// Two operands whose shapes do not fit.
    Shape {
        /// The operation.
        op: &'static str,
        /// The shapes, as text.
        detail: String,
    },
    /// Lists that must be equally long and are not.
    Length {
        /// What was being built.
        what: &'static str,
        /// The length wanted.
        want: usize,
        /// The length given.
        got: usize,
    },
}

impl fmt::Display for SparseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SparseError::BadOffsets { what, why } => write!(f, "{what}: offsets {why}"),
            SparseError::IndexOutOfRange { what, index, dim } => {
                write!(f, "{what}: index {index} is outside dimension {dim}")
            }
            SparseError::NotSorted { what } => write!(f, "{what}: indices are not strictly rising"),
            SparseError::Shape { op, detail } => write!(f, "{op}: shapes do not fit ({detail})"),
            SparseError::Length { what, want, got } => {
                write!(f, "{what}: {got} values, {want} wanted")
            }
        }
    }
}

impl std::error::Error for SparseError {}