1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use std::fmt::{Display, Formatter};
use std::sync::PoisonError;
use std::{io, panic};

#[derive(Debug)]
pub enum Error {
    Corrupted(String),
    TableTypeMismatch(String),
    DbSizeMismatch {
        path: String,
        size: usize,
        requested_size: usize,
    },
    DoesNotExist(String),
    LeakedWriteTransaction(&'static panic::Location<'static>),
    // Tables cannot be opened for writing multiple times, since they could retrieve immutable &
    // mutable references to the same dirty pages, or multiple mutable references via insert_reserve()
    TableAlreadyOpen(&'static panic::Location<'static>),
    OutOfSpace,
    Io(io::Error),
    LockPoisoned(&'static panic::Location<'static>),
}

impl<T> From<PoisonError<T>> for Error {
    fn from(_: PoisonError<T>) -> Error {
        Error::LockPoisoned(panic::Location::caller())
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Error {
        Error::Io(err)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Corrupted(msg) => {
                write!(f, "DB corrupted: {}", msg)
            }
            Error::TableTypeMismatch(msg) => {
                write!(f, "{}", msg)
            }
            Error::DbSizeMismatch {
                path,
                size,
                requested_size,
            } => {
                write!(
                    f,
                    "Database {} is of size {} bytes, but you requested {} bytes",
                    path, size, requested_size
                )
            }
            Error::DoesNotExist(msg) => {
                write!(f, "{}", msg)
            }
            Error::LeakedWriteTransaction(location) => {
                write!(f, "Leaked write transaction: {}", location)
            }
            Error::TableAlreadyOpen(location) => {
                write!(f, "Table already opened at: {}", location)
            }
            Error::OutOfSpace => {
                write!(f, "Database is out of space")
            }
            Error::Io(err) => {
                write!(f, "I/O error: {}", err)
            }
            Error::LockPoisoned(location) => {
                write!(f, "Poisoned internal lock: {}", location)
            }
        }
    }
}

impl std::error::Error for Error {}