use std::fmt;
use dive_value::ValueError;
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum StoreError {
Predecessor {
operation: String,
message: String,
},
Value(ValueError),
BadParent {
id: u64,
parent: u64,
},
UnknownState(u64),
VarCount {
expected: usize,
found: usize,
},
}
impl StoreError {
#[must_use]
pub const fn code(&self) -> &'static str {
match self {
Self::Predecessor { .. } => "DIVE-S0300",
Self::Value(_) => "DIVE-S0001",
Self::BadParent { .. } => "DIVE-S0101",
Self::UnknownState(_) => "DIVE-S0102",
Self::VarCount { .. } => "DIVE-S0105",
}
}
}
impl fmt::Display for StoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Predecessor { operation, message } => {
write!(f, "{}: predecessor {operation}: {message}", self.code())
}
Self::Value(e) => write!(f, "{e}"),
Self::BadParent { id, parent } => write!(
f,
"{}: internal error: state {id} recorded with parent {parent}, which is not an earlier state",
self.code()
),
Self::UnknownState(id) => write!(
f,
"{}: internal error: state {id} is not in the trace log",
self.code()
),
Self::VarCount { expected, found } => write!(
f,
"{}: internal error: a state of {found} values fingerprinted with a variable order over {expected}",
self.code()
),
}
}
}
impl std::error::Error for StoreError {}
impl From<ValueError> for StoreError {
fn from(e: ValueError) -> Self {
Self::Value(e)
}
}