1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::path::PathBuf;
use globset::Error as GlobSetError;
/// The primary error type for all database loading and mutation operations.
///
/// This enum consolidates errors from various sources, including file I/O,
/// pattern matching, and concurrent access issues, into a single, unified type.
#[derive(Debug)]
pub enum DatabaseError {
/// An attempt was made to access a file that does not exist in the database.
FileNotFound,
/// An error occurred during a filesystem read or write operation.
IOError(std::io::Error),
/// The set of user-provided glob patterns could not be compiled into a `GlobSet`.
InvalidGlobSet(GlobSetError),
/// An error occurred while processing a glob pattern.
Glob(String),
/// The file being loaded into the database is too large to be processed.
FileTooLarge(PathBuf, usize, usize),
/// An attempt was made to commit or consume a `ChangeLog` while other
/// references to it still exist, indicating that other threads may not have
/// finished their work.
ChangeLogInUse,
/// The lock on a `ChangeLog` was "poisoned."
///
/// This happens when a thread panics while holding the lock, leaving the
/// data in an unrecoverable and potentially inconsistent state.
PoisonedLogMutex,
/// Failed to initialize the file system watcher.
WatcherInit(notify::Error),
/// Failed to add a path to the file system watcher.
WatcherWatch(notify::Error),
/// Attempted to wait on a watcher that is not currently watching.
WatcherNotActive,
}
impl std::fmt::Display for DatabaseError {
#[inline]
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 {
#[inline]
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 {
#[inline]
fn from(error: std::io::Error) -> Self {
Self::IOError(error)
}
}
impl From<GlobSetError> for DatabaseError {
#[inline]
fn from(error: GlobSetError) -> Self {
Self::InvalidGlobSet(error)
}
}