config-get
Cross-platform configuration file locator and reader for Rust.
config-get automatically discovers and reads configuration files from standard OS-specific locations. Supports .env, .ini, .toml, .json, .yml, and .yaml formats — no manual path wrangling required.
Features
- 🔍 Auto-discovery — searches platform-standard directories (
%APPDATA%,~/.config, etc.) - 📄 Multi-format —
.env,.ini,.toml,.json,.yml/.yaml - 🪟 Cross-platform — Windows, Linux, macOS (tested in CI)
- 🔗 Minimal deps — optional format features keep the dependency tree lean
- 🦀 Idiomatic Rust — builder pattern, typed parsing,
Indexoperator,thiserror-based errors - 🔄 Reload support — re-read config from disk at any time
- 🖥️ Optional CLI — inspect and query configs from the terminal
Installation
[]
= "0.1.7"
With optional format support:
# All formats (recommended)
= { = "0.1.7", = ["all"] }
# Pick and choose
= { = "0.1.7", = ["toml", "yaml", "dotenv", "ini"] }
| Feature | Enables | Crate | Default |
|---|---|---|---|
dotenv |
.env parsing |
dotenvy |
✓ |
ini |
.ini parsing |
rust-ini |
✓ |
toml |
.toml parsing |
toml |
✓ |
yaml |
.yml/.yaml |
serde_yaml |
✓ |
cli |
config-get binary |
clap |
✗ |
all |
All of the above | — | ✗ |
Quick Start
use ConfigGet;
Search Order
Linux / macOS
| Priority | Path |
|---|---|
| 1 | ~/<config_dir>/ |
| 2 | ~/.config/<config_dir>/ |
| 3 | ~/.config/ |
| 4 | ~/ |
| 5 | Current working directory |
Windows
| Priority | Path |
|---|---|
| 1 | %APPDATA%\<config_dir>\ |
| 2 | %USERPROFILE%\<config_dir>\ |
| 3 | %APPDATA%\ |
| 4 | %USERPROFILE%\ |
| 5 | Current working directory |
Within each directory, the following filenames are checked in order:
.env → <stem>.ini → <stem>.toml → <stem>.json → <stem>.yml → <stem>.yaml
API Reference
Builder
let cfg = builder
.config_dir // sub-directory to search (default: same as stem)
.auto_load // load on build() (default: true)
.create // create an empty .env if not found (default: false)
.build?;
Shortcut constructors
from_file?; // explicit path
from_env?; // .env shortcut
from_ini?;
from_toml?;
from_json?;
from_yaml?;
Reading values
| Method | Description |
|---|---|
cfg.get("KEY") |
Flat lookup → Option<&str> |
cfg.get_or("KEY", "default") |
Flat lookup with fallback |
cfg.require("KEY") |
Flat lookup, Err if absent |
cfg.get_in("section", "key") |
Section + key → Option<&str> |
cfg.get_in_or("section", "key", "default") |
Section + key with fallback |
cfg.require_in("section", "key") |
Section + key, Err if absent |
cfg.get_section("section") |
Entire section as IndexMap |
cfg.parse::<T>("KEY") |
Flat key parsed into T: FromStr |
cfg.parse_in::<T>("section", "key") |
Section key parsed into T |
cfg.all() |
Clone of entire ConfigMap |
cfg.reload(None) |
Re-read from disk (auto-discover) |
cfg.reload(Some(path)) |
Re-read from explicit path |
cfg.loaded_from() |
Path the config was loaded from |
Discovery helpers
// Inspect candidate paths without loading
let paths = search_paths;
for p in &paths
// Module-level helper
use get_config_file;
if let Some = get_config_file
Iteration
// Flat entries
for in cfg.iter
// Section names
for section in cfg.sections
// Membership
if cfg.contains_key
println!;
Format Examples
.env
DB_HOST=localhost
DB_PORT=5432
SECRET_KEY="my-secret"
let cfg = from_file?;
println!; // localhost
.ini
[database]
host = localhost
port = 5432
[server]
debug = true
let cfg = from_file?;
println!;
let section = cfg.get_section?;
.toml
[]
= "localhost"
= 5432
let cfg = from_file?;
println!;
.json
let cfg = from_file?;
let port: u16 = cfg.parse_in?;
.yaml / .yml
database:
host: localhost
port: 5432
let cfg = from_file?;
println!;
Error Handling
All errors implement std::error::Error via thiserror:
use ;
match builder.config_dir.build
CLI
Enable the cli feature and the config-get binary is built:
config-get find myapp --dir myapp
config-get dump myapp --dir myapp
config-get get myapp DB_HOST --dir myapp --fallback localhost
config-get get myapp server.debug --dir myapp
config-get paths myapp --dir myapp
Logging
config-get uses the standard log facade.
Wire up any compatible backend (e.g. env_logger) and set RUST_LOG=debug to
see which files are being searched and loaded.
[]
= "0.11"
init;
// Now config-get emits debug-level messages to stderr.
MSRV
Minimum Supported Rust Version: 1.70 (tested in CI against stable, beta, and 1.70).
License
MIT © Hadi Cahyadi
