Skip to main content

aria_engine_core/
error.rs

1use thiserror::Error;
2
3use crate::action::Action;
4
5/// Structured invariant violation.
6#[derive(Debug, Clone, Error)]
7pub enum AriaError {
8    /// An invariant was violated during apply.
9    #[error("invariant violation: {0}")]
10    InvariantViolation(InvViolation),
11
12    /// A backend operation failed.
13    #[error("backend error: {0}")]
14    Backend(String),
15
16    /// Configuration error.
17    #[error("config error: {0}")]
18    Config(String),
19
20    /// I/O error.
21    #[error("io error: {0}")]
22    Io(String),
23
24    /// Invalid action sequence.
25    #[error("invalid schedule: {0}")]
26    Schedule(String),
27}
28
29/// Which invariant was violated, with before/after snapshots.
30#[derive(Debug, Clone)]
31pub struct InvViolation {
32    /// Which invariant (1-4)
33    pub invariant: u8,
34    /// Human-readable message
35    pub message: String,
36    /// The action that triggered the violation
37    pub action: Action,
38    /// State before the action (if available)
39    pub before: Option<String>,
40    /// State after the action (if available)
41    pub after: Option<String>,
42}
43
44impl std::fmt::Display for InvViolation {
45    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46        write!(f, "{}", self.message)
47    }
48}
49
50impl InvViolation {
51    pub fn new(invariant: u8, message: impl Into<String>, action: Action) -> Self {
52        InvViolation {
53            invariant,
54            message: message.into(),
55            action,
56            before: None,
57            after: None,
58        }
59    }
60
61    pub fn inv1(action: Action, energy: f64, energy_0: f64) -> Self {
62        InvViolation {
63            invariant: 1,
64            message: format!("Inv1 violated: energy {energy:.6} ≠ initial energy {energy_0:.6}"),
65            action,
66            before: None,
67            after: None,
68        }
69    }
70
71    pub fn inv2(action: Action, residual: f64, prev_res: f64, eps: f64) -> Self {
72        InvViolation {
73            invariant: 2,
74            message: format!(
75                "Inv2 violated: residual {:.6} > prevRes + ε = {:.6} + {:.6} = {:.6}",
76                residual,
77                prev_res,
78                eps,
79                prev_res + eps
80            ),
81            action,
82            before: None,
83            after: None,
84        }
85    }
86
87    pub fn inv3(action: Action, reason: impl Into<String>) -> Self {
88        InvViolation {
89            invariant: 3,
90            message: format!("Inv3 violated: GraphOK failed — {}", reason.into()),
91            action,
92            before: None,
93            after: None,
94        }
95    }
96
97    pub fn inv4(action: Action, reason: impl Into<String>) -> Self {
98        InvViolation {
99            invariant: 4,
100            message: format!("Inv4 violated: TypeOK failed — {}", reason.into()),
101            action,
102            before: None,
103            after: None,
104        }
105    }
106}