use std::io;
use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum CacheError {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Deserialization error: {0}")]
Deserialization(String),
#[error("Cache capacity exceeded: {message}")]
CapacityExceeded {
message: String,
},
#[error("Storage backend error: {0}")]
StorageBackend(String),
#[error("Entry not found for key")]
NotFound,
#[error("Invalid configuration: {0}")]
InvalidConfiguration(String),
#[error("Persistence snapshot is too large ({actual_bytes} bytes; limit is {max_bytes})")]
SnapshotTooLarge {
actual_bytes: u64,
max_bytes: u64,
},
#[error("Unsupported persistence format: {0}")]
UnsupportedPersistenceFormat(String),
#[error("Custom error: {0}")]
Custom(String),
}
pub type Result<T> = std::result::Result<T, CacheError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_error_variants() {
let io_err: CacheError = io::Error::other("oops").into();
assert!(matches!(io_err, CacheError::Io(_)));
let ser_err = CacheError::Serialization("ser".into());
assert_eq!(format!("{ser_err}"), "Serialization error: ser");
let des_err = CacheError::Deserialization("de".into());
assert_eq!(format!("{des_err}"), "Deserialization error: de");
let cap_err = CacheError::CapacityExceeded {
message: "full".into(),
};
assert!(matches!(cap_err, CacheError::CapacityExceeded { .. }));
let backend_err = CacheError::StorageBackend("be".into());
assert!(matches!(backend_err, CacheError::StorageBackend(_)));
let not_found = CacheError::NotFound;
assert_eq!(format!("{not_found}"), "Entry not found for key");
let custom = CacheError::Custom("c".into());
assert_eq!(format!("{custom}"), "Custom error: c");
}
}