pub type Result<T, E = Error> = std::result::Result<T, E>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Format {
message: String,
},
Locked,
Busy {
message: String,
},
Corrupt {
message: String,
},
ReadOnly,
Io {
source: std::io::Error,
},
Version {
found: u16,
supported: u16,
},
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Format { message } => write!(f, "format error: {message}"),
Self::Version { found, supported } => {
write!(
f,
"format error: unsupported version {found} (this build reads {supported})"
)
}
Self::Locked => f.write_str("store is locked by another writer"),
Self::Busy { message } => write!(f, "store is busy: {message}"),
Self::Corrupt { message } => write!(f, "store is corrupt: {message}"),
Self::ReadOnly => f.write_str("store was opened read-only"),
Self::Io { source } => write!(f, "io error: {source}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io { source } => Some(source),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(source: std::io::Error) -> Self {
Self::Io { source }
}
}
impl Error {
#[must_use]
pub fn format(message: impl Into<String>) -> Self {
Self::Format { message: message.into() }
}
#[must_use]
pub fn busy(message: impl Into<String>) -> Self {
Self::Busy { message: message.into() }
}
#[must_use]
pub fn corrupt(message: impl Into<String>) -> Self {
Self::Corrupt { message: message.into() }
}
}