external_buffered_stream/
error.rs

1use std::sync::PoisonError;
2
3#[derive(Debug)]
4pub enum Error {
5    Unknown,
6
7    #[cfg(feature = "bincode")]
8    EncodeError(bincode::error::EncodeError),
9    #[cfg(feature = "bincode")]
10    DecodeError(bincode::error::DecodeError),
11    #[cfg(feature = "sled")]
12    SledError(sled::Error),
13    #[cfg(feature = "sled")]
14    InvalidSledKeyFormat,
15
16    // Failed to accquire a mutex lock
17    MutexError,
18}
19
20impl core::fmt::Display for Error {
21    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
22        match self {
23            Error::Unknown => write!(f, "Unknown error"),
24
25            #[cfg(feature = "bincode")]
26            Error::EncodeError(e) => write!(f, "Encode error: {}", e),
27            #[cfg(feature = "bincode")]
28            Error::DecodeError(e) => write!(f, "Decode error: {}", e),
29
30            #[cfg(feature = "sled")]
31            Error::SledError(e) => write!(f, "Sled error: {}", e),
32            #[cfg(feature = "sled")]
33            Error::InvalidSledKeyFormat => write!(f, "Invalid key format"),
34
35            Error::MutexError => write!(f, "Failed to acquire mutex lock"),
36        }
37    }
38}
39
40impl std::error::Error for Error {}
41
42#[cfg(feature = "bincode")]
43impl From<bincode::error::EncodeError> for Error {
44    fn from(err: bincode::error::EncodeError) -> Self {
45        Error::EncodeError(err)
46    }
47}
48
49#[cfg(feature = "bincode")]
50impl From<bincode::error::DecodeError> for Error {
51    fn from(err: bincode::error::DecodeError) -> Self {
52        Error::DecodeError(err)
53    }
54}
55
56#[cfg(feature = "sled")]
57impl From<sled::Error> for Error {
58    fn from(err: sled::Error) -> Self {
59        Error::SledError(err)
60    }
61}
62
63impl<T> From<PoisonError<T>> for Error {
64    fn from(_: PoisonError<T>) -> Self {
65        Error::MutexError
66    }
67}