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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::db::{SetVersionError, SetupError};
use crate::format::Format;
use std::fmt;

#[derive(Debug)]
pub enum OpenError {
    Sqlite(rusqlite::Error),
    Fmt(std::fmt::Error),
    ApplicationId(i32),
    TableVersion(i64),
    UserVersion(i32),
    TableType { expected: String, actual: String },
}

impl From<std::fmt::Error> for OpenError {
    fn from(v: std::fmt::Error) -> Self {
        Self::Fmt(v)
    }
}

impl From<rusqlite::Error> for OpenError {
    fn from(v: rusqlite::Error) -> Self {
        Self::Sqlite(v)
    }
}

impl From<SetupError> for OpenError {
    fn from(v: SetupError) -> Self {
        match v {
            SetupError::Sqlite(e) => OpenError::Sqlite(e),
            SetupError::ApplicationId(e) => OpenError::ApplicationId(e),
            SetupError::UserVersion(e) => OpenError::UserVersion(e),
            SetupError::TableType { expected, actual } => OpenError::TableType { expected, actual },
        }
    }
}
impl From<SetVersionError> for OpenError {
    fn from(v: SetVersionError) -> Self {
        match v {
            SetVersionError::Sqlite(e) => OpenError::Sqlite(e),
        }
    }
}

impl fmt::Display for OpenError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OpenError::Sqlite(e) => write!(f, "sqlite error: {e}"),
            OpenError::Fmt(e) => write!(f, "fmt error: {e}"),
            OpenError::ApplicationId(id) => write!(f, "wrong application id: {id}"),
            OpenError::UserVersion(version) => write!(f, "illegal user version: {version}"),
            OpenError::TableVersion(version) => write!(f, "illegal table version: {version}"),
            OpenError::TableType { expected, actual } => {
                write!(
                    f,
                    "table was wrong type: expected={expected} actual={actual}"
                )
            }
        }
    }
}

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

pub enum Error<K, V>
where
    K: Format,
    V: Format,
{
    Sqlite(rusqlite::Error),
    Fmt(std::fmt::Error),
    KeySerialize(K::SerializeError),
    KeyDeserialize(K::DeserializeError),
    ValueSerialize(V::SerializeError),
    ValueDeserialize(V::DeserializeError),
}

impl<K, V> fmt::Debug for Error<K, V>
where
    K: Format,
    V: Format,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Sqlite(e) => write!(f, "Error::Sqlite({e:?})"),
            Error::Fmt(e) => write!(f, "Error::Fmt({e:?})"),
            Error::KeySerialize(e) => write!(f, "Error::KeySerialize({e:?})"),
            Error::KeyDeserialize(e) => write!(f, "Error::KeyDeserialize({e:?})"),
            Error::ValueSerialize(e) => write!(f, "Error::ValueSerialize({e:?})"),
            Error::ValueDeserialize(e) => write!(f, "Error::ValueDeserialize({e:?})"),
        }
    }
}

impl<K, V> From<std::fmt::Error> for Error<K, V>
where
    K: Format,
    V: Format,
{
    fn from(v: std::fmt::Error) -> Self {
        Self::Fmt(v)
    }
}

impl<K, V> From<rusqlite::Error> for Error<K, V>
where
    K: Format,
    V: Format,
{
    fn from(v: rusqlite::Error) -> Self {
        Self::Sqlite(v)
    }
}

impl<K, V> fmt::Display for Error<K, V>
where
    K: Format,
    V: Format,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::Sqlite(e) => write!(f, "sqlite error: {e}"),
            Error::Fmt(e) => write!(f, "fmt error: {e}"),
            Error::KeySerialize(e) => write!(f, "key serialize error: {e}"),
            Error::KeyDeserialize(e) => write!(f, "key deserialize error: {e}"),
            Error::ValueSerialize(e) => write!(f, "value serialize error: {e}"),
            Error::ValueDeserialize(e) => write!(f, "value deserialize error: {e}"),
        }
    }
}

impl<K, V> std::error::Error for Error<K, V>
where
    K: Format,
    V: Format,
{
}