1use std::{error, fmt, io};
2
3use crate::options::DurabilityMode;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12 Io(io::Error),
14 Corruption {
16 message: String,
18 },
19 InvalidFormat {
21 message: String,
23 },
24 UnsupportedFormat {
26 message: String,
28 },
29 CodecUnavailable {
31 codec: String,
33 },
34 Conflict {
36 message: String,
38 },
39 ReadOnly,
41 Closed,
43 RuntimeBusy {
45 message: String,
47 },
48 BucketMissing {
50 name: String,
52 },
53 InvalidOptions {
55 message: String,
57 },
58 Unsupported {
60 feature: &'static str,
62 },
63 UnsupportedBackend {
65 feature: &'static str,
67 },
68 UnsupportedDurability {
70 requested: DurabilityMode,
72 },
73}
74
75impl Error {
76 #[must_use]
78 pub const fn unsupported(feature: &'static str) -> Self {
79 Self::Unsupported { feature }
80 }
81
82 #[must_use]
84 pub const fn unsupported_backend(feature: &'static str) -> Self {
85 Self::UnsupportedBackend { feature }
86 }
87
88 #[must_use]
90 pub const fn unsupported_durability(requested: DurabilityMode) -> Self {
91 Self::UnsupportedDurability { requested }
92 }
93
94 #[must_use]
96 pub fn invalid_options(message: impl Into<String>) -> Self {
97 Self::InvalidOptions {
98 message: message.into(),
99 }
100 }
101
102 #[must_use]
104 pub fn runtime_busy(message: impl Into<String>) -> Self {
105 Self::RuntimeBusy {
106 message: message.into(),
107 }
108 }
109}
110
111impl fmt::Display for Error {
112 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
113 match self {
114 Self::Io(error) => write!(formatter, "io error: {error}"),
115 Self::Corruption { message } => write!(formatter, "corruption: {message}"),
116 Self::InvalidFormat { message } => write!(formatter, "invalid format: {message}"),
117 Self::UnsupportedFormat { message } => {
118 write!(formatter, "unsupported format: {message}")
119 }
120 Self::CodecUnavailable { codec } => write!(formatter, "codec unavailable: {codec}"),
121 Self::Conflict { message } => write!(formatter, "transaction conflict: {message}"),
122 Self::ReadOnly => formatter.write_str("database is read-only"),
123 Self::Closed => formatter.write_str("database is closed"),
124 Self::RuntimeBusy { message } => write!(formatter, "runtime busy: {message}"),
125 Self::BucketMissing { name } => write!(formatter, "bucket is missing: {name}"),
126 Self::InvalidOptions { message } => write!(formatter, "invalid options: {message}"),
127 Self::Unsupported { feature } => write!(formatter, "unsupported feature: {feature}"),
128 Self::UnsupportedBackend { feature } => {
129 write!(formatter, "unsupported storage backend feature: {feature}")
130 }
131 Self::UnsupportedDurability { requested } => {
132 write!(
133 formatter,
134 "unsupported durability mode: {}",
135 requested.as_str()
136 )
137 }
138 }
139 }
140}
141
142impl error::Error for Error {
143 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
144 match self {
145 Self::Io(error) => Some(error),
146 _ => None,
147 }
148 }
149}
150
151impl From<io::Error> for Error {
152 fn from(error: io::Error) -> Self {
153 Self::Io(error)
154 }
155}