Skip to main content

dive_store/
error.rs

1//! Store errors.
2
3use std::fmt;
4
5use dive_value::ValueError;
6
7/// An error from state storage.
8///
9/// Apart from [`StoreError::Predecessor`] (native I/O/allocation/corruption) and
10/// [`StoreError::Value`], which carries the value layer's TLC-format error
11/// (a state that cannot be fingerprinted or encoded, as in TLC), every variant is an
12/// internal invariant violation: a bug in the caller or in `dive-store`, reported with
13/// enough context to reproduce it.
14#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum StoreError {
17    /// Native predecessor storage failed; never a model verdict.
18    Predecessor {
19        /// The operation that failed.
20        operation: String,
21        /// Underlying I/O, corruption or allocation detail.
22        message: String,
23    },
24    /// A value could not be fingerprinted, encoded or decoded.
25    Value(ValueError),
26    /// A trace-log parent that is not an earlier state.
27    BadParent {
28        /// The state being recorded.
29        id: u64,
30        /// Its claimed parent.
31        parent: u64,
32    },
33    /// A state id that the trace log does not contain.
34    UnknownState(u64),
35    /// A state fingerprinted with a variable order over another number of
36    /// variables.
37    VarCount {
38        /// The variable order's length.
39        expected: usize,
40        /// The state's number of values.
41        found: usize,
42    },
43}
44
45impl StoreError {
46    /// The stable dive error code (`DIVE-Snnnn`).
47    #[must_use]
48    pub const fn code(&self) -> &'static str {
49        match self {
50            Self::Predecessor { .. } => "DIVE-S0300",
51            Self::Value(_) => "DIVE-S0001",
52            Self::BadParent { .. } => "DIVE-S0101",
53            Self::UnknownState(_) => "DIVE-S0102",
54            Self::VarCount { .. } => "DIVE-S0105",
55        }
56    }
57}
58
59impl fmt::Display for StoreError {
60    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61        match self {
62            Self::Predecessor { operation, message } => {
63                write!(f, "{}: predecessor {operation}: {message}", self.code())
64            }
65            Self::Value(e) => write!(f, "{e}"),
66            Self::BadParent { id, parent } => write!(
67                f,
68                "{}: internal error: state {id} recorded with parent {parent}, which is not an earlier state",
69                self.code()
70            ),
71            Self::UnknownState(id) => write!(
72                f,
73                "{}: internal error: state {id} is not in the trace log",
74                self.code()
75            ),
76            Self::VarCount { expected, found } => write!(
77                f,
78                "{}: internal error: a state of {found} values fingerprinted with a variable order over {expected}",
79                self.code()
80            ),
81        }
82    }
83}
84
85impl std::error::Error for StoreError {}
86
87impl From<ValueError> for StoreError {
88    fn from(e: ValueError) -> Self {
89        Self::Value(e)
90    }
91}