Skip to main content

supercode_reduce/
error.rs

1//! Errors raised by reversible projection, verification, and rehydration.
2
3use std::fmt;
4
5/// A reduction invariant failed or a reversible pointer could not be resolved.
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub struct ReductionError {
8    message: String,
9}
10
11impl ReductionError {
12    /// Build an error with a stable, user-facing explanation.
13    pub fn new(message: impl Into<String>) -> Self {
14        Self {
15            message: message.into(),
16        }
17    }
18
19    /// Return the invariant failure explanation.
20    pub fn message(&self) -> &str {
21        &self.message
22    }
23}
24
25impl fmt::Display for ReductionError {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        formatter.write_str(&self.message)
28    }
29}
30
31impl std::error::Error for ReductionError {}
32
33/// Result type for standalone reduction operations.
34pub type Result<T> = std::result::Result<T, ReductionError>;
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn display_is_the_stable_invariant_explanation() {
42        let error = ReductionError::new("sidecar hash mismatch");
43        assert_eq!(error.message(), "sidecar hash mismatch");
44        assert_eq!(error.to_string(), "sidecar hash mismatch");
45    }
46}