SpiceX
A complete configuration solution for Rust applications, inspired by viper.
SpiceX is designed to work within an application and can handle all types of configuration needs and formats. It provides a unified interface for reading configuration from multiple sources with a clear precedence hierarchy.
Features
- ✅ Multiple Configuration Sources - Files, environment variables, command line flags, defaults
- ✅ Multiple File Formats - JSON, YAML, TOML, INI support
- ✅ Precedence Hierarchy - Clear ordering of configuration sources
- ✅ Nested Configuration - Dot notation access to nested values
- ✅ Type Safety - Strong typing with automatic type conversion
- ✅ Struct Deserialization - Deserialize configuration into Rust structs
- ✅ File Watching - Automatic reloading when configuration files change
- ✅ Environment Variables - Automatic mapping with prefix support
- ✅ Command Line Flags - Integration with clap for CLI arguments
- ✅ Default Values - Fallback values for missing configuration
- ✅ Configuration Writing - Save configuration back to files
Quick Start
Add this to your Cargo.toml:
[]
= "0.1.0"
# Optional: Enable CLI support
[]
= "0.1.0"
= ["cli"]
Basic Usage
use ;
With Struct Deserialization
use ;
use Deserialize;
Configuration Precedence
Spice uses the following precedence order (highest to lowest):
- Explicit calls - Values set via
spice.set() - Command line flags - CLI arguments (requires
clifeature) - Environment variables - System environment variables
- Configuration files - JSON, YAML, TOML, INI files
- Key/value stores - Remote configuration (future feature)
- Default values - Fallback values set via
spice.set_default()
Configuration File Formats
JSON Example (config.json)
YAML Example (config.yaml)
database:
host: localhost
port: 5432
ssl: true
credentials:
username: admin
password: secret
server:
port: 8080
host: 0.0.0.0
features:
- auth
- logging
- metrics
debug: false
TOML Example (config.toml)
= false
= ["auth", "logging", "metrics"]
[]
= "localhost"
= 5432
= true
[]
= "admin"
= "secret"
[]
= 8080
= "0.0.0.0"
INI Example (config.ini)
debug = false
[database]
host = localhost
port = 5432
ssl = true
[server]
port = 8080
host = 0.0.0.0
Environment Variables
Environment variables are automatically mapped to configuration keys:
# Set environment variables
# These become available as:
# database.host = "production-db"
# database.port = 5432
# debug = true
use Spice;
let mut spice = new;
spice.set_env_prefix;
spice.set_automatic_env;
// Access environment variables
let host = spice.get_string?;
let debug = spice.get_bool?;
Command Line Flags
With the cli feature enabled, you can integrate with clap:
use Spice;
use ;
File Watching
Enable automatic reloading when configuration files change:
use Spice;
use ;
Writing Configuration
Save current configuration to files:
use ;
Advanced Usage
Sub-configurations
Work with configuration subsections:
use ;
use HashMap;
let mut spice = new;
// Set up nested configuration
let mut db_config = new;
db_config.insert;
db_config.insert;
spice.set?;
// Create sub-configuration for database settings
if let Some = spice.sub?
Configuration Validation
Validate configuration during deserialization:
use ;
use Deserialize;
let mut spice = new;
spice.set?;
spice.set?;
let config: ServerConfig = spice.unmarshal_with_validation?;
Error Handling
Spice provides detailed error information:
use ;
let spice = new;
match spice.get_string
Migration from Other Libraries
From config crate
// Old way (config crate)
use ;
let settings = builder
.add_source
.build?;
let host: String = settings.get?;
// New way (spice)
use Spice;
let mut spice = new;
spice.set_config_name;
spice.read_in_config?;
let host = spice.get_string?.unwrap_or_default;
From Environment Variables Only
// Old way (std::env)
use env;
let host = var.unwrap_or_else;
let port: u16 = var
.unwrap_or_else
.parse
.unwrap_or;
// New way (spice)
use ;
let mut spice = new;
spice.set_default?;
spice.set_default?;
spice.set_env_prefix;
spice.set_automatic_env;
let host = spice.get_string?.unwrap_or_default;
let port = spice.get_i64?.unwrap_or as u16;
Examples
The examples/ directory contains comprehensive examples:
basic_usage.rs- Basic configuration loading and accessstruct_deserialization.rs- Deserializing into structsenv_layer_usage.rs- Environment variable configurationfile_watching.rs- Watching for configuration changescli_flag_usage.rs- Command line flag integrationnested_access_usage.rs- Working with nested configurationdefault_values_usage.rs- Setting and using defaultsfile_discovery_usage.rs- Automatic file discoveryweb_server_config.rs- Real-world web server configurationmicroservice_config.rs- Microservice configuration patterns
Performance
Spice-rust is designed for performance:
- Lazy Loading - Configuration sources are loaded on-demand
- Caching - Values are cached after first access
- Zero-Copy - Minimal allocations through strategic use of references
- Efficient Parsing - Uses optimized parsers for each format
Run benchmarks with:
Contributing
Contributions are welcome! Please see our Contributing Guide for details.
Development Setup
Running Examples
# Basic usage
# With CLI support
# File watching (requires a config file)
License
- MIT license (LICENSE-MIT)
at your option.
Acknowledgments
- Inspired by viper for Go
- Built with the excellent Rust ecosystem including serde, clap, notify, and more