confyg
A simple, TOML-based, ENV-enabled library for configuration management
Overview
confyg provides a flexible way to build application configurations by:
- Loading from TOML files with path searching
- Scanning environment variables with prefix mapping
- Merging multiple configuration sources with override semantics
- Deserializing directly into strongly-typed Rust structs
Features
- Multiple Sources: Combine TOML files, environment variables, strings, and Rust structs
- Path Search: Automatically search multiple directories for configuration files
- Environment Variable Mapping: Map prefixed environment variables to TOML sections
- Deep Merging: Intelligently merge configurations with clear priority rules
- Type Safety: Deserialize directly into your Rust types using serde
- Error Handling: Comprehensive error types with context preservation
- Zero Unsafe: Pure safe Rust with no unsafe code
Quick Start
Add to your Cargo.toml:
[]
= "0.3"
= { = "1.0", = ["derive"] }
Basic example:
use ;
use Deserialize;
Usage Examples
Loading from Files
use ;
let mut opts = default;
opts.add_path
.add_path
.add_path;
let config: MyConfig = new?
.with_opts?
.add_file?
.add_file?
.build?;
Environment Variables
Environment variables follow the pattern: TOPLEVEL_SECTION_KEY=value
# Set environment variables
use ;
let mut env_opts = with_top_level;
env_opts.add_section;
let config: MyConfig = new?
.add_file?
.add_env? // Environment overrides file config
.build?;
Configuration Priority
Sources added later override earlier sources:
new?
.add_str? // Priority: 1 (lowest)
.add_file? // Priority: 2
.add_file? // Priority: 3
.add_env? // Priority: 4 (highest)
.build?
Inline Configuration
let config: MyConfig = new?
.add_str?
.build?;
From Rust Structs
use Serialize;
let defaults = Defaults ;
let config: MyConfig = new?
.add_struct?
.add_file?
.build?;
Environment Variable Naming
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:
- Use underscores instead of dots in section names
- Hyphens in section names become underscores (e.g.,
my-app→MY_APP) - All keys are converted to lowercase
Examples
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 features
Run examples:
API Documentation
Full API documentation is available on docs.rs.
Error Handling
All operations return Result<T, ConfigError> with descriptive error messages:
match new?.add_file?.
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-8
Requirements
- Rust 1.75 or later
serdewithderivefeature for deserialization
License
Copyright © 2022-2026, Oxur Group
Apache License, Version 2.0
