Expand description
Shared TOML config loader for hjkl-based apps.
Apps implement AppConfig on their config struct and call load
to read it from the platform’s XDG config directory. Missing files
return Default::default() paired with ConfigSource::Defaults —
no file is ever auto-created.
use hjkl_config::{AppConfig, load};
use serde::Deserialize;
#[derive(Debug, Default, Deserialize)]
struct MyConfig {
greeting: String,
}
impl AppConfig for MyConfig {
const APPLICATION: &'static str = "myapp";
}
let (cfg, source) = load::<MyConfig>().unwrap();Structs§
- Validation
Error - Standard validation error carrying a field name and a human-readable
message. Consumers can use this directly as their
Validate::Erroror wrap it in their own error type.
Enums§
- Config
Error - Config
Source - Where the loaded config came from.
Traits§
Functions§
- cache_
dir - Resolve
<XDG_CACHE_HOME>/<app>, defaulting to~/.cache/<app>. - config_
dir - Resolve
<XDG_CONFIG_HOME>/<app>, defaulting to~/.config/<app>. - config_
path - Resolve the full config file path for
C—<config_dir>/<FILE>. - data_
dir - Resolve
<XDG_DATA_HOME>/<app>, defaulting to~/.local/share/<app>. - ensure_
non_ empty_ str !value.is_empty(), elseErr.- ensure_
non_ zero value != T::default(), elseErr. For numeric typesT::default()is0, so this rejects zero. Generic overDefaultrather than a numeric trait to keep the dep footprint at zero.- ensure_
one_ of value ∈ allowed, elseErrwith the allowed list in the message. Use for enum-shaped string fields (theme names, channels, etc).- ensure_
range min ≤ value ≤ max, elseErr. Inclusive on both ends — the common shape for config bounds (e.g.tab_widthin1..=16).- load
- Load
Cfrom its default XDG path. - load_
from - Load
Cfrom an explicit path. Used by--config <PATH>flags and tests. - load_
layered - Load
Cby layering a user file over a bundled defaults TOML. - load_
layered_ from - Same as
load_layeredbut reads the user file from an explicit path (for--config <PATH>flags and tests). Always readspath— does not fall back to defaults if missing. - write_
key_ at - Set
dotted_path(e.g."explorer.width") tovaluein the TOML file atpath, preserving every other byte of the file.