use serde::Deserialize;
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct Config {
pub tab_size: usize,
pub indent_guides: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
tab_size: 4,
indent_guides: true,
}
}
}
pub struct Knob {
pub key: &'static str,
pub kind: &'static str, pub desc: &'static str,
}
pub const KNOBS: &[Knob] = &[
Knob {
key: "tab_size",
kind: "number",
desc: "indent width in spaces",
},
Knob {
key: "indent_guides",
kind: "bool",
desc: "dim │ guide per indent level",
},
];
impl Config {
pub fn print_knobs(&self) {
for k in KNOBS {
let value = match k.key {
"tab_size" => self.tab_size.to_string(),
"indent_guides" => self.indent_guides.to_string(),
_ => "?".into(),
};
println!(" {:<16} {:<7} {:<8} {}", k.key, k.kind, value, k.desc);
}
}
pub fn load() -> (Self, Option<String>) {
let Some(path) = config_path() else {
return (Self::default(), None);
};
let Ok(text) = std::fs::read_to_string(&path) else {
return (Self::default(), None); };
match toml::from_str::<Config>(&text) {
Ok(c) => (c, None),
Err(e) => (
Self::default(),
Some(format!("config {}: {e} — using defaults", path.display())),
),
}
}
pub fn indent(&self) -> String {
" ".repeat(self.tab_size)
}
}
fn config_path() -> Option<std::path::PathBuf> {
let base = std::env::var_os("XDG_CONFIG_HOME")
.map(std::path::PathBuf::from)
.or_else(|| {
std::env::var_os("HOME").map(|h| std::path::PathBuf::from(h).join(".config"))
})?;
Some(base.join("strop").join("config.toml"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn defaults_when_absent() {
let (c, err) = Config::load();
let _ = err; assert!(c.tab_size >= 2);
}
#[test]
fn parses_tab_size() {
let c: Config = toml::from_str("tab_size = 2").unwrap();
assert_eq!(c.tab_size, 2);
assert_eq!(c.indent(), " ");
}
#[test]
fn parses_indent_guides() {
let c: Config = toml::from_str("indent_guides = false").unwrap();
assert!(!c.indent_guides);
let c: Config = toml::from_str("").unwrap();
assert!(c.indent_guides);
}
#[test]
fn knobs_table_covers_every_field() {
assert_eq!(KNOBS.len(), 2);
}
#[test]
fn malformed_falls_back() {
assert!(toml::from_str::<Config>("tab_size = \"oops\"").is_err());
}
}