use std::fmt;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Backend(String),
QuotaExceeded { needed: u64, available: Option<u64> },
ValueTooLarge { bytes: usize, max_inline: usize },
LockerTooLarge { bytes: u64, budget: u64 },
SchemaMismatch { stored: String, requested: String },
Corrupt(String),
UnsupportedVersion { found: u8, supported: u8 },
Filter(String),
InvalidConfig(String),
Closed,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Backend(msg) => write!(f, "storage backend failed: {msg}"),
Self::QuotaExceeded { needed, available } => match available {
Some(a) => write!(f, "quota exceeded: needed {needed} bytes, {a} available"),
None => write!(f, "quota exceeded: needed {needed} bytes"),
},
Self::ValueTooLarge { bytes, max_inline } => write!(
f,
"value of {bytes} bytes exceeds the {max_inline} byte inline limit \
for an eager locker; use a lazy locker instead"
),
Self::LockerTooLarge { bytes, budget } => write!(
f,
"eager locker holds {bytes} bytes, over its {budget} byte budget; \
use a lazy locker instead"
),
Self::SchemaMismatch { stored, requested } => write!(
f,
"schema mismatch: stored as {stored:?}, opened as {requested:?}"
),
Self::Corrupt(msg) => write!(f, "stored data is corrupt: {msg}"),
Self::UnsupportedVersion { found, supported } => write!(
f,
"envelope version {found} is not readable by this build (supports {supported})"
),
Self::Filter(msg) => write!(f, "codec or cipher failed: {msg}"),
Self::InvalidConfig(msg) => write!(f, "invalid locker configuration: {msg}"),
Self::Closed => write!(f, "the bank has been closed"),
}
}
}
impl std::error::Error for Error {}
impl Error {
pub fn backend(msg: impl fmt::Display) -> Self {
Self::Backend(msg.to_string())
}
pub fn is_corruption(&self) -> bool {
matches!(
self,
Self::Corrupt(_) | Self::UnsupportedVersion { .. } | Self::Filter(_)
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_is_send_sync_static() {
fn assert_bounds<T: Send + Sync + 'static>() {}
assert_bounds::<Error>();
assert_bounds::<Result<Vec<u8>>>();
}
#[test]
fn quota_message_reads_sensibly_without_an_estimate() {
let with = Error::QuotaExceeded {
needed: 10,
available: Some(4),
};
assert_eq!(
with.to_string(),
"quota exceeded: needed 10 bytes, 4 available"
);
let without = Error::QuotaExceeded {
needed: 10,
available: None,
};
assert_eq!(without.to_string(), "quota exceeded: needed 10 bytes");
}
#[test]
fn only_unreadable_bytes_count_as_corruption() {
assert!(Error::Corrupt("bad crc".into()).is_corruption());
assert!(Error::UnsupportedVersion {
found: 9,
supported: 1
}
.is_corruption());
assert!(Error::Filter("lz4 failed".into()).is_corruption());
assert!(!Error::backend("disk went away").is_corruption());
assert!(!Error::Closed.is_corruption());
assert!(!Error::SchemaMismatch {
stored: "a".into(),
requested: "b".into()
}
.is_corruption());
}
#[test]
fn value_too_large_names_the_alternative() {
let e = Error::ValueTooLarge {
bytes: 41_000_000,
max_inline: 262_144,
};
assert!(
e.to_string().contains("lazy locker"),
"the error must tell the caller what to do instead: {e}"
);
}
}