# Configuration Examples
This directory contains examples demonstrating various features of the Config derive macro.
## Features Demonstrated
1. Basic Configuration
- Default values
- Type validation
- Required fields
2. Array Configuration
- String arrays
- Numeric arrays
- Default array values
- Empty arrays
3. Optional Values
- Optional strings
- Optional numbers with validation
- Default values for optional fields
4. Complex Configuration
- Combination of all features
- Optional arrays
- Multiple validations
- Environment-specific settings
## Running the Examples
1. First, ensure you have the required dependencies in your `Cargo.toml`:
```toml
[dependencies]
genies_derive = { path = "../" }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.0", features = ["full"] }
```
2. Run the examples:
```bash
cargo run --example config_examples
```
## Environment Variables
You can override any configuration value using environment variables. The environment variable names are automatically generated by converting the field names to SCREAMING_SNAKE_CASE.
Example environment variable setup:
```bash
# Basic Configuration
export HOST="production.example.com"
export PORT="443"
export APP_NAME="prod-service"
# Array Configuration
export TOPICS="prod/events,prod/logs,prod/metrics"
export NUMBERS="100,200,300"
export EMPTY_ARRAY=""
# Optional Configuration
export USERNAME="prod-admin"
export PASSWORD="prod-secret"
export TIMEOUT_SECONDS="3600"
# Complex Configuration
export APP_NAME="prod-app"
export ENVIRONMENT="production"
export ALLOWED_HOSTS="api.prod.com,admin.prod.com"
export PORTS="80,443,8443"
export CONNECTION_TIMEOUT="120"
export READ_TIMEOUT="900"
```
## Configuration Files
The examples use YAML configuration files located in the `config` directory:
- `basic.yml`: Basic configuration example
- `array.yml`: Array configuration example
- `optional.yml`: Optional values example
- `complex.yml`: Complex configuration example
## Features
1. Default Values
```rust
#[config(default = "localhost")]
pub host: String
```
2. Validation
```rust
#[config(validate(range(min = 1, max = 65535)))]
pub port: u16
```
3. Optional Fields
```rust
pub username: Option<String>
```
4. Arrays
```rust
#[config(default = "topic1,topic2,topic3")]
pub topics: Vec<String>
```
5. Optional Arrays
```rust
pub allowed_hosts: Option<Vec<String>>
```
6. Hot Reloading
```rust
let mut config = ComplexConfig::from_sources("config/complex.yml")?;
config.reload().await?;
```
## Priority Order
The configuration values are loaded in the following order (highest priority first):
1. Environment variables
2. Configuration file
3. Default values
4. None (for optional fields)