use bincode::error::DecodeError as BincodeDecodeError;
use bincode::error::EncodeError as BincodeEncodeError;
use std::io::Error as IoError;
use std::sync::PoisonError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("Transaction is closed")]
TxClosed,
#[error("Transaction is not writable")]
TxNotWritable,
#[error("Key being inserted already exists")]
KeyAlreadyExists,
#[error("Value being checked was not correct")]
ValNotExpectedValue,
#[error("Read conflict, retry the transaction")]
KeyReadConflict,
#[error("Write conflict, retry the transaction")]
KeyWriteConflict,
#[error("Can not fetch value at a future version")]
VersionInFuture,
#[error("No savepoint has been set")]
NoSavepoint,
#[error("Transaction is not persistent")]
TxCommitNotPersisted(PersistenceError),
}
#[derive(Error, Debug)]
pub enum PersistenceError {
#[error("IO error: {0}")]
Io(#[from] IoError),
#[error("Serialization error: {0}")]
Serialization(#[from] BincodeEncodeError),
#[error("Deserialization error: {0}")]
Deserialization(#[from] BincodeDecodeError),
#[error("Lock acquisition failed")]
LockFailed(String),
#[error("Snapshot creation failed: {0}")]
SnapshotFailed(String),
#[error("AOL append failed: {0}")]
AppendFailed(String),
}
impl<T> From<PoisonError<std::sync::MutexGuard<'_, T>>> for PersistenceError {
fn from(error: PoisonError<std::sync::MutexGuard<'_, T>>) -> Self {
PersistenceError::LockFailed(error.to_string())
}
}