use std::fmt;
use std::path::PathBuf;
use crate::store::cache::CacheError;
#[derive(Debug)]
pub enum StoreError {
DirectoryNotFound,
NotADirectory { path: PathBuf },
MissingParentDirectory { path: PathBuf },
InvalidCapacity,
Io(std::io::Error),
Compression(String),
Deserialise(String),
}
impl fmt::Display for StoreError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DirectoryNotFound => write!(f, "storage directory was not found"),
Self::NotADirectory { path } => {
write!(f, "storage path is not a directory: {}", path.display())
}
Self::MissingParentDirectory { path } => {
write!(f, "node path has no parent directory: {}", path.display())
}
Self::InvalidCapacity => write!(f, "cache capacity must be greater than zero"),
Self::Io(error) => write!(f, "disk store I/O error: {error}"),
Self::Compression(error) => write!(f, "zstd compression error: {error}"),
Self::Deserialise(error) => write!(f, "node deserialisation error: {error}"),
}
}
}
impl std::error::Error for StoreError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(error) => Some(error),
Self::DirectoryNotFound
| Self::NotADirectory { .. }
| Self::MissingParentDirectory { .. }
| Self::InvalidCapacity
| Self::Compression(_)
| Self::Deserialise(_) => None,
}
}
}
impl From<std::io::Error> for StoreError {
fn from(error: std::io::Error) -> Self {
Self::Io(error)
}
}
impl From<CacheError> for StoreError {
fn from(error: CacheError) -> Self {
match error {
CacheError::InvalidCapacity => Self::InvalidCapacity,
}
}
}