use serde::Deserialize;
use std::collections::HashMap;
use std::path::Path;
#[derive(Debug, Default, Clone, Deserialize)]
pub struct Config {
#[serde(default)]
pub plugins: HashMap<String, PluginConfig>,
#[serde(default)]
pub exclude: Vec<String>,
#[serde(default)]
pub debt_weights: DebtWeights,
}
#[derive(Debug, Clone, Deserialize)]
pub struct DebtWeights {
#[serde(default = "default_hint_debt")]
pub hint: u32,
#[serde(default = "default_warning_debt")]
pub warning: u32,
#[serde(default = "default_error_debt")]
pub error: u32,
}
fn default_hint_debt() -> u32 {
5
}
fn default_warning_debt() -> u32 {
15
}
fn default_error_debt() -> u32 {
30
}
impl Default for DebtWeights {
fn default() -> Self {
Self {
hint: 5,
warning: 15,
error: 30,
}
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct PluginConfig {
#[serde(default = "default_true")]
pub enabled: bool,
#[serde(flatten)]
pub options: HashMap<String, toml::Value>,
}
fn default_true() -> bool {
true
}
impl Default for PluginConfig {
fn default() -> Self {
Self {
enabled: true,
options: HashMap::new(),
}
}
}
impl Config {
pub fn load(dir: &Path) -> Self {
let path = dir.join(".cha.toml");
match std::fs::read_to_string(&path) {
Ok(content) => toml::from_str(&content).unwrap_or_default(),
Err(_) => Self::default(),
}
}
pub fn load_for_file(file_path: &Path, project_root: &Path) -> Self {
let abs_file = std::fs::canonicalize(file_path).unwrap_or(file_path.to_path_buf());
let abs_root = std::fs::canonicalize(project_root).unwrap_or(project_root.to_path_buf());
let dir = abs_file.parent().unwrap_or(&abs_root);
let mut configs = collect_configs_upward(dir, &abs_root);
configs.reverse();
let mut merged = Config::default();
for cfg in configs {
merged.merge(cfg);
}
merged
}
pub fn merge(&mut self, other: Config) {
for (name, other_pc) in other.plugins {
let entry = self.plugins.entry(name).or_default();
entry.enabled = other_pc.enabled;
for (k, v) in other_pc.options {
entry.options.insert(k, v);
}
}
self.exclude.extend(other.exclude);
self.debt_weights = other.debt_weights;
}
pub fn is_enabled(&self, name: &str) -> bool {
self.plugins.get(name).is_none_or(|c| c.enabled)
}
pub fn get_usize(&self, plugin: &str, key: &str) -> Option<usize> {
self.plugins
.get(plugin)?
.options
.get(key)?
.as_integer()
.map(|v| v as usize)
}
pub fn get_str(&self, plugin: &str, key: &str) -> Option<String> {
self.plugins
.get(plugin)?
.options
.get(key)?
.as_str()
.map(|s| s.to_string())
}
}
fn collect_configs_upward(start_dir: &Path, root: &Path) -> Vec<Config> {
let mut configs = Vec::new();
let mut current = start_dir.to_path_buf();
loop {
let cfg_path = current.join(".cha.toml");
if cfg_path.is_file()
&& let Ok(content) = std::fs::read_to_string(&cfg_path)
&& let Ok(cfg) = toml::from_str::<Config>(&content)
{
configs.push(cfg);
}
if current == root {
break;
}
match current.parent() {
Some(p) if p.starts_with(root) || p == root => current = p.to_path_buf(),
_ => break,
}
}
configs
}