1pub 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#[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
71pub 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}