redb_extras/
error.rs

1//! Crate-scoped error handling for redb-extras.
2//!
3//! This module provides a unified error type for public APIs while maintaining
4//! precise error information for internal utilities.
5
6use std::fmt;
7
8/// Result type alias for convenience
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// Main error type exposed to users of the crate.
12///
13/// This provides a simple interface for facade users while wrapping more specific
14/// internal error types for debugging and advanced usage.
15#[derive(Debug)]
16pub enum Error {
17    /// Errors from the partition layer (generic storage mechanics)
18    Partition(crate::partition::PartitionError),
19
20    /// Errors from the roaring layer (bitmap-specific operations)
21    Roaring(crate::roaring::RoaringError),
22
23    /// Errors from the bucket layer (bucket-specific operations)
24    Bucket(crate::key_buckets::BucketError),
25
26    /// Errors from the database copy utilities
27    DbCopy(crate::dbcopy::DbCopyError),
28
29    /// Invalid input parameters
30    InvalidInput(String),
31
32    /// Transaction-related errors
33    TransactionFailed(String),
34}
35
36impl From<crate::partition::PartitionError> for Error {
37    fn from(err: crate::partition::PartitionError) -> Self {
38        Error::Partition(err)
39    }
40}
41
42impl From<crate::roaring::RoaringError> for Error {
43    fn from(err: crate::roaring::RoaringError) -> Self {
44        Error::Roaring(err)
45    }
46}
47
48impl From<crate::key_buckets::BucketError> for Error {
49    fn from(err: crate::key_buckets::BucketError) -> Self {
50        Error::Bucket(err)
51    }
52}
53
54impl From<crate::dbcopy::DbCopyError> for Error {
55    fn from(err: crate::dbcopy::DbCopyError) -> Self {
56        Error::DbCopy(err)
57    }
58}
59
60impl From<redb::StorageError> for Error {
61    fn from(err: redb::StorageError) -> Self {
62        Error::TransactionFailed(format!("Storage error: {}", err))
63    }
64}
65
66impl std::error::Error for Error {
67    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
68        match self {
69            Error::Partition(err) => err.source(),
70            Error::Roaring(err) => err.source(),
71            Error::Bucket(err) => err.source(),
72            Error::DbCopy(err) => err.source(),
73            Error::InvalidInput(_) => None,
74            Error::TransactionFailed(_) => None,
75        }
76    }
77}
78
79impl fmt::Display for Error {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            Error::Partition(err) => write!(f, "Partition error: {}", err),
83            Error::Roaring(err) => write!(f, "Roaring error: {}", err),
84            Error::Bucket(err) => write!(f, "Bucket error: {}", err),
85            Error::DbCopy(err) => write!(f, "Database copy error: {}", err),
86            Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
87            Error::TransactionFailed(msg) => write!(f, "Transaction failed: {}", msg),
88        }
89    }
90}