use core::fmt;
pub type Result<T, E = Error> = core::result::Result<T, E>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
WriteConflict {
table: &'static str,
},
SerializationFailure,
DuplicateKey {
table: &'static str,
index: &'static str,
},
Aborted,
TableNotRegistered {
table: &'static str,
},
PrimaryKeyChanged {
table: &'static str,
},
}
impl Error {
pub fn is_retriable(&self) -> bool {
matches!(
self,
Error::WriteConflict { .. } | Error::SerializationFailure
)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::WriteConflict { table } => {
write!(f, "write-write conflict on table `{table}`")
}
Error::SerializationFailure => {
f.write_str("could not serialize access due to read/write dependencies")
}
Error::DuplicateKey { table, index } => {
write!(f, "duplicate key on `{table}`.`{index}`")
}
Error::Aborted => f.write_str("transaction is aborted"),
Error::TableNotRegistered { table } => {
write!(
f,
"`{table}` was not registered: call `db.register::<{table}>()` at startup"
)
}
Error::PrimaryKeyChanged { table } => write!(
f,
"an update may not change the primary key of `{table}`; delete and insert instead"
),
}
}
}
impl std::error::Error for Error {}