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
use std::{error, fmt, io, str::Utf8Error};
/// Error indicating failures when reading and writing data from/to a
/// [`Storage`].
///
/// [`Storage`]: trait.Storage.html
#[derive(Debug)]
#[non_exhaustive]
pub enum ResourceStorageError {
/// Wrapper of [`io::Error`] with resource name for which the error
/// occurred.
///
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
Io(io::Error, String),
/// Wrapper of [`Utf8Error`].
///
/// [`Utf8Error`]: https://doc.rust-lang.org/std/str/struct.Utf8Error.html
Utf8Error(Utf8Error),
/// Indicates that schema for the resource with stored name is missing in
/// resource storage.
MissingSchema(String),
/// Indicates that part of the data is missing, e.g. one file of a multi_vector
MissingData,
/// Indicates that the schema stored in resource storage differs from the
/// expected schema.
WrongSignature {
/// Resource name for which the error occurred.
resource_name: String,
/// Diff from the stored schema to the expected schema.
diff: String,
},
/// Indicates that the size of the data does not fit to the serialized
/// control size.
///
/// When data is serialized to resource storage, a control header is
/// written which, in particular, contains the final size of the whole
/// resource.
UnexpectedDataSize,
/// A resource is too big, e.g. when references by a small number of bits
TooBig {
/// Resource name for which the error occurred.
resource_name: &'static str,
/// Size of the resource
size: usize,
},
/// A resource / archive is missing completely
Missing,
}
impl ResourceStorageError {
/// Wraps an [`io::Error`] with additional resource name.
///
/// [`io::Error`]: https://doc.rust-lang.org/std/io/struct.Error.html
pub fn from_io_error(err: io::Error, resource_name: String) -> Self {
ResourceStorageError::Io(err, resource_name)
}
}
impl fmt::Display for ResourceStorageError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{:?}", self)
}
}
impl error::Error for ResourceStorageError {
fn description(&self) -> &str {
match *self {
ResourceStorageError::Io(_, _) => "resource io error",
ResourceStorageError::MissingSchema(_) => "schema of resource is missing",
ResourceStorageError::MissingData => "resource has partially missing data",
ResourceStorageError::UnexpectedDataSize => "resource has unexpected size",
ResourceStorageError::Utf8Error(_) => "utf8 error in schema",
ResourceStorageError::WrongSignature { .. } => "schema is not matching expected schema",
ResourceStorageError::TooBig { .. } => "resource is too big",
ResourceStorageError::Missing => "Missing resource / archive",
}
}
}