sqlite_collections/ds/map/
error.rs

1use crate::format::Format;
2use std::fmt;
3
4pub enum Error<K, V>
5where
6    K: Format,
7    V: Format,
8{
9    Sqlite(rusqlite::Error),
10    Fmt(std::fmt::Error),
11    KeySerialize(K::SerializeError),
12    KeyDeserialize(K::DeserializeError),
13    ValueSerialize(V::SerializeError),
14    ValueDeserialize(V::DeserializeError),
15}
16
17impl<K, V> fmt::Debug for Error<K, V>
18where
19    K: Format,
20    V: Format,
21{
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            Error::Sqlite(e) => write!(f, "Error::Sqlite({e:?})"),
25            Error::Fmt(e) => write!(f, "Error::Fmt({e:?})"),
26            Error::KeySerialize(e) => write!(f, "Error::KeySerialize({e:?})"),
27            Error::KeyDeserialize(e) => write!(f, "Error::KeyDeserialize({e:?})"),
28            Error::ValueSerialize(e) => write!(f, "Error::ValueSerialize({e:?})"),
29            Error::ValueDeserialize(e) => write!(f, "Error::ValueDeserialize({e:?})"),
30        }
31    }
32}
33
34impl<K, V> From<std::fmt::Error> for Error<K, V>
35where
36    K: Format,
37    V: Format,
38{
39    fn from(v: std::fmt::Error) -> Self {
40        Self::Fmt(v)
41    }
42}
43
44impl<K, V> From<rusqlite::Error> for Error<K, V>
45where
46    K: Format,
47    V: Format,
48{
49    fn from(v: rusqlite::Error) -> Self {
50        Self::Sqlite(v)
51    }
52}
53
54impl<K, V> fmt::Display for Error<K, V>
55where
56    K: Format,
57    V: Format,
58{
59    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
60        match self {
61            Error::Sqlite(e) => write!(f, "sqlite error: {e}"),
62            Error::Fmt(e) => write!(f, "fmt error: {e}"),
63            Error::KeySerialize(e) => write!(f, "key serialize error: {e}"),
64            Error::KeyDeserialize(e) => write!(f, "key deserialize error: {e}"),
65            Error::ValueSerialize(e) => write!(f, "value serialize error: {e}"),
66            Error::ValueDeserialize(e) => write!(f, "value deserialize error: {e}"),
67        }
68    }
69}
70
71impl<K, V> std::error::Error for Error<K, V>
72where
73    K: Format,
74    V: Format,
75{
76}