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!(
65                "Inv1 violated: energy {energy:.6} ≠ initial energy {energy_0:.6}"
66            ),
67            action,
68            before: None,
69            after: None,
70        }
71    }
72
73    pub fn inv2(action: Action, residual: f64, prev_res: f64, eps: f64) -> Self {
74        InvViolation {
75            invariant: 2,
76            message: format!(
77                "Inv2 violated: residual {:.6} > prevRes + ε = {:.6} + {:.6} = {:.6}",
78                residual,
79                prev_res,
80                eps,
81                prev_res + eps
82            ),
83            action,
84            before: None,
85            after: None,
86        }
87    }
88
89    pub fn inv3(action: Action, reason: impl Into<String>) -> Self {
90        InvViolation {
91            invariant: 3,
92            message: format!("Inv3 violated: GraphOK failed — {}", reason.into()),
93            action,
94            before: None,
95            after: None,
96        }
97    }
98
99    pub fn inv4(action: Action, reason: impl Into<String>) -> Self {
100        InvViolation {
101            invariant: 4,
102            message: format!("Inv4 violated: TypeOK failed — {}", reason.into()),
103            action,
104            before: None,
105            after: None,
106        }
107    }
108}