Add Rust modules: paths, jsonl, usage, watch, commands, state, settings

This commit is contained in:
megaproxy 2026-05-09 00:05:12 +01:00
parent 8abb0599f6
commit 14ffcf4bd3
9 changed files with 1226 additions and 0 deletions

58
src-tauri/src/state.rs Normal file
View file

@ -0,0 +1,58 @@
//! Process-global mutable state.
use parking_lot::{Mutex, RwLock};
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::sync::Arc;
use crate::jsonl::UsageEvent;
use crate::settings::Settings;
use crate::watch::WatcherHandle;
/// Per-file parse cache. We resume reading from `byte_offset` on the next
/// poll, so we never re-parse already-seen lines.
pub struct FileCache {
pub mtime_ns: i128,
pub size: u64,
pub byte_offset: u64,
pub messages: Vec<UsageEvent>,
}
pub struct AppState {
pub roots: RwLock<Vec<PathBuf>>,
pub files: RwLock<HashMap<PathBuf, FileCache>>,
pub seen_ids: RwLock<HashSet<String>>,
pub settings: RwLock<Settings>,
/// Boxed so we can keep the watcher alive across the whole app lifetime
/// without polluting Tauri's setup hook.
pub watcher: Mutex<Option<WatcherHandle>>,
}
pub type SharedState = Arc<AppState>;
impl AppState {
pub fn new(settings: Settings) -> SharedState {
Arc::new(Self {
roots: RwLock::new(Vec::new()),
files: RwLock::new(HashMap::new()),
seen_ids: RwLock::new(HashSet::new()),
settings: RwLock::new(settings),
watcher: Mutex::new(None),
})
}
pub fn set_watcher(&self, w: WatcherHandle) {
*self.watcher.lock() = Some(w);
}
/// Snapshot all events across all cached files in one allocation.
/// Caller is responsible for sorting if needed.
pub fn collect_events(&self) -> Vec<UsageEvent> {
let files = self.files.read();
let mut out = Vec::with_capacity(files.values().map(|f| f.messages.len()).sum());
for f in files.values() {
out.extend(f.messages.iter().cloned());
}
out
}
}