From 41842ee356c7d49a7a3b173383471cb59822c807 Mon Sep 17 00:00:00 2001 From: Nikita Galaiko Date: Mon, 6 Mar 2023 09:32:45 +0100 Subject: [PATCH] expose search api --- src-tauri/src/main.rs | 21 ++++++++++++++++++++- src-tauri/src/search/deltas.rs | 3 ++- src-tauri/src/search/mod.rs | 2 +- src/lib/search.ts | 12 ++++++++++++ 4 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/lib/search.ts diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 99319aa31..b3ae7ea23 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -137,6 +137,24 @@ fn proxy_image(handle: tauri::AppHandle, src: &str) -> Result { Ok(build_asset_url(&save_to.display().to_string())) } +#[tauri::command] +fn search( + handle: tauri::AppHandle, + project_id: &str, + query: &str, +) -> Result, Error> { + let app_state = handle.state::(); + + let deltas = app_state + .deltas_searcher + .lock() + .unwrap() + .search(project_id, query) + .with_context(|| format!("Failed to search for {}", query))?; + + Ok(deltas) +} + #[tauri::command] fn list_sessions( handle: tauri::AppHandle, @@ -444,7 +462,8 @@ fn main() { list_session_files, set_user, delete_user, - get_user + get_user, + search ]); let tauri_context = generate_context!(); diff --git a/src-tauri/src/search/deltas.rs b/src-tauri/src/search/deltas.rs index 619dafca4..3c9faecc8 100644 --- a/src-tauri/src/search/deltas.rs +++ b/src-tauri/src/search/deltas.rs @@ -1,5 +1,6 @@ use crate::{deltas, projects, sessions, storage}; use anyhow::{Context, Result}; +use serde::Serialize; use std::{ fs, path::{Path, PathBuf}, @@ -161,7 +162,7 @@ fn build_schema() -> schema::Schema { const WRITE_BUFFER_SIZE: usize = 10_000_000; // 10MB -#[derive(Debug)] +#[derive(Debug, Serialize)] pub struct SearchResult { pub project_id: String, pub session_id: String, diff --git a/src-tauri/src/search/mod.rs b/src-tauri/src/search/mod.rs index 7d707a7a4..e4a81e6c2 100644 --- a/src-tauri/src/search/mod.rs +++ b/src-tauri/src/search/mod.rs @@ -1,6 +1,6 @@ mod deltas; -pub use deltas::Deltas; +pub use deltas::{Deltas, SearchResult}; #[cfg(test)] mod deltas_test; diff --git a/src/lib/search.ts b/src/lib/search.ts new file mode 100644 index 000000000..4179a8d91 --- /dev/null +++ b/src/lib/search.ts @@ -0,0 +1,12 @@ +import { invoke } from '@tauri-apps/api'; + +export type SearchResult = { + project_id: string; + session_id: string; + file_id: string; + // index of the delta in the session. + index: number; +}; + +export const search = (params: { project_id: string; query: string }) => + invoke('search', params);