quick_search/src/config.rs

54 lines
1.5 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,
}
#[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(())
}
}