use std::{error::Error as StdError, fmt};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
pub enum Error {
#[cfg(feature = "json")]
JsonSerialize(serde_json::error::Error),
#[cfg(feature = "json")]
JsonDeserialize(serde_json::error::Error),
#[cfg(feature = "cbor")]
CborSerialize(serde_cbor::error::Error),
#[cfg(feature = "cbor")]
CborDeserialize(serde_cbor::error::Error),
#[cfg(feature = "bincode")]
BincodeSerialize(bincode::Error),
#[cfg(feature = "bincode")]
BincodeDeserialize(bincode::Error),
Custom(Box<dyn StdError + Send + Sync>),
Sled(sled::Error),
}
impl Error {
pub fn custom<E>(error: E) -> Error
where
E: StdError + Send + Sync + 'static,
{
Error::Custom(Box::new(error))
}
}
pub(crate) fn coerce<T>(opt: Option<Result<T>>) -> Result<Option<T>> {
match opt {
Some(Ok(t)) => Ok(Some(t)),
Some(Err(e)) => Err(e),
None => Ok(None),
}
}
impl From<sled::Error> for Error {
fn from(e: sled::Error) -> Self {
Error::Sled(e)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
#[cfg(feature = "json")]
Error::JsonSerialize(ref e) => write!(f, "There was an error serializing data, {}", e),
#[cfg(feature = "json")]
Error::JsonDeserialize(ref e) => {
write!(f, "There was an error deserializing data, {}", e)
}
#[cfg(feature = "cbor")]
Error::CborSerialize(ref e) => write!(f, "There was an error serializing data, {}", e),
#[cfg(feature = "cbor")]
Error::CborDeserialize(ref e) => {
write!(f, "There was an error deserializing data, {}", e)
}
#[cfg(feature = "bincode")]
Error::BincodeSerialize(ref e) => {
write!(f, "There was an error serializing data, {}", e)
}
#[cfg(feature = "bincode")]
Error::BincodeDeserialize(ref e) => {
write!(f, "There was an error deserializing data, {}", e)
}
Error::Custom(ref e) => write!(f, "There was a custom error, {}", e),
Error::Sled(ref e) => write!(f, "There was an error in the database, {}", e),
}
}
}
impl StdError for Error {
fn description(&self) -> &str {
match *self {
#[cfg(feature = "json")]
Error::JsonSerialize(_) => "There was an error serializing data",
#[cfg(feature = "json")]
Error::JsonDeserialize(_) => "There was an error deserializing data",
#[cfg(feature = "cbor")]
Error::CborSerialize(_) => "There was an error serializing data",
#[cfg(feature = "cbor")]
Error::CborDeserialize(_) => "There was an error deserializing data",
#[cfg(feature = "bincode")]
Error::BincodeSerialize(ref e) => e.description(),
#[cfg(feature = "bincode")]
Error::BincodeDeserialize(ref e) => e.description(),
Error::Custom(ref e) => e.description(),
Error::Sled(ref e) => e.description(),
}
}
fn cause(&self) -> Option<&dyn StdError> {
match *self {
Error::Sled(ref e) => Some(e),
Error::Custom(_) => None,
#[cfg(feature = "bincode")]
Error::BincodeSerialize(ref e) | Error::BincodeDeserialize(ref e) => Some(e),
#[cfg(feature = "json")]
Error::JsonSerialize(ref e) | Error::JsonDeserialize(ref e) => Some(e),
#[cfg(feature = "cbor")]
Error::CborSerialize(ref e) | Error::CborDeserialize(ref e) => Some(e),
}
}
}