1use std::fmt;
4
5use dive_value::ValueError;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
15#[non_exhaustive]
16pub enum StoreError {
17 Predecessor {
19 operation: String,
21 message: String,
23 },
24 Value(ValueError),
26 BadParent {
28 id: u64,
30 parent: u64,
32 },
33 UnknownState(u64),
35 VarCount {
38 expected: usize,
40 found: usize,
42 },
43}
44
45impl StoreError {
46 #[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}