config_tools
config_tools is a lightweight, ergonomic Rust library for working with INI-style configuration files. It offers:
- A builder pattern for programmatic config creation
- Macros for concise default declarations
- Optional typed section parsing using
FromSection - Graceful fallbacks with
load_or_default_outcome serdesupport for full serialization and deserialization
It is built on top of rust-ini and designed for developer ergonomics first.
Quickstart
use ;
let outcome = load_or_default_outcome;
if outcome.used_default
let config = outcome.into_inner;
Manual Configuration
use Config;
let config = builder
.general
.set
.set
.section
.set
.set
.build;
Macros for Inline Defaults
use ;
let sectioned: Config = sectioned_defaults! ;
let general: Config = general_defaults! ;
Loading and Saving Configs
use Config;
let config = load?;
config.save?;
You can also handle missing files gracefully:
let default = builder.set.build;
let config = load_or_default;
Or check whether defaults were used:
let outcome = load_or_default_outcome;
if outcome.used_default
let config = outcome.into_inner;
Typed Section Parsing with FromSection
use ;
let config = load?;
let server_section = config.section.unwrap;
let server: ServerConfig = from_section?;
Config API
Config::builder(): Starts a new builderConfig::load(path): Loads from fileConfig::save(path): Saves to fileConfig::load_or_default(path, default): Uses a fallback if loading failsConfig::load_or_default_outcome(...): Same as above, but returnsLoadOutcomeconfig.get(section, key): Returns a value asOption<String>config.get_as::<T>(...): Parses value into a typeconfig.update(...): Updates or inserts a key-value pair
LoadOutcome
Returned from load_or_default_outcome:
LoadOutcome::FromFile(config)LoadOutcome::FromDefault(config)
Methods:
.into_inner(): Extract the config.as_ref(),.as_mut(): Borrow access.used_default() -> bool: Did fallback occur?
Macros
sectioned_defaults!
let config = sectioned_defaults! ;
Supports variables for section names, keys, and values (must be strings). General keys must come first.
general_defaults!
let config = general_defaults! ;
Procedural Macro: #[derive(FromSection)]
Allows typed parsing of section contents:
Fields must implement FromStr.