Files
rlogg/src/config.rs

40 lines
1.1 KiB
Rust

use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;
use crate::types::HighlightRule;
#[derive(Serialize, Deserialize, Default)]
pub struct AppConfig {
pub search_history: Vec<String>,
pub case_sensitive: bool,
pub use_regex: bool,
pub last_search_query: String,
#[serde(default)]
pub highlight_rules: Vec<HighlightRule>,
}
impl AppConfig {
fn config_path() -> PathBuf {
let exe_path = std::env::current_exe().unwrap_or_else(|_| PathBuf::from("."));
let exe_dir = exe_path.parent().unwrap_or_else(|| std::path::Path::new("."));
exe_dir.join("rlogg_config.json")
}
pub fn load() -> Self {
let path = Self::config_path();
if let Ok(contents) = fs::read_to_string(&path) {
serde_json::from_str(&contents).unwrap_or_default()
} else {
Self::default()
}
}
pub fn save(&self) {
let path = Self::config_path();
if let Ok(json) = serde_json::to_string_pretty(self) {
let _ = fs::write(&path, json);
}
}
}