1use std::error::Error as StdError;
2use std::fmt;
3use std::io::Error as IoError;
4use std::result::Result as StdResult;
5
6#[cfg(feature = "rocksdb-datastore")]
7use bincode::Error as BincodeError;
8use rmp_serde::encode::Error as RmpEncodeError;
9#[cfg(feature = "rocksdb-datastore")]
10use rocksdb::Error as RocksDbError;
11use serde_json::Error as JsonError;
12
13#[non_exhaustive]
15#[derive(Debug)]
16pub enum Error {
17 UuidTaken,
19
20 Datastore(Box<dyn StdError + Send + Sync>),
22
23 Io(IoError),
25
26 NotIndexed,
28
29 Unsupported,
31
32 Invalid(ValidationError),
34
35 OperationOnQuery,
38}
39
40impl StdError for Error {
41 fn source(&self) -> Option<&(dyn StdError + 'static)> {
42 match *self {
43 Error::Datastore(ref err) => Some(&**err),
44 Error::Io(ref err) => Some(err),
45 Error::Invalid(ref err) => Some(err),
46 _ => None,
47 }
48 }
49}
50
51impl fmt::Display for Error {
52 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
53 match *self {
54 Error::UuidTaken => write!(f, "UUID already taken"),
55 Error::Datastore(ref err) => write!(f, "error in the underlying datastore: {err}"),
56 Error::Io(ref err) => write!(f, "I/O error: {err}"),
57 Error::NotIndexed => write!(f, "query attempted on a property that isn't indexed"),
58 Error::Unsupported => write!(f, "functionality not supported"),
59 Error::Invalid(ref err) => write!(f, "{err}"),
60 Error::OperationOnQuery => write!(f, "the operation cannot work with the given query"),
61 }
62 }
63}
64
65impl From<JsonError> for Error {
66 fn from(err: JsonError) -> Self {
67 Error::Datastore(Box::new(err))
68 }
69}
70
71impl From<IoError> for Error {
72 fn from(err: IoError) -> Self {
73 Error::Io(err)
74 }
75}
76
77#[cfg(feature = "rocksdb-datastore")]
78impl From<BincodeError> for Error {
79 fn from(err: BincodeError) -> Self {
80 Error::Datastore(Box::new(err))
81 }
82}
83
84impl From<RmpEncodeError> for Error {
85 fn from(err: RmpEncodeError) -> Self {
86 Error::Datastore(Box::new(err))
87 }
88}
89
90#[cfg(feature = "rocksdb-datastore")]
91impl From<RocksDbError> for Error {
92 fn from(err: RocksDbError) -> Self {
93 Error::Datastore(Box::new(err))
94 }
95}
96
97impl From<ValidationError> for Error {
98 fn from(err: ValidationError) -> Self {
99 Error::Invalid(err)
100 }
101}
102
103pub type Result<T> = StdResult<T, Error>;
105
106#[derive(Debug)]
108pub enum ValidationError {
109 InvalidValue,
111 ValueTooLong,
113 CannotIncrementUuid,
115 InnerQuery,
119}
120
121impl StdError for ValidationError {}
122
123impl fmt::Display for ValidationError {
124 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
125 match *self {
126 ValidationError::InvalidValue => write!(f, "invalid value"),
127 ValidationError::ValueTooLong => write!(f, "value too long"),
128 ValidationError::CannotIncrementUuid => write!(f, "could not increment the UUID"),
129 ValidationError::InnerQuery => write!(f, "the given query combination cannot be nested"),
130 }
131 }
132}
133
134pub type ValidationResult<T> = StdResult<T, ValidationError>;