1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use std::{error::Error as StdError, fmt};

/// An alias for Result<T, sled_extensions::Error>
pub type Result<T> = std::result::Result<T, Error>;

/// The error type for this library
///
/// If different features are enabled, different variantes of this enum will be present.
/// - `json` -- `JsonSerialize` | `JsonDeserialize`
/// - `cbor` -- `CborSerialize` | `CborDeserialize`
/// - `bincode` -- `BincodeSerialize` | `BincodeDeserialize`
///
#[derive(Debug)]
pub enum Error {
    #[cfg(feature = "json")]
    /// Json Serialization error
    JsonSerialize(serde_json::error::Error),

    #[cfg(feature = "json")]
    /// Json Deserialization error
    JsonDeserialize(serde_json::error::Error),

    #[cfg(feature = "cbor")]
    /// Cbor Serialization error
    CborSerialize(serde_cbor::error::Error),
    #[cfg(feature = "cbor")]
    /// Cbor Deserialization error
    CborDeserialize(serde_cbor::error::Error),

    #[cfg(feature = "bincode")]
    /// Bincode Serialization error
    BincodeSerialize(bincode::Error),
    #[cfg(feature = "bincode")]
    /// Bincode Deserialization error
    BincodeDeserialize(bincode::Error),

    /// Custom errors provided by users of this crate
    Custom(Box<dyn StdError + Send>),
    /// Errors in the Sled database
    Sled(sled::Error),
}

impl Error {
    /// Helper to automatically box custom error types
    pub fn custom<E>(error: E) -> Error
    where
        E: StdError + Send + '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),
        }
    }
}