About this project
dotcfg is a Rust crate for flexible, developer-controlled application configuration management. Unlike crates that enforce a fixed directory strategy or only support reading, dotcfg lets you choose where configs live and which formats to include, keeping the binary small with feature-gated format support.
### Storage locations
By default configs are stored in `~/.toolname/config.toml`, similar to `.cargo` or `.ssh`. Users can opt into XDG-standard paths via `.xdg()`, store at an arbitrary absolute path with `.at_dir()`, or let dotcfg walk up from the current working directory to locate a project-local config (analogous to how Git finds `.git`).
### Format support
TOML is enabled by default. JSON and YAML are available as optional features (`json`, `yaml`), and both can be combined so a single binary reads multiple formats. A custom filename can also be set (e.g. `settings.json`).
### Loading strategies
- `load()` — returns `None` when the file does not exist, letting the application decide how to proceed.
- `load_or_error()` — returns an error if the file is missing, suitable for CLIs that require prior setup.
- `load_or_default()` — creates the file populated with `Default` values on first use.
### Per-key access
Read and write individual keys without loading or overwriting the full configuration:
- `cfg.get("user.name")` / `cfg.set("user.name", "jane")`
- `cfg.get_as::<u16>("port")` / `cfg.set_val("port", 8080u16)`
Nested keys using dot notation (e.g. `section.field`) are supported and map naturally to TOML tables, JSON objects, and YAML mappings. Missing files or directories are created on write.
### Environment variable overrides
Prefix-based env overrides let `MYAPP_PORT=9000` take precedence over the file value without modifying disk state. `get` returns the raw string, while `get_as` parses booleans, numbers, and comma-separated `Vec` values. Write operations still target the config file only.
### CLI integration
The library works alongside `clap` to implement the standard precedence chain: CLI flag > config file > default fallback.
### Other utilities
`exists()`, `dir()`, `file_path()`, `delete_file()`, and `delete_dir()` are available for programmatic inspection and cleanup.
### License
Dual-licensed under MIT OR Apache-2.0.
Comments
0 Rating appears after 10 ratings
Sign in to join the discussion.