feat(snippets): initialize snippet db

This commit creates the initial backend infrastructure for the snippets feature. It sets up the core data model, a dedicated `SnippetManager` for database interactions using `rusqlite`, and integrates it into the Tauri application state.
This commit is contained in:
ByteAtATime 2025-06-24 18:46:47 -07:00
parent 21cd217a26
commit 1a2fa36a79
No known key found for this signature in database
4 changed files with 60 additions and 4 deletions

View file

@ -13,12 +13,13 @@ mod quicklinks;
mod snippets;
mod system;
use crate::{app::App, cache::AppCache, snippets::input_manager::EvdevInputManager};
use crate::{app::App, cache::AppCache};
use browser_extension::WsState;
use frecency::FrecencyManager;
use quicklinks::QuicklinkManager;
use selection::get_text;
use snippets::input_manager::InputManager;
use snippets::input_manager::{InputManager, RdevInputManager};
use snippets::manager::SnippetManager;
use std::process::Command;
use std::thread;
use std::time::Duration;
@ -156,7 +157,7 @@ fn setup_global_shortcut(app: &mut tauri::App) -> Result<(), Box<dyn std::error:
fn setup_input_listener() {
thread::spawn(move || {
let manager = EvdevInputManager::new();
let manager = RdevInputManager::new();
let callback = |event| {
println!("[InputManager] Received Key: {:?}", event);
};
@ -245,6 +246,10 @@ pub fn run() {
let frecency_manager = FrecencyManager::new(app.handle().clone())?;
app.manage(frecency_manager);
let snippet_manager = SnippetManager::new(app.handle().clone())?;
snippet_manager.init_db()?;
app.manage(snippet_manager);
setup_background_refresh();
setup_global_shortcut(app)?;
setup_input_listener();

View file

@ -0,0 +1,36 @@
use crate::error::AppError;
use rusqlite::{Connection, Result as RusqliteResult};
use std::sync::Mutex;
use tauri::{AppHandle, Manager};
pub struct SnippetManager {
db: Mutex<Connection>,
}
impl SnippetManager {
pub fn new(app_handle: AppHandle) -> Result<Self, AppError> {
let data_dir = app_handle
.path()
.app_local_data_dir()
.map_err(|_| AppError::DirectoryNotFound)?;
let db_path = data_dir.join("snippets.sqlite");
let db = Connection::open(db_path)?;
Ok(Self { db: Mutex::new(db) })
}
pub fn init_db(&self) -> RusqliteResult<()> {
let db = self.db.lock().unwrap();
db.execute(
"CREATE TABLE IF NOT EXISTS snippets (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
keyword TEXT NOT NULL UNIQUE,
content TEXT NOT NULL,
created_at INTEGER NOT NULL,
updated_at INTEGER NOT NULL
)",
[],
)?;
Ok(())
}
}

View file

@ -1 +1,3 @@
pub mod input_manager;
pub mod manager;
pub mod types;

View file

@ -0,0 +1,13 @@
use chrono::{DateTime, Utc};
use serde::Serialize;
#[derive(Serialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Snippet {
pub id: i64,
pub name: String,
pub keyword: String,
pub content: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
}