| Crates.io | config-tools |
| lib.rs | config-tools |
| version | 0.5.0 |
| created_at | 2024-10-15 14:03:13.724507+00 |
| updated_at | 2025-05-05 21:17:28.124081+00 |
| description | A simplified set of tools for working with configuration files. |
| homepage | https://github.com/piccoloser/config-tools |
| repository | https://github.com/piccoloser/config-tools |
| max_upload_size | |
| id | 1409497 |
| size | 53,972 |
config_tools is a lightweight, ergonomic Rust library for working with INI-style configuration files. It offers:
FromSectionload_or_default_outcomeserde support for full serialization and deserializationIt is built on top of rust-ini and designed for developer ergonomics first.
use config_tools::{Config, sectioned_defaults};
let outcome = Config::load_or_default_outcome(
"config.ini",
sectioned_defaults! {
{ "debug" => "true" }
["App"] {
"threads" => "4"
}
}
);
if outcome.used_default() {
eprintln!("Using fallback config.");
}
let config = outcome.into_inner();
use config_tools::Config;
let config = Config::builder()
.general()
.set("logging", "true")
.set("verbose", "false")
.section("Database")
.set("host", "localhost")
.set("port", "5432")
.build();
use config_tools::{sectioned_defaults, general_defaults, Config};
let sectioned: Config = sectioned_defaults! {
{ "logging" => "true" }
["Server"] {
"host" => "127.0.0.1",
"port" => "8080"
}
};
let general: Config = general_defaults! {
"console" => "true",
"logging" => "true",
};
use config_tools::Config;
let config = Config::load("config.ini")?;
config.save("out.ini")?;
You can also handle missing files gracefully:
let default = Config::builder().set("fallback", "true").build();
let config = Config::load_or_default("config.ini", default);
Or check whether defaults were used:
let outcome = Config::load_or_default_outcome("config.ini", Config::default());
if outcome.used_default() {
println!("File not found; using defaults.");
}
let config = outcome.into_inner();
FromSectionuse config_tools::{Config, FromSection};
#[derive(FromSection)]
struct ServerConfig {
host: String,
port: u16,
}
let config = Config::load("config.ini")?;
let server_section = config.section("Server").unwrap();
let server: ServerConfig = ServerConfig::from_section(server_section)?;
Config APIConfig::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 returns LoadOutcomeconfig.get(section, key): Returns a value as Option<String>config.get_as::<T>(...): Parses value into a typeconfig.update(...): Updates or inserts a key-value pairLoadOutcomeReturned from load_or_default_outcome:
LoadOutcome::FromFile(config)LoadOutcome::FromDefault(config).into_inner(): Extract the config.as_ref(), .as_mut(): Borrow access.used_default() -> bool: Did fallback occur?sectioned_defaults!let config = sectioned_defaults! {
{ "logging" => "true" }
["App"] {
"theme" => "dark"
}
};
Supports variables for section names, keys, and values (must be strings). General keys must come first.
general_defaults!let config = general_defaults! {
"logging" => "true",
"console" => "true"
};
#[derive(FromSection)]Allows typed parsing of section contents:
#[derive(FromSection)]
struct MySettings {
path: String,
enabled: bool,
}
Fields must implement FromStr.