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::buckets::BucketError),
25
26    /// Invalid input parameters
27    InvalidInput(String),
28
29    /// Transaction-related errors
30    TransactionFailed(String),
31}
32
33impl From<crate::partition::PartitionError> for Error {
34    fn from(err: crate::partition::PartitionError) -> Self {
35        Error::Partition(err)
36    }
37}
38
39impl From<crate::roaring::RoaringError> for Error {
40    fn from(err: crate::roaring::RoaringError) -> Self {
41        Error::Roaring(err)
42    }
43}
44
45impl From<crate::buckets::BucketError> for Error {
46    fn from(err: crate::buckets::BucketError) -> Self {
47        Error::Bucket(err)
48    }
49}
50
51impl From<redb::StorageError> for Error {
52    fn from(err: redb::StorageError) -> Self {
53        Error::TransactionFailed(format!("Storage error: {}", err))
54    }
55}
56
57impl std::error::Error for Error {
58    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
59        match self {
60            Error::Partition(err) => err.source(),
61            Error::Roaring(err) => err.source(),
62            Error::Bucket(err) => err.source(),
63            Error::InvalidInput(_) => None,
64            Error::TransactionFailed(_) => None,
65        }
66    }
67}
68
69impl fmt::Display for Error {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        match self {
72            Error::Partition(err) => write!(f, "Partition error: {}", err),
73            Error::Roaring(err) => write!(f, "Roaring error: {}", err),
74            Error::Bucket(err) => write!(f, "Bucket error: {}", err),
75            Error::InvalidInput(msg) => write!(f, "Invalid input: {}", msg),
76            Error::TransactionFailed(msg) => write!(f, "Transaction failed: {}", msg),
77        }
78    }
79}