tokitai-operator 0.1.0

Verified DL kernel compiler: formally-checked GEMM, p-adic, sheaf, contract-carrying ops. Paper-artifact grade.
Documentation
//! The `Error` and `Result` types shared by the whole crate.
//!
//! `Error` is a flat enum with 6 variants: `Domain`, `Shape`,
//! `Operator`, `Ir`, `Backend`, `Verification`. Each variant carries
//! a single `String` message. The fail-closed guarantee is
//! implemented at the call sites: any obligation violation or backend
//! error returns an `Err(Error::...)` rather than silently producing
//! wrong output.
//!
//! The companion `Result<T>` is `std::result::Result<T, Error>`.
//!
//! P440: removed the `Contract` and `Planner` variants. They had
//! zero production sites across the crate (only the round-trip
//! tests in `tests/error_variants.rs` exercised them); the real
//! obligation and planner violations use the call sites'
//! category-specific variants (`Operator`, `Ir`, `Backend`).
//!
//! See `examples/fail_closed_audit.rs` (P383) and
//! `examples/book/07_error_handling.rs` for the demonstrative uses.
//!
use std::fmt;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    Domain(String),
    Shape(String),
    Operator(String),
    Ir(String),
    Backend(String),
    Verification(String),
}

impl Error {
    pub fn domain(message: impl Into<String>) -> Self {
        Self::Domain(message.into())
    }

    pub fn shape(message: impl Into<String>) -> Self {
        Self::Shape(message.into())
    }

    pub fn operator(message: impl Into<String>) -> Self {
        Self::Operator(message.into())
    }

    pub fn ir(message: impl Into<String>) -> Self {
        Self::Ir(message.into())
    }

    pub fn backend(message: impl Into<String>) -> Self {
        Self::Backend(message.into())
    }

    pub fn verification(message: impl Into<String>) -> Self {
        Self::Verification(message.into())
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Domain(message) => write!(f, "domain error: {message}"),
            Error::Shape(message) => write!(f, "shape error: {message}"),
            Error::Operator(message) => write!(f, "operator error: {message}"),
            Error::Ir(message) => write!(f, "ir error: {message}"),
            Error::Backend(message) => write!(f, "backend error: {message}"),
            Error::Verification(message) => {
                write!(f, "verification error: {message}")
            }
        }
    }
}

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