use std::fmt;
use crate::{traits::Key, PathBuf};
#[derive(Debug, thiserror::Error)]
pub enum Error<K, E>
where
K: Key,
E: std::error::Error,
{
Init(PathBuf),
NotInCache(K),
NotOnDisk(K),
NotFound(K),
FileOp(E),
Immutable(K),
}
impl<K, E> fmt::Display for Error<K, E>
where
K: Key,
E: std::error::Error,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
use Error::*;
let repr = match self {
Init(path) => format!("Cannot initialise {path:?} as a backing directory."),
NotInCache(key) => format!("Cannot find an item with key {key:?} in cache"),
NotOnDisk(key) => format!("Cannot find an item with key {key:?} on disk"),
NotFound(key) => {
format!("Cannot find an item with key {key:?} either in cache or on disk")
}
FileOp(error) => {
format!("An error occurred when performing file operations: {error}")
}
Immutable(key) => format!(
"An item with key {key:?} is temporarily immutable due to outstanding references"
),
};
write!(f, "{repr}")
}
}
impl<K, E> From<E> for Error<K, E>
where
K: Key,
E: std::error::Error,
{
fn from(err: E) -> Self {
Self::FileOp(err)
}
}