quick_search/src/config.rs

71 lines
2 KiB
Rust
Raw Normal View History

2025-09-11 14:08:46 -04:00
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Config {
pub paths: PathConfig,
pub processing: ProcessingConfig,
2025-09-11 14:08:46 -04:00
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PathConfig {
pub default_indexing_path: String,
pub database_path: String,
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ProcessingConfig {
pub hash_length: usize,
pub maximum_text_size: usize,
pub maximum_file_size: u64,
pub batch_size: usize,
pub tokenize: String,
}
2025-09-11 14:08:46 -04:00
impl Default for Config {
fn default() -> Self {
Config {
paths: PathConfig {
default_indexing_path: "C:\\".to_string(),
database_path: "QuickSearch.db".to_string(),
},
processing: ProcessingConfig {
hash_length: 1024 * 8,
maximum_text_size: 1024 * 512,
maximum_file_size: 1024 * 1024 * 50,
batch_size: 200,
tokenize: "trigram".to_string(),
},
2025-09-11 14:08:46 -04:00
}
}
}
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(())
}
}