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
use std::error::Error as StdError;
use std::fmt;
use std::io::Error as IoError;
use std::result::Result as StdResult;

use bincode::Error as BincodeError;
#[cfg(feature = "rocksdb-datastore")]
use rocksdb::Error as RocksDbError;
use serde_json::Error as JsonError;
#[cfg(feature = "sled-datastore")]
use sled::Error as SledError;
use tempfile::PersistError as TempFilePersistError;

/// An error triggered by the datastore
#[non_exhaustive]
#[derive(Debug)]
pub enum Error {
    /// Json (de-)serialization failed
    Json {
        inner: JsonError,
    },

    #[cfg(feature = "rocksdb-datastore")]
    #[deprecated(since = "2.1.0", note = "use the Datastore variant instead")]
    Rocksdb {
        inner: RocksDbError,
    },

    #[cfg(feature = "sled-datastore")]
    #[deprecated(since = "2.1.0", note = "use the Datastore variant instead")]
    Sled {
        inner: SledError,
    },

    UuidTaken,

    /// An error occurred in the underlying datastore
    Datastore {
        inner: Box<dyn StdError + Send>,
    },
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match *self {
            Error::Json { ref inner } => Some(inner),
            Error::Datastore { ref inner } => Some(&**inner),
            _ => None,
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Error::Json { ref inner } => write!(f, "json error: {}", inner),
            Error::UuidTaken => write!(f, "UUID already taken"),
            Error::Datastore { ref inner } => write!(f, "error in the underlying datastore: {}", inner),
            #[cfg(feature = "rocksdb-datastore")]
            #[allow(deprecated)]
            Error::Rocksdb { ref inner } => write!(f, "rocksdb error: {}", inner),
            #[cfg(feature = "sled-datastore")]
            #[allow(deprecated)]
            Error::Sled { ref inner } => write!(f, "sled error: {}", inner),
        }
    }
}

impl From<JsonError> for Error {
    fn from(err: JsonError) -> Self {
        Error::Json { inner: err }
    }
}

#[cfg(feature = "rocksdb-datastore")]
impl From<RocksDbError> for Error {
    fn from(err: RocksDbError) -> Self {
        Error::Datastore { inner: Box::new(err) }
    }
}

#[cfg(feature = "sled-datastore")]
impl From<SledError> for Error {
    fn from(err: SledError) -> Self {
        Error::Datastore { inner: Box::new(err) }
    }
}

impl From<IoError> for Error {
    fn from(err: IoError) -> Self {
        Error::Datastore { inner: Box::new(err) }
    }
}

impl From<BincodeError> for Error {
    fn from(err: BincodeError) -> Self {
        Error::Datastore { inner: Box::new(err) }
    }
}

impl From<TempFilePersistError> for Error {
    fn from(err: TempFilePersistError) -> Self {
        Error::Datastore { inner: Box::new(err) }
    }
}

pub type Result<T> = StdResult<T, Error>;

/// A validation error
#[derive(Debug)]
pub enum ValidationError {
    /// The value is invalid
    InvalidValue,
    /// The value is too long
    ValueTooLong,
    /// The input UUID is the maximum value, and cannot be incremented
    CannotIncrementUuid,
}

impl StdError for ValidationError {}

impl fmt::Display for ValidationError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ValidationError::InvalidValue => write!(f, "invalid value"),
            ValidationError::ValueTooLong => write!(f, "value too long"),
            ValidationError::CannotIncrementUuid => write!(f, "could not increment the UUID"),
        }
    }
}

pub type ValidationResult<T> = StdResult<T, ValidationError>;