#[cfg(feature = "std")]
pub use std::error::Error;
#[cfg(not(feature = "std"))]
pub trait Error: core::fmt::Display + core::fmt::Debug {}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum TransactionError<C: Error, P: Error> {
#[cfg_attr(feature = "std", error("transaction is read-only"))]
ReadOnly,
#[cfg_attr(feature = "std", error("transaction conflict, please retry"))]
Conflict,
#[cfg_attr(
feature = "std",
error("transaction has been discarded, please create a new one")
)]
Discard,
#[cfg_attr(feature = "std", error("transaction is too large"))]
LargeTxn,
#[cfg_attr(feature = "std", error("transaction manager error: {0}"))]
Pwm(P),
#[cfg_attr(feature = "std", error("conflict manager error: {0}"))]
Cm(C),
}
#[cfg(not(feature = "std"))]
impl<C: Error, P: Error> core::fmt::Display for TransactionError<C, P> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::ReadOnly => write!(f, "transaction is read-only"),
Self::Conflict => write!(f, "transaction conflict, please retry"),
Self::Discard => write!(f, "transaction has been discarded, please create a new one"),
Self::LargeTxn => write!(f, "transaction is too large"),
Self::Pwm(e) => write!(f, "transaction manager error: {}", e),
Self::Cm(e) => write!(f, "conflict manager error: {}", e),
}
}
}
impl<C: Error, P: Error> TransactionError<C, P> {
#[inline]
pub const fn conflict(err: C) -> Self {
Self::Cm(err)
}
#[inline]
pub const fn pending(err: P) -> Self {
Self::Pwm(err)
}
}
pub enum WtmError<C: Error, P: Error, E: Error> {
Transaction(TransactionError<C, P>),
Commit(E),
}
impl<C: Error, P: Error, E: Error> core::fmt::Debug for WtmError<C, P, E> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Transaction(e) => write!(f, "Transaction({:?})", e),
Self::Commit(e) => write!(f, "Commit({:?})", e),
}
}
}
impl<C: Error, P: Error, E: Error> core::fmt::Display for WtmError<C, P, E> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Transaction(e) => write!(f, "transaction error: {e}"),
Self::Commit(e) => write!(f, "commit error: {e}"),
}
}
}
impl<C: Error, P: Error, E: Error> Error for WtmError<C, P, E> {}
impl<C: Error, P: Error, E: Error> From<TransactionError<C, P>> for WtmError<C, P, E> {
#[inline]
fn from(err: TransactionError<C, P>) -> Self {
Self::Transaction(err)
}
}
impl<C: Error, P: Error, E: Error> WtmError<C, P, E> {
#[inline]
pub const fn transaction(err: TransactionError<C, P>) -> Self {
Self::Transaction(err)
}
#[inline]
pub const fn commit(err: E) -> Self {
Self::Commit(err)
}
}