Skip to main content

marsdb_storage/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum StorageError {
5    Io(std::io::Error),
6    Database(redb::DatabaseError),
7    Transaction(Box<redb::TransactionError>),
8    Table(redb::TableError),
9    Storage(redb::StorageError),
10    Commit(redb::CommitError),
11    UnsupportedFormat {
12        found: u64,
13        oldest_supported: u64,
14        current: u64,
15    },
16}
17
18impl fmt::Display for StorageError {
19    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20        match self {
21            StorageError::Io(e) => write!(f, "I/O error: {e}"),
22            StorageError::Database(e) => write!(f, "database error: {e}"),
23            StorageError::Transaction(e) => write!(f, "transaction error: {e}"),
24            StorageError::Table(e) => write!(f, "table error: {e}"),
25            StorageError::Storage(e) => write!(f, "storage error: {e}"),
26            StorageError::Commit(e) => write!(f, "commit error: {e}"),
27            StorageError::UnsupportedFormat {
28                found,
29                oldest_supported,
30                current,
31            } => write!(
32                f,
33                "unsupported database format version {found}; this build supports {oldest_supported}..={current}"
34            ),
35        }
36    }
37}
38
39impl From<std::io::Error> for StorageError {
40    fn from(e: std::io::Error) -> Self {
41        StorageError::Io(e)
42    }
43}
44
45impl std::error::Error for StorageError {}
46
47impl From<redb::DatabaseError> for StorageError {
48    fn from(e: redb::DatabaseError) -> Self {
49        StorageError::Database(e)
50    }
51}
52
53impl From<redb::TransactionError> for StorageError {
54    fn from(e: redb::TransactionError) -> Self {
55        StorageError::Transaction(Box::new(e))
56    }
57}
58
59impl From<redb::TableError> for StorageError {
60    fn from(e: redb::TableError) -> Self {
61        StorageError::Table(e)
62    }
63}
64
65impl From<redb::StorageError> for StorageError {
66    fn from(e: redb::StorageError) -> Self {
67        StorageError::Storage(e)
68    }
69}
70
71impl From<redb::CommitError> for StorageError {
72    fn from(e: redb::CommitError) -> Self {
73        StorageError::Commit(e)
74    }
75}