Skip to main content

oomtop_config/
lib.rs

1//! # oomtop-config
2//!
3//! Everything under `~/.config/oomtop/` (UX §12), so `oomtop-core` stays pure:
4//!
5//! | Module | UX | What |
6//! |---|---|---|
7//! | [`paths`] | §12.2 | XDG paths on both OSes (config, state, `/etc/oomtop`) |
8//! | [`layered`] | §12.1.3 | precedence defaults → /etc → config.toml → config.d → host file → env → flags → runtime, origins (file:line) |
9//! | [`watch`] | §12.2 | live reload (debounced 200 ms), last good config kept on invalid edits |
10//! | [`write`] | §12.8 | comment-preserving `toml_edit` writes to a chosen layer |
11//! | [`template`], [`docs`], [`schema`] | §12.9 | `config init` template, per-setting docs, JSON Schemas for every file |
12//! | [`theme`], [`import`] | §12.3–12.4 | semantic tokens, built-in themes, contrast guard, base16/iTerm2/Ghostty/Kitty/Alacritty import |
13//! | [`keymap`] | §7, §12.7 | presets default/vim/emacs/htop, remapping, conflict detection |
14//! | [`layout`], [`expr`] | §12.6 | layouts, columns, sandboxed custom-column expressions |
15//! | [`views`] | §12.7 | aliases and saved views |
16//! | [`semantic`] | §12.9 | the one set of range/name/format rules shared by the loader, `config set`, `config validate` and the schema |
17//! | [`validate`] | §12.9 | `config validate` over all files |
18
19pub mod docs;
20pub mod expr;
21pub mod import;
22pub mod keymap;
23pub mod layered;
24pub mod layout;
25pub mod model;
26pub mod paths;
27pub mod schema;
28pub mod semantic;
29pub mod template;
30pub mod theme;
31pub mod validate;
32pub mod views;
33pub mod watch;
34pub mod write;
35
36pub use layered::{load_layered, EffectiveEntry, LoadOptions, Loaded, Origin};
37pub use model::Config;
38pub use template::{init_template, json_schema};
39pub use watch::LiveConfig;
40pub use write::{set_value, Layer};
41
42use std::path::{Path, PathBuf};
43
44/// A config problem with a location when known ("file:line: message").
45#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
46#[error("{}{}", location(.path, .line), .message)]
47pub struct ConfigError {
48    pub path: Option<PathBuf>,
49    pub line: Option<usize>,
50    pub message: String,
51}
52
53fn location(path: &Option<PathBuf>, line: &Option<usize>) -> String {
54    match (path, line) {
55        (Some(p), Some(l)) => format!("{}:{l}: ", p.display()),
56        (Some(p), None) => format!("{}: ", p.display()),
57        _ => String::new(),
58    }
59}
60
61impl ConfigError {
62    pub fn io(path: &Path, e: &std::io::Error) -> Self {
63        ConfigError {
64            path: Some(path.to_path_buf()),
65            line: None,
66            message: e.to_string(),
67        }
68    }
69}
70
71/// Loads with the real environment (`OOMTOP_CONFIG` honored).
72pub fn load_default() -> Loaded {
73    let opts = LoadOptions {
74        config_path: std::env::var_os("OOMTOP_CONFIG").map(PathBuf::from),
75        ..Default::default()
76    };
77    load_layered(&opts)
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn error_display() {
86        let e = ConfigError {
87            path: Some("/x/config.toml".into()),
88            line: Some(3),
89            message: "bad".into(),
90        };
91        assert_eq!(e.to_string(), "/x/config.toml:3: bad");
92        let e = ConfigError {
93            path: None,
94            line: None,
95            message: "bad".into(),
96        };
97        assert_eq!(e.to_string(), "bad");
98    }
99
100    #[test]
101    fn defaults_map_to_core() {
102        let c = Config::default();
103        assert_eq!(
104            c.headroom_config(),
105            oomtop_core::headroom::HeadroomConfig::default()
106        );
107        assert_eq!(c.idle_config(), oomtop_core::idle::IdleConfig::default());
108        assert_eq!(
109            c.personalization.weights.to_core(),
110            oomtop_core::ranking::RankWeights::default()
111        );
112        assert_eq!(c.appearance.theme, "terminal");
113        assert_eq!(c.serve.listen, "127.0.0.1:9469");
114    }
115}