bhandaar 0.1.0-alpha.1

On-disk LRU/TTL file cache for Rust with cross-process concurrency via flock(2)
Documentation
use std::fmt;
use std::io;
use std::path::PathBuf;

/// Errors that can occur during cache operations.
#[derive(Debug)]
pub enum BhandaarError {
    /// Entry not found for the given key.
    NotFound(String),

    /// Entry already exists for the given key.
    /// Use `remove()` first, or handle with `get()` to check existence.
    AlreadyExists(String),

    /// Maximum cache size reached after eviction attempt.
    /// The entry could not be stored even after evicting LRU entries.
    CapacityExceeded,

    /// Underlying I/O error occurred.
    Io(io::Error),

    /// Metadata file is corrupted or unreadable.
    CorruptedMetadata(PathBuf),

    /// Serialization/deserialization error (bincode).
    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)
    }
}