Added config file

This commit is contained in:
Jeremy Karst 2025-09-11 14:08:46 -04:00
parent 8248517d42
commit 3e3369ba27
7 changed files with 78 additions and 6 deletions

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target
*.db
config.toml

2
Cargo.lock generated
View file

@ -3082,8 +3082,10 @@ dependencies = [
"dpc-pariter",
"quick-xml",
"rusqlite",
"serde",
"sha2",
"tokio",
"toml",
"tqdm",
"walkdir",
"zip",

View file

@ -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"

3
config_example.toml Normal file
View file

@ -0,0 +1,3 @@
[paths]
default_indexing_path = "C:\\"
database_path = "QuickSearch.db"

53
src/config.rs Normal file
View file

@ -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<Self, String> {
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(())
}
}

View file

@ -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<IndexingService>,
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();

View file

@ -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
}
}
}