| Crates.io | confyg |
| lib.rs | confyg |
| version | 0.3.0 |
| created_at | 2022-11-13 19:05:13.364934+00 |
| updated_at | 2026-01-14 17:59:18.948309+00 |
| description | A simple, TOML-based, ENV-enabled library that can find and merge configs |
| homepage | |
| repository | https://github.com/oxur/confyg |
| max_upload_size | |
| id | 714443 |
| size | 163,657 |
A simple, TOML-based, ENV-enabled library for configuration management
confyg provides a flexible way to build application configurations by:
Add to your Cargo.toml:
[dependencies]
confyg = "0.3"
serde = { version = "1.0", features = ["derive"] }
Basic example:
use confyg::{Confygery, Result};
use serde_derive::Deserialize;
#[derive(Debug, Deserialize)]
struct Config {
env: String,
database: Database,
}
#[derive(Debug, Deserialize)]
struct Database {
host: String,
port: u16,
}
fn main() -> Result<()> {
let config: Config = Confygery::new()?
.add_file("defaults.toml")?
.add_file("production.toml")?
.build()?;
println!("Environment: {}", config.env);
println!("Database: {}:{}", config.database.host, config.database.port);
Ok(())
}
use confyg::{Confygery, conf, Result};
let mut opts = conf::Options::default();
opts.add_path("./config")
.add_path("/etc/myapp")
.add_path("/usr/local/etc/myapp");
let config: MyConfig = Confygery::new()?
.with_opts(opts)?
.add_file("defaults.toml")?
.add_file("production.toml")?
.build()?;
Environment variables follow the pattern: TOPLEVEL_SECTION_KEY=value
# Set environment variables
export MYAPP_ENV=production
export MYAPP_DATABASE_HOST=db.example.com
export MYAPP_DATABASE_PORT=5432
use confyg::{Confygery, env, Result};
let mut env_opts = env::Options::with_top_level("myapp");
env_opts.add_section("database");
let config: MyConfig = Confygery::new()?
.add_file("defaults.toml")?
.add_env(env_opts)? // Environment overrides file config
.build()?;
Sources added later override earlier sources:
Confygery::new()?
.add_str(defaults_toml)? // Priority: 1 (lowest)
.add_file("defaults.toml")? // Priority: 2
.add_file("production.toml")? // Priority: 3
.add_env(env_opts)? // Priority: 4 (highest)
.build()?
let config: MyConfig = Confygery::new()?
.add_str(r#"
env = "development"
[database]
host = "localhost"
port = 5432
"#)?
.build()?;
use serde_derive::Serialize;
#[derive(Serialize)]
struct Defaults {
timeout: u32,
retries: u8,
}
let defaults = Defaults {
timeout: 30,
retries: 3,
};
let config: MyConfig = Confygery::new()?
.add_struct(&defaults)?
.add_file("config.toml")?
.build()?;
Environment variables are converted to lowercase TOML keys:
| Environment Variable | TOML Equivalent |
|---|---|
MYAPP_ENV |
env = "..." |
MYAPP_LOG_LEVEL |
log_level = "..." |
MYAPP_DATABASE_HOST |
[database]host = "..." |
MYAPP_DATABASE_PORT |
[database]port = "..." |
Note: Due to environment variable naming limitations:
my-app → MY_APP)See the examples directory for complete working examples:
env.rs - Environment variable scanningenv_str.rs - Combining strings and environment variablesfile_search.rs - File path searchingfull.rs - Complete example with all featuresRun examples:
make demos # Run all examples with environment variables set
Full API documentation is available on docs.rs.
All operations return Result<T, ConfigError> with descriptive error messages:
match Confygery::new()?.add_file("config.toml")?.build::<MyConfig>() {
Ok(config) => println!("Loaded config: {:?}", config),
Err(e) => eprintln!("Configuration error: {}", e),
}
Error types include:
FileRead - Failed to read a fileTomlParse - Invalid TOML syntaxTomlSerialize - Failed to serialize to TOMLFileNotFound - File not found in search pathsNoConfigs - No configuration sources addedInvalidState - Invalid configuration stateInvalidPath - Path contains invalid UTF-8serde with derive feature for deserializationCopyright © 2022-2026, Oxur Group
Apache License, Version 2.0