use std::fmt;
#[derive(Debug)]
pub enum KVStoreError {
DatabaseError(redb::DatabaseError),
TransactionError(redb::TransactionError),
TableError(redb::TableError),
CommitError(redb::CommitError),
SerializationError(postcard::Error),
StorageError(redb::StorageError),
Task(tokio::task::JoinError),
}
impl fmt::Display for KVStoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
KVStoreError::DatabaseError(e) => write!(f, "Database error: {}", e),
KVStoreError::TransactionError(e) => write!(f, "Transaction error: {}", e),
KVStoreError::TableError(e) => write!(f, "Table error: {}", e),
KVStoreError::CommitError(e) => write!(f, "CommitError error: {}", e),
KVStoreError::SerializationError(e) => write!(f, "Serialization error: {}", e),
KVStoreError::StorageError(e) => write!(f, "Storage error: {}", e),
KVStoreError::Task(e) => write!(f, "Internal task join error: {}", e),
}
}
}
impl std::error::Error for KVStoreError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
KVStoreError::DatabaseError(e) => Some(e),
KVStoreError::TransactionError(e) => Some(e),
KVStoreError::TableError(e) => Some(e),
KVStoreError::CommitError(e) => Some(e),
KVStoreError::SerializationError(e) => Some(e),
KVStoreError::StorageError(e) => Some(e),
KVStoreError::Task(e) => Some(e),
}
}
}
impl From<redb::DatabaseError> for KVStoreError {
fn from(e: redb::DatabaseError) -> Self {
KVStoreError::DatabaseError(e)
}
}
impl From<redb::TransactionError> for KVStoreError {
fn from(e: redb::TransactionError) -> Self {
KVStoreError::TransactionError(e)
}
}
impl From<redb::TableError> for KVStoreError {
fn from(e: redb::TableError) -> Self {
KVStoreError::TableError(e)
}
}
impl From<redb::CommitError> for KVStoreError {
fn from(e: redb::CommitError) -> Self {
KVStoreError::CommitError(e)
}
}
impl From<postcard::Error> for KVStoreError {
fn from(e: postcard::Error) -> Self {
KVStoreError::SerializationError(e)
}
}
impl From<redb::StorageError> for KVStoreError {
fn from(e: redb::StorageError) -> Self {
KVStoreError::StorageError(e)
}
}
impl From<tokio::task::JoinError> for KVStoreError {
fn from(e: tokio::task::JoinError) -> Self {
KVStoreError::Task(e)
}
}
pub type Result<T> = std::result::Result<T, KVStoreError>;