Skip to main content

lore_engine/engine/
error.rs

1use std::fmt;
2use std::sync::PoisonError;
3
4/// Engine error type. All public engine functions return `Result<T, LoreError>`.
5#[derive(Debug)]
6pub enum LoreError {
7    /// `SQLite` error (query failure, constraint violation, etc.).
8    Db(rusqlite::Error),
9    /// Filesystem I/O error (read, write, rename, delete).
10    Io(std::io::Error),
11    /// A `Mutex` was poisoned (another thread panicked while holding it).
12    Lock(String),
13    /// File watcher failed to start or encountered a runtime error.
14    WatcherError(String),
15    /// A page title produced an empty or invalid slug.
16    InvalidSlug(String),
17    /// No page exists with the requested slug.
18    PageNotFound(String),
19    /// Attempted to save content to a placeholder page (no backing file).
20    PlaceholderPage(String),
21    /// No vault is currently open.
22    VaultNotSet,
23    /// A folder path contains unsafe components (e.g. `..` or absolute paths).
24    UnsafePath(String),
25}
26
27impl fmt::Display for LoreError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::Db(e) => write!(f, "Database error: {e}"),
31            Self::Io(e) => write!(f, "IO error: {e}"),
32            Self::Lock(e) => write!(f, "Lock poisoned: {e}"),
33            Self::WatcherError(e) => write!(f, "File watcher error: {e}"),
34            Self::InvalidSlug(s) => write!(f, "Invalid slug: {s}"),
35            Self::PageNotFound(s) => write!(f, "Page not found: {s}"),
36            Self::PlaceholderPage(s) => write!(f, "Cannot save a placeholder page: {s}"),
37            Self::VaultNotSet => write!(f, "No vault folder selected"),
38            Self::UnsafePath(s) => write!(f, "Unsafe path component: {s}"),
39        }
40    }
41}
42
43impl std::error::Error for LoreError {}
44
45impl From<rusqlite::Error> for LoreError {
46    fn from(e: rusqlite::Error) -> Self {
47        Self::Db(e)
48    }
49}
50
51impl From<std::io::Error> for LoreError {
52    fn from(e: std::io::Error) -> Self {
53        Self::Io(e)
54    }
55}
56
57impl<T> From<PoisonError<T>> for LoreError {
58    fn from(e: PoisonError<T>) -> Self {
59        Self::Lock(e.to_string())
60    }
61}
62
63impl From<LoreError> for String {
64    fn from(e: LoreError) -> Self {
65        e.to_string()
66    }
67}