use std::path::Path;
fn search_files_impl(vault_root: &Path, query: &str, search_content: bool) -> Result<Vec<String>, String> {
use crate::vault::init_vault_db;
if query.trim().is_empty() {
return Ok(Vec::new());
}
let vault_db = init_vault_db(vault_root).map_err(|e| format!("Failed to init vault DB: {}", e))?;
let fts_query = if search_content {
query.to_string()
} else {
format!("path:{} OR name:{}", query, query)
};
let mut stmt = vault_db
.conn
.prepare("SELECT path FROM files_fts WHERE files_fts MATCH ?1 LIMIT 100")
.map_err(|e| format!("Failed to prepare search statement: {}", e))?;
let results = stmt
.query_map([&fts_query], |row| row.get::<_, String>(0))
.map_err(|e| format!("Failed to execute search: {}", e))?
.collect::<Result<Vec<_>, _>>()
.map_err(|e| format!("Failed to collect results: {}", e))?;
let absolute_results = results
.into_iter()
.map(|rel_path| vault_root.join(&rel_path).to_string_lossy().into_owned())
.collect();
Ok(absolute_results)
}
#[tauri::command]
pub async fn search_files(
vault_root: String,
query: String,
search_content: bool,
) -> Result<Vec<String>, String> {
let root = Path::new(&vault_root);
search_files_impl(root, &query, search_content)
}