Skip to main content

tritium_core/
error.rs

1//! Error type for the foundation crate. Hand-rolled (no `thiserror`) to keep
2//! `tritium-core` dependency-free and `no_std`.
3
4/// Errors from ternary type construction and reference math.
5#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6#[non_exhaustive]
7pub enum TritError {
8    /// A value outside `{-1, 0, +1}` was offered to [`crate::Trit`].
9    OutOfRange(i32),
10    /// Operand/output buffer lengths disagree with the [`crate::GemmShape`].
11    ShapeMismatch {
12        /// What the shape implies.
13        expected: usize,
14        /// What the buffer actually has.
15        got: usize,
16    },
17}
18
19impl core::fmt::Display for TritError {
20    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
21        match self {
22            TritError::OutOfRange(v) => {
23                write!(f, "value {v} out of ternary range {{-1, 0, 1}}")
24            }
25            TritError::ShapeMismatch { expected, got } => {
26                write!(f, "buffer length mismatch: expected {expected}, got {got}")
27            }
28        }
29    }
30}
31
32// `core::error::Error` is stable since Rust 1.81 (MSRV here is 1.89), so the
33// Error impl is unconditional — no `std` feature gate.
34impl core::error::Error for TritError {}