1use std::error::Error;
2use std::fmt::Display;
3use std::io;
4use std::sync::PoisonError;
5
6pub type DbResult<T> = Result<T, DbError>;
7
8#[derive(Debug)]
9pub enum DbError {
10 Unexpected(&'static str),
11 Io(io::Error),
12 Bincode(bincode::Error),
13 MutexPoisoned,
14}
15
16impl From<bincode::Error> for DbError {
17 fn from(err: bincode::Error) -> Self {
18 Self::Bincode(err)
19 }
20}
21
22impl From<io::Error> for DbError {
23 fn from(err: io::Error) -> Self {
24 Self::Io(err)
25 }
26}
27
28impl<G> From<PoisonError<G>> for DbError {
29 fn from(_: PoisonError<G>) -> Self {
30 Self::MutexPoisoned
31 }
32}
33
34impl Display for DbError {
35 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36 match self {
37 DbError::Unexpected(u) => write!(f, "unexpected error: {u}"),
38 DbError::Io(i) => write!(f, "io error: {i}"),
39 DbError::Bincode(b) => write!(f, "bincode error: {b}"),
40 DbError::MutexPoisoned => write!(f, "mutex poisoned"),
41 }
42 }
43}
44
45impl Error for DbError {
46 fn source(&self) -> Option<&(dyn Error + 'static)> {
47 match self {
48 DbError::Io(e) => Some(e),
49 DbError::Bincode(e) => Some(e),
50 DbError::MutexPoisoned => None,
51 DbError::Unexpected(_) => None,
52 }
53 }
54}