use serde::{Deserialize, Serialize};
use std::path::Path;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "kebab-case")]
pub enum Backend {
#[default]
Auto,
Rayon,
IoUring,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct Config {
pub include: Vec<String>,
pub exclude: Vec<String>,
pub max_file_size: u64,
pub respect_gitignore: bool,
pub index_hidden: bool,
pub backend: Backend,
pub merge_threshold: usize,
}
impl Default for Config {
fn default() -> Self {
Self {
include: Vec::new(),
exclude: vec![
"**/.git/**".to_string(),
"**/node_modules/**".to_string(),
"**/target/**".to_string(),
"**/.greplm/**".to_string(),
],
max_file_size: 4 * 1024 * 1024,
respect_gitignore: true,
index_hidden: false,
backend: Backend::Auto,
merge_threshold: 16,
}
}
}
impl Config {
pub fn load(path: &Path) -> Result<Config> {
match std::fs::read_to_string(path) {
Ok(s) => Ok(toml::from_str(&s)?),
Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Config::default()),
Err(e) => Err(Error::io(path, e)),
}
}
pub fn save(&self, path: &Path) -> Result<()> {
let s = toml::to_string_pretty(self)?;
std::fs::write(path, s).map_err(|e| Error::io(path, e))
}
}