1pub type Result<T, E = Error> = std::result::Result<T, E>;
11
12#[derive(Debug)]
14#[non_exhaustive]
15pub enum Error {
16 Format {
18 message: String,
20 },
21
22 Locked,
24
25 Busy {
28 message: String,
30 },
31
32 Corrupt {
34 message: String,
36 },
37
38 ReadOnly,
40
41 Io {
43 source: std::io::Error,
45 },
46}
47
48impl std::fmt::Display for Error {
49 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50 match self {
51 Self::Format { message } => write!(f, "format error: {message}"),
52 Self::Locked => f.write_str("store is locked by another writer"),
53 Self::Busy { message } => write!(f, "store is busy: {message}"),
54 Self::Corrupt { message } => write!(f, "store is corrupt: {message}"),
55 Self::ReadOnly => f.write_str("store was opened read-only"),
56 Self::Io { source } => write!(f, "io error: {source}"),
57 }
58 }
59}
60
61impl std::error::Error for Error {
62 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
63 match self {
64 Self::Io { source } => Some(source),
65 _ => None,
66 }
67 }
68}
69
70impl From<std::io::Error> for Error {
71 fn from(source: std::io::Error) -> Self {
72 Self::Io { source }
73 }
74}
75
76impl Error {
77 #[must_use]
79 pub fn format(message: impl Into<String>) -> Self {
80 Self::Format { message: message.into() }
81 }
82
83 #[must_use]
85 pub fn busy(message: impl Into<String>) -> Self {
86 Self::Busy { message: message.into() }
87 }
88
89 #[must_use]
91 pub fn corrupt(message: impl Into<String>) -> Self {
92 Self::Corrupt { message: message.into() }
93 }
94}