use std::fmt;
use std::io;
use std::path::PathBuf;
#[derive(Debug)]
pub enum BhandaarError {
NotFound(String),
AlreadyExists(String),
CapacityExceeded,
Io(io::Error),
CorruptedMetadata(PathBuf),
SerializationError(Box<dyn std::error::Error + Send + Sync>),
}
impl fmt::Display for BhandaarError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
BhandaarError::NotFound(key) => write!(f, "entry not found: {}", key),
BhandaarError::AlreadyExists(key) => write!(f, "entry already exists: {}", key),
BhandaarError::CapacityExceeded => write!(f, "cache capacity exceeded"),
BhandaarError::Io(e) => write!(f, "I/O error: {}", e),
BhandaarError::CorruptedMetadata(path) => {
write!(f, "corrupted metadata: {}", path.display())
}
BhandaarError::SerializationError(e) => write!(f, "serialization error: {}", e),
}
}
}
impl std::error::Error for BhandaarError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
BhandaarError::Io(e) => Some(e),
BhandaarError::SerializationError(e) => Some(e.as_ref()),
_ => None,
}
}
}
impl From<io::Error> for BhandaarError {
fn from(e: io::Error) -> Self {
BhandaarError::Io(e)
}
}
impl From<bincode::Error> for BhandaarError {
fn from(e: bincode::Error) -> Self {
BhandaarError::SerializationError(e)
}
}