use std::path::PathBuf;
use globset::Error as GlobSetError;
#[derive(Debug)]
pub enum DatabaseError {
FileNotFound,
IOError(std::io::Error),
InvalidGlobSet(GlobSetError),
Glob(String),
FileTooLarge(PathBuf, usize, usize),
ChangeLogInUse,
PoisonedLogMutex,
WatcherInit(notify::Error),
WatcherWatch(notify::Error),
WatcherNotActive,
}
impl std::fmt::Display for DatabaseError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::FileNotFound => write!(f, "file not found in database"),
Self::IOError(err) => write!(f, "I/O error: {err}"),
Self::InvalidGlobSet(err) => write!(f, "failed to build exclusion filter from patterns: {err}"),
Self::Glob(err) => write!(f, "glob pattern error: {err}"),
Self::FileTooLarge(path, size, max_size) => {
write!(
f,
"file at {} is too large to be processed: {} bytes (maximum is {} bytes)",
path.display(),
size,
max_size
)
}
Self::ChangeLogInUse => {
write!(f, "cannot commit changelog because it is still in use by another thread")
}
Self::PoisonedLogMutex => {
write!(f, "changelog is in an unrecoverable state because a thread panicked while modifying it")
}
Self::WatcherInit(err) => write!(f, "failed to initialize file watcher: {err}"),
Self::WatcherWatch(err) => write!(f, "failed to watch path: {err}"),
Self::WatcherNotActive => write!(f, "watcher is not currently watching - call watch() first"),
}
}
}
impl std::error::Error for DatabaseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::IOError(err) => Some(err),
Self::InvalidGlobSet(err) => Some(err),
Self::WatcherInit(err) | Self::WatcherWatch(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for DatabaseError {
fn from(error: std::io::Error) -> Self {
Self::IOError(error)
}
}
impl From<GlobSetError> for DatabaseError {
fn from(error: GlobSetError) -> Self {
Self::InvalidGlobSet(error)
}
}