Skip to main content

rumba_core/utils/
error.rs

1//! The error type returned by the MBA solver.
2
3use crate::expr::{Expr, VarId};
4
5/// Why the solver could not simplify an expression.
6///
7/// Simplifying an MBA is best-effort: a caller that hands over an expression the
8/// solver cannot handle should be able to keep its original expression and carry
9/// on, not die. Every variant is a "leave this one alone" signal rather than a
10/// reason to abort the program.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub enum SolveError {
13    /// The linear MBA still held more variables (`found`) than the solver's
14    /// internal limit (`max`) after reduction / PCT expansion, so its truth
15    /// table is too large to build.
16    TooManyVariables {
17        /// Number of variables the expression carried after reduction.
18        found: usize,
19        /// The solver's internal variable limit.
20        max: usize,
21    },
22
23    /// A variable produced during reduction had no entry in the restore map.
24    /// Indicates an inconsistent variable map rather than a hard input.
25    UnknownVariable(VarId),
26
27    /// A solved linear MBA came back in a shape the polynomial reconstruction
28    /// does not model.
29    UnrecognizedForm(Expr),
30}
31
32impl std::fmt::Display for SolveError {
33    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34        match self {
35            SolveError::TooManyVariables { found, max } => {
36                write!(f, "too many variables: {found} (max {max})")
37            }
38            SolveError::UnknownVariable(v) => write!(f, "unknown variable v{v}"),
39            SolveError::UnrecognizedForm(e) => {
40                write!(f, "solved linear MBA is in an unrecognized form: {e}")
41            }
42        }
43    }
44}
45
46impl std::error::Error for SolveError {}