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