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
use {
    gluesql_core::error::{AlterTableError, Error, IndexError},
    sled::transaction::TransactionError as SledTransactionError,
    std::{str, time},
    thiserror::Error as ThisError,
};

#[derive(ThisError, Debug)]
pub enum StorageError {
    #[error(transparent)]
    AlterTable(#[from] AlterTableError),
    #[error(transparent)]
    Index(#[from] IndexError),

    #[error(transparent)]
    Sled(#[from] sled::Error),
    #[error(transparent)]
    Bincode(#[from] bincode::Error),
    #[error(transparent)]
    Str(#[from] str::Utf8Error),
    #[error(transparent)]
    SystemTime(#[from] time::SystemTimeError),
    #[error(transparent)]
    TryFromSlice(#[from] std::array::TryFromSliceError),
}

impl From<StorageError> for Error {
    fn from(e: StorageError) -> Error {
        use StorageError::*;

        match e {
            Sled(e) => Error::StorageMsg(e.to_string()),
            Bincode(e) => Error::StorageMsg(e.to_string()),
            Str(e) => Error::StorageMsg(e.to_string()),
            SystemTime(e) => Error::StorageMsg(e.to_string()),
            TryFromSlice(e) => Error::StorageMsg(e.to_string()),
            AlterTable(e) => e.into(),
            Index(e) => e.into(),
        }
    }
}

pub fn err_into<E>(e: E) -> Error
where
    E: Into<StorageError>,
{
    let e: StorageError = e.into();
    let e: Error = e.into();

    e
}

pub fn tx_err_into(e: SledTransactionError<Error>) -> Error {
    match e {
        SledTransactionError::Abort(e) => e,
        SledTransactionError::Storage(e) => StorageError::Sled(e).into(),
    }
}