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
138
139
140
141
142
143
144
use std::{io, fmt, result};
use std::error::Error;
use uuid::{self, Uuid};
#[derive(Debug)]
pub struct StorageError(Box<RawStorageError>);
#[derive(Debug)]
struct RawStorageError {
kind: StorageErrorKind,
source: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl StorageError {
pub(crate) fn not_found(uuid: Uuid) -> Self {
Self(Box::new(RawStorageError {
kind: StorageErrorKind::NotFound(uuid),
source: None,
}))
}
pub(crate) fn invalid_uuid<E>(error: E) -> Self
where E: Into<Box<dyn std::error::Error + Send + Sync>> {
Self(Box::new(RawStorageError {
kind: StorageErrorKind::InvalidUuid,
source: Some(error.into()),
}))
}
pub(crate) fn io<E>(error: E) -> Self
where E: Into<Box<dyn std::error::Error + Send + Sync>> {
Self(Box::new(RawStorageError {
kind: StorageErrorKind::Io,
source: Some(error.into()),
}))
}
pub(crate) fn other<E>(error: E) -> Self
where E: Into<Box<dyn std::error::Error + Send + Sync>> {
Self(Box::new(RawStorageError {
kind: StorageErrorKind::Other,
source: Some(error.into()),
}))
}
pub fn kind(&self) -> StorageErrorKind {
self.0.kind
}
pub fn is_not_found(&self) -> bool {
if let StorageErrorKind::NotFound(_) = self.0.kind {
true
} else {
false
}
}
pub fn is_invalid_uuid(&self) -> bool {
if let StorageErrorKind::InvalidUuid = self.0.kind {
true
} else {
false
}
}
pub fn is_io(&self) -> bool {
if let StorageErrorKind::Io = self.0.kind {
true
} else {
false
}
}
pub fn is_other(&self) -> bool {
if let StorageErrorKind::Other = self.0.kind {
true
} else {
false
}
}
}
impl Error for StorageError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self.0.source {
Some(ref source) => Some(&**source),
None => None
}
}
}
impl fmt::Display for StorageError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.kind.fmt(f)
}
}
impl From<uuid::Error> for StorageError {
fn from(error: uuid::Error) -> Self {
Self::invalid_uuid(error)
}
}
impl From<io::Error> for StorageError {
fn from(error: io::Error) -> Self {
Self::io(error)
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum StorageErrorKind {
NotFound(Uuid),
InvalidUuid,
Io,
Other,
}
impl fmt::Display for StorageErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use StorageErrorKind::*;
match self {
NotFound(id) => write!(f, "entry {} not found", id),
InvalidUuid => write!(f, "invalid uuid"),
Io => write!(f, "io error"),
Other => write!(f, "other error"),
}
}
}
pub type Result<T> = result::Result<T, StorageError>;