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