use std::path::PathBuf;
fn config_paths() -> Vec<PathBuf> {
let mut paths = vec![PathBuf::from("ch32rv.toml")];
let env = |k: &str| {
std::env::var_os(k)
.filter(|v| !v.is_empty())
.map(PathBuf::from)
};
let dir = if cfg!(windows) {
env("APPDATA")
} else if cfg!(target_os = "macos") {
env("HOME").map(|h| h.join("Library").join("Application Support"))
} else {
env("XDG_CONFIG_HOME").or_else(|| env("HOME").map(|h| h.join(".config")))
};
if let Some(d) = dir {
paths.push(d.join("ch32rv").join("config.toml"));
}
paths
}
fn tables() -> impl Iterator<Item = (PathBuf, toml::Value)> {
config_paths().into_iter().filter_map(|path| {
let text = std::fs::read_to_string(&path).ok()?;
match toml::from_str(&text) {
Ok(v) => Some((path, v)),
Err(e) => {
eprintln!("warning[config-parse]: {}: {e}", path.display());
None
}
}
})
}
pub fn default_chip() -> Option<String> {
tables().find_map(|(_, v)| v.get("defaults")?.get("chip")?.as_str().map(str::to_owned))
}
pub fn probe_alias(name: &str) -> Option<String> {
tables().find_map(|(_, v)| v.get("probes")?.get(name)?.as_str().map(str::to_owned))
}