Skip to main content

tokitai_operator/
error.rs

1//! The `Error` and `Result` types shared by the whole crate.
2//!
3//! `Error` is a flat enum with 6 variants: `Domain`, `Shape`,
4//! `Operator`, `Ir`, `Backend`, `Verification`. Each variant carries
5//! a single `String` message. The fail-closed guarantee is
6//! implemented at the call sites: any obligation violation or backend
7//! error returns an `Err(Error::...)` rather than silently producing
8//! wrong output.
9//!
10//! The companion `Result<T>` is `std::result::Result<T, Error>`.
11//!
12//! P440: removed the `Contract` and `Planner` variants. They had
13//! zero production sites across the crate (only the round-trip
14//! tests in `tests/error_variants.rs` exercised them); the real
15//! obligation and planner violations use the call sites'
16//! category-specific variants (`Operator`, `Ir`, `Backend`).
17//!
18//! See `examples/fail_closed_audit.rs` (P383) and
19//! `examples/book/07_error_handling.rs` for the demonstrative uses.
20//!
21use std::fmt;
22
23pub type Result<T> = std::result::Result<T, Error>;
24
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub enum Error {
27    Domain(String),
28    Shape(String),
29    Operator(String),
30    Ir(String),
31    Backend(String),
32    Verification(String),
33}
34
35impl Error {
36    pub fn domain(message: impl Into<String>) -> Self {
37        Self::Domain(message.into())
38    }
39
40    pub fn shape(message: impl Into<String>) -> Self {
41        Self::Shape(message.into())
42    }
43
44    pub fn operator(message: impl Into<String>) -> Self {
45        Self::Operator(message.into())
46    }
47
48    pub fn ir(message: impl Into<String>) -> Self {
49        Self::Ir(message.into())
50    }
51
52    pub fn backend(message: impl Into<String>) -> Self {
53        Self::Backend(message.into())
54    }
55
56    pub fn verification(message: impl Into<String>) -> Self {
57        Self::Verification(message.into())
58    }
59}
60
61impl fmt::Display for Error {
62    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
63        match self {
64            Error::Domain(message) => write!(f, "domain error: {message}"),
65            Error::Shape(message) => write!(f, "shape error: {message}"),
66            Error::Operator(message) => write!(f, "operator error: {message}"),
67            Error::Ir(message) => write!(f, "ir error: {message}"),
68            Error::Backend(message) => write!(f, "backend error: {message}"),
69            Error::Verification(message) => {
70                write!(f, "verification error: {message}")
71            }
72        }
73    }
74}
75
76impl std::error::Error for Error {}