1use std::path::PathBuf;
2
3use globset::Error as GlobSetError;
4
5#[derive(Debug)]
10pub enum DatabaseError {
11 FileNotFound,
13 IOError(std::io::Error),
15 InvalidGlobSet(GlobSetError),
17 Glob(String),
19 FileTooLarge(PathBuf, usize, usize),
21 ChangeLogInUse,
25 PoisonedLogMutex,
30 WatcherInit(notify::Error),
32 WatcherWatch(notify::Error),
34 WatcherNotActive,
36}
37
38impl std::fmt::Display for DatabaseError {
39 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40 match self {
41 Self::FileNotFound => write!(f, "file not found in database"),
42 Self::IOError(err) => write!(f, "I/O error: {err}"),
43 Self::InvalidGlobSet(err) => write!(f, "failed to build exclusion filter from patterns: {err}"),
44 Self::Glob(err) => write!(f, "glob pattern error: {err}"),
45 Self::FileTooLarge(path, size, max_size) => {
46 write!(
47 f,
48 "file at {} is too large to be processed: {} bytes (maximum is {} bytes)",
49 path.display(),
50 size,
51 max_size
52 )
53 }
54 Self::ChangeLogInUse => {
55 write!(f, "cannot commit changelog because it is still in use by another thread")
56 }
57 Self::PoisonedLogMutex => {
58 write!(f, "changelog is in an unrecoverable state because a thread panicked while modifying it")
59 }
60 Self::WatcherInit(err) => write!(f, "failed to initialize file watcher: {err}"),
61 Self::WatcherWatch(err) => write!(f, "failed to watch path: {err}"),
62 Self::WatcherNotActive => write!(f, "watcher is not currently watching - call watch() first"),
63 }
64 }
65}
66
67impl std::error::Error for DatabaseError {
68 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
69 match self {
70 Self::IOError(err) => Some(err),
71 Self::InvalidGlobSet(err) => Some(err),
72 Self::WatcherInit(err) | Self::WatcherWatch(err) => Some(err),
73 _ => None,
74 }
75 }
76}
77
78impl From<std::io::Error> for DatabaseError {
79 fn from(error: std::io::Error) -> Self {
80 Self::IOError(error)
81 }
82}
83
84impl From<GlobSetError> for DatabaseError {
85 fn from(error: GlobSetError) -> Self {
86 Self::InvalidGlobSet(error)
87 }
88}