pub mod docs;
pub mod expr;
pub mod import;
pub mod keymap;
pub mod layered;
pub mod layout;
pub mod model;
pub mod paths;
pub mod schema;
pub mod semantic;
pub mod template;
pub mod theme;
pub mod validate;
pub mod views;
pub mod watch;
pub mod write;
pub use layered::{load_layered, EffectiveEntry, LoadOptions, Loaded, Origin};
pub use model::Config;
pub use template::{init_template, json_schema};
pub use watch::LiveConfig;
pub use write::{set_value, Layer};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{}{}", location(.path, .line), .message)]
pub struct ConfigError {
pub path: Option<PathBuf>,
pub line: Option<usize>,
pub message: String,
}
fn location(path: &Option<PathBuf>, line: &Option<usize>) -> String {
match (path, line) {
(Some(p), Some(l)) => format!("{}:{l}: ", p.display()),
(Some(p), None) => format!("{}: ", p.display()),
_ => String::new(),
}
}
impl ConfigError {
pub fn io(path: &Path, e: &std::io::Error) -> Self {
ConfigError {
path: Some(path.to_path_buf()),
line: None,
message: e.to_string(),
}
}
}
pub fn load_default() -> Loaded {
let opts = LoadOptions {
config_path: std::env::var_os("OOMTOP_CONFIG").map(PathBuf::from),
..Default::default()
};
load_layered(&opts)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_display() {
let e = ConfigError {
path: Some("/x/config.toml".into()),
line: Some(3),
message: "bad".into(),
};
assert_eq!(e.to_string(), "/x/config.toml:3: bad");
let e = ConfigError {
path: None,
line: None,
message: "bad".into(),
};
assert_eq!(e.to_string(), "bad");
}
#[test]
fn defaults_map_to_core() {
let c = Config::default();
assert_eq!(
c.headroom_config(),
oomtop_core::headroom::HeadroomConfig::default()
);
assert_eq!(c.idle_config(), oomtop_core::idle::IdleConfig::default());
assert_eq!(
c.personalization.weights.to_core(),
oomtop_core::ranking::RankWeights::default()
);
assert_eq!(c.appearance.theme, "terminal");
assert_eq!(c.serve.listen, "127.0.0.1:9469");
}
}