Skip to main content

lore_engine/
state.rs

1use crate::engine::graph::WikiGraph;
2use notify_debouncer_mini::Debouncer;
3use rusqlite::Connection;
4use std::path::PathBuf;
5use std::sync::Mutex;
6
7/// Application state shared across all consumers (GUI, CLI, MCP).
8///
9/// Each consumer creates and owns its own `AppState` instance.
10/// The engine does not manage global state — that's the consumer's responsibility.
11///
12/// All fields are wrapped in [`Mutex`] for thread safety. The struct itself is
13/// `Send + Sync` (via Mutex), so it can be shared with `Arc<AppState>`.
14pub struct AppState {
15    /// Path to the currently open vault directory, or `None` if no vault is open.
16    pub vault_path: Mutex<Option<PathBuf>>,
17    /// `SQLite` connection to the vault's `.lore/lore.db`, or `None`.
18    pub db: Mutex<Option<Connection>>,
19    /// In-memory wiki link graph (nodes = pages, edges = links).
20    pub graph: Mutex<WikiGraph>,
21    /// File watcher handle. Drop to stop watching.
22    pub watcher: Mutex<Option<Debouncer<notify::RecommendedWatcher>>>,
23}
24
25impl Default for AppState {
26    fn default() -> Self {
27        Self::new()
28    }
29}
30
31impl AppState {
32    pub fn new() -> Self {
33        Self {
34            vault_path: Mutex::new(None),
35            db: Mutex::new(None),
36            graph: Mutex::new(WikiGraph::new()),
37            watcher: Mutex::new(None),
38        }
39    }
40}