Skip to main content

Crate hjkl_config

Crate hjkl_config 

Source
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§

ValidationError
Standard validation error carrying a field name and a human-readable message. Consumers can use this directly as their Validate::Error or wrap it in their own error type.

Enums§

ConfigError
ConfigSource
Where the loaded config came from.

Traits§

AppConfig
Application config descriptor.
Validate
Optional consumer-defined validation hook.

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(), else Err.
ensure_non_zero
value != T::default(), else Err. For numeric types T::default() is 0, so this rejects zero. Generic over Default rather than a numeric trait to keep the dep footprint at zero.
ensure_one_of
value ∈ allowed, else Err with the allowed list in the message. Use for enum-shaped string fields (theme names, channels, etc).
ensure_range
min ≤ value ≤ max, else Err. Inclusive on both ends — the common shape for config bounds (e.g. tab_width in 1..=16).
load
Load C from its default XDG path.
load_from
Load C from an explicit path. Used by --config <PATH> flags and tests.
load_layered
Load C by layering a user file over a bundled defaults TOML.
load_layered_from
Same as load_layered but reads the user file from an explicit path (for --config <PATH> flags and tests). Always reads path — does not fall back to defaults if missing.
write_key_at
Set dotted_path (e.g. "explorer.width") to value in the TOML file at path, preserving every other byte of the file.