1use std::{error::Error as StdError, fmt};
2
3pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug)]
14pub enum Error {
15 #[cfg(feature = "json")]
16 JsonSerialize(serde_json::error::Error),
18
19 #[cfg(feature = "json")]
20 JsonDeserialize(serde_json::error::Error),
22
23 #[cfg(feature = "cbor")]
24 CborSerialize(serde_cbor::error::Error),
26 #[cfg(feature = "cbor")]
27 CborDeserialize(serde_cbor::error::Error),
29
30 #[cfg(feature = "bincode")]
31 BincodeSerialize(bincode::Error),
33 #[cfg(feature = "bincode")]
34 BincodeDeserialize(bincode::Error),
36
37 Custom(Box<dyn StdError + Send + Sync>),
39 Sled(sled::Error),
41}
42
43impl Error {
44 pub fn custom<E>(error: E) -> Error
46 where
47 E: StdError + Send + Sync + 'static,
48 {
49 Error::Custom(Box::new(error))
50 }
51}
52
53pub(crate) fn coerce<T>(opt: Option<Result<T>>) -> Result<Option<T>> {
54 match opt {
55 Some(Ok(t)) => Ok(Some(t)),
56 Some(Err(e)) => Err(e),
57 None => Ok(None),
58 }
59}
60
61impl From<sled::Error> for Error {
62 fn from(e: sled::Error) -> Self {
63 Error::Sled(e)
64 }
65}
66
67impl fmt::Display for Error {
68 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69 match *self {
70 #[cfg(feature = "json")]
71 Error::JsonSerialize(ref e) => write!(f, "There was an error serializing data, {}", e),
72 #[cfg(feature = "json")]
73 Error::JsonDeserialize(ref e) => {
74 write!(f, "There was an error deserializing data, {}", e)
75 }
76
77 #[cfg(feature = "cbor")]
78 Error::CborSerialize(ref e) => write!(f, "There was an error serializing data, {}", e),
79 #[cfg(feature = "cbor")]
80 Error::CborDeserialize(ref e) => {
81 write!(f, "There was an error deserializing data, {}", e)
82 }
83
84 #[cfg(feature = "bincode")]
85 Error::BincodeSerialize(ref e) => {
86 write!(f, "There was an error serializing data, {}", e)
87 }
88 #[cfg(feature = "bincode")]
89 Error::BincodeDeserialize(ref e) => {
90 write!(f, "There was an error deserializing data, {}", e)
91 }
92
93 Error::Custom(ref e) => write!(f, "There was a custom error, {}", e),
94 Error::Sled(ref e) => write!(f, "There was an error in the database, {}", e),
95 }
96 }
97}
98
99impl StdError for Error {
100 fn description(&self) -> &str {
101 match *self {
102 #[cfg(feature = "json")]
103 Error::JsonSerialize(_) => "There was an error serializing data",
104 #[cfg(feature = "json")]
105 Error::JsonDeserialize(_) => "There was an error deserializing data",
106
107 #[cfg(feature = "cbor")]
108 Error::CborSerialize(_) => "There was an error serializing data",
109 #[cfg(feature = "cbor")]
110 Error::CborDeserialize(_) => "There was an error deserializing data",
111
112 #[cfg(feature = "bincode")]
113 Error::BincodeSerialize(ref e) => e.description(),
114 #[cfg(feature = "bincode")]
115 Error::BincodeDeserialize(ref e) => e.description(),
116
117 Error::Custom(ref e) => e.description(),
118 Error::Sled(ref e) => e.description(),
119 }
120 }
121
122 fn cause(&self) -> Option<&dyn StdError> {
123 match *self {
124 Error::Sled(ref e) => Some(e),
125 Error::Custom(_) => None,
126
127 #[cfg(feature = "bincode")]
128 Error::BincodeSerialize(ref e) | Error::BincodeDeserialize(ref e) => Some(e),
129
130 #[cfg(feature = "json")]
131 Error::JsonSerialize(ref e) | Error::JsonDeserialize(ref e) => Some(e),
132
133 #[cfg(feature = "cbor")]
134 Error::CborSerialize(ref e) | Error::CborDeserialize(ref e) => Some(e),
135 }
136 }
137}