diff --git a/.gitignore b/.gitignore index d9b2525..3452226 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target -*.db \ No newline at end of file +*.db +config.toml \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 6ba7819..715d9f0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3082,8 +3082,10 @@ dependencies = [ "dpc-pariter", "quick-xml", "rusqlite", + "serde", "sha2", "tokio", + "toml", "tqdm", "walkdir", "zip", diff --git a/Cargo.toml b/Cargo.toml index 42fb7c3..139860e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,5 @@ walkdir = "2.5.0" zip = "0.6" quick-xml = "0.31" tokio = { version = "1.0", features = ["time"] } +serde = { version = "1.0", features = ["derive"] } +toml = "0.8" diff --git a/config_example.toml b/config_example.toml new file mode 100644 index 0000000..3ed9698 --- /dev/null +++ b/config_example.toml @@ -0,0 +1,3 @@ +[paths] +default_indexing_path = "C:\\" +database_path = "QuickSearch.db" diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..94a42d8 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,53 @@ +use serde::{Deserialize, Serialize}; +use std::fs; +use std::path::Path; + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct Config { + pub paths: PathConfig, +} + +#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] +pub struct PathConfig { + pub default_indexing_path: String, + pub database_path: String, +} + +impl Default for Config { + fn default() -> Self { + Config { + paths: PathConfig { + default_indexing_path: "C:\\".to_string(), + database_path: "QuickSearch.db".to_string(), + }, + } + } +} + +impl Config { + pub fn load() -> Result { + let config_path = "config.toml"; + + if Path::new(config_path).exists() { + let content = fs::read_to_string(config_path) + .map_err(|e| format!("Failed to read config file: {}", e))?; + + toml::from_str(&content) + .map_err(|e| format!("Failed to parse config file: {}", e)) + } else { + let default_config = Config::default(); + default_config.save()?; + Ok(default_config) + } + } + + pub fn save(&self) -> Result<(), String> { + let content = toml::to_string_pretty(self) + .map_err(|e| format!("Failed to serialize config: {}", e))?; + + fs::write("config.toml", content) + .map_err(|e| format!("Failed to write config file: {}", e))?; + + Ok(()) + } +} diff --git a/src/frontend.rs b/src/frontend.rs index 7893728..4606895 100644 --- a/src/frontend.rs +++ b/src/frontend.rs @@ -3,21 +3,23 @@ use std::sync::Arc; use dioxus::prelude::*; use crate::indexing::{IndexingService, IndexingStatus}; +use crate::config::Config; #[derive(Props, Clone)] pub struct AppProps { pub indexing_service: Arc, + pub config: Config, } impl PartialEq for AppProps { fn eq(&self, other: &Self) -> bool { - Arc::ptr_eq(&self.indexing_service, &other.indexing_service) + Arc::ptr_eq(&self.indexing_service, &other.indexing_service) && self.config.paths.default_indexing_path == other.config.paths.default_indexing_path && self.config.paths.database_path == other.config.paths.database_path } } pub fn App(props: AppProps) -> Element { - let mut indexing_path = use_signal(|| "C:\\".to_string()); - let mut db_path = use_signal(|| "QuickSearch.db".to_string()); + let mut indexing_path = use_signal(|| props.config.paths.default_indexing_path.clone()); + let mut db_path = use_signal(|| props.config.paths.database_path.clone()); let mut status_text = use_signal(|| "Idle".to_string()); let indexing_service_for_start = props.indexing_service.clone(); diff --git a/src/main.rs b/src/main.rs index 3fb7402..d9cda52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,18 +5,27 @@ mod frontend; mod file_handling; mod document_extraction; mod indexing; +mod config; fn main() { - // Launch the frontend with the indexing service launch(app); } fn app() -> Element { + let config = match config::Config::load() { + Ok(config) => config, + Err(e) => { + eprintln!("Failed to load config: {}", e); + return rsx! { div { "Failed to load configuration" } }; + } + }; + let indexing_service = Arc::new(indexing::IndexingService::new()); rsx! { frontend::App { - indexing_service: indexing_service + indexing_service: indexing_service, + config: config } } }