spicex 0.1.2

A complete configuration solution for Rust applications, inspired by Viper
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
//! Real-world example: Web server configuration
//!
//! This example demonstrates how to configure a web server application using
//! multiple configuration sources with proper precedence and validation.

use serde::{Deserialize, Serialize};
use spicex::{ConfigValue, EnvConfigLayer, Spice};
use std::collections::HashMap;
use std::env;
use std::fs;
use std::path::PathBuf;

#[derive(Debug, Deserialize, Serialize, Clone)]
struct ServerConfig {
    host: String,
    port: u16,
    #[serde(default = "default_workers")]
    workers: u32,
    #[serde(default)]
    ssl: SslConfig,
    #[serde(default)]
    timeouts: TimeoutConfig,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct SslConfig {
    #[serde(default)]
    enabled: bool,
    cert_file: Option<String>,
    key_file: Option<String>,
}

impl Default for SslConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            cert_file: None,
            key_file: None,
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct TimeoutConfig {
    #[serde(default = "default_read_timeout")]
    read: u64,
    #[serde(default = "default_write_timeout")]
    write: u64,
    #[serde(default = "default_idle_timeout")]
    idle: u64,
}

impl Default for TimeoutConfig {
    fn default() -> Self {
        Self {
            read: default_read_timeout(),
            write: default_write_timeout(),
            idle: default_idle_timeout(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct DatabaseConfig {
    host: String,
    port: u16,
    database: String,
    username: String,
    password: String,
    #[serde(default = "default_max_connections")]
    max_connections: u32,
    #[serde(default = "default_connection_timeout")]
    connection_timeout: u64,
    #[serde(default)]
    ssl_mode: String,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct LoggingConfig {
    #[serde(default = "default_log_level")]
    level: String,
    #[serde(default)]
    format: String,
    file: Option<String>,
    #[serde(default)]
    rotation: LogRotationConfig,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct LogRotationConfig {
    #[serde(default)]
    enabled: bool,
    #[serde(default = "default_max_size")]
    max_size: String,
    #[serde(default = "default_max_files")]
    max_files: u32,
}

impl Default for LogRotationConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            max_size: default_max_size(),
            max_files: default_max_files(),
        }
    }
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct RedisConfig {
    host: String,
    port: u16,
    #[serde(default)]
    password: Option<String>,
    #[serde(default)]
    database: u32,
    #[serde(default = "default_pool_size")]
    pool_size: u32,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct AppConfig {
    #[serde(default = "default_app_name")]
    name: String,
    #[serde(default = "default_version")]
    version: String,
    #[serde(default)]
    debug: bool,
    #[serde(default = "default_environment")]
    environment: String,

    server: ServerConfig,
    database: DatabaseConfig,
    #[serde(default)]
    logging: LoggingConfig,
    redis: Option<RedisConfig>,

    #[serde(default)]
    features: HashMap<String, bool>,
    #[serde(default)]
    metrics: MetricsConfig,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
struct MetricsConfig {
    #[serde(default)]
    enabled: bool,
    #[serde(default = "default_metrics_port")]
    port: u16,
    #[serde(default = "default_metrics_path")]
    path: String,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            enabled: false,
            port: default_metrics_port(),
            path: default_metrics_path(),
        }
    }
}

impl Default for LoggingConfig {
    fn default() -> Self {
        Self {
            level: default_log_level(),
            format: "json".to_string(),
            file: None,
            rotation: LogRotationConfig::default(),
        }
    }
}

// Default value functions
fn default_workers() -> u32 {
    4
}
fn default_read_timeout() -> u64 {
    30
}
fn default_write_timeout() -> u64 {
    30
}
fn default_idle_timeout() -> u64 {
    120
}
fn default_max_connections() -> u32 {
    100
}
fn default_connection_timeout() -> u64 {
    30
}
fn default_log_level() -> String {
    "info".to_string()
}
fn default_max_size() -> String {
    "100MB".to_string()
}
fn default_max_files() -> u32 {
    10
}
fn default_pool_size() -> u32 {
    10
}
fn default_app_name() -> String {
    "web-server".to_string()
}
fn default_version() -> String {
    "1.0.0".to_string()
}
fn default_environment() -> String {
    "development".to_string()
}
fn default_metrics_port() -> u16 {
    9090
}
fn default_metrics_path() -> String {
    "/metrics".to_string()
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("🚀 Web Server Configuration Example");
    println!("===================================");

    // Create a comprehensive configuration setup
    let mut spice_instance = Spice::new();

    // 1. Set up defaults for all configuration values
    setup_defaults(&mut spice_instance)?;

    // 2. Try to load configuration files in order of precedence
    load_config_files(&mut spice_instance)?;

    // 3. Set up environment variable bindings
    setup_environment_variables(&mut spice_instance)?;

    // 4. Override with command line arguments (if any)
    // In a real application, you would parse command line args here

    // 5. Validate and load the complete configuration
    let config: AppConfig = spice_instance.unmarshal()?;

    // 6. Display the final configuration
    display_configuration(&config);

    // 7. Demonstrate accessing individual configuration values
    demonstrate_individual_access(&mut spice_instance)?;

    // 8. Show configuration watching (if files are available)
    demonstrate_config_watching(&mut spice_instance)?;

    println!("\n✅ Web server configuration example completed successfully!");
    Ok(())
}

fn setup_defaults(spice_instance: &mut Spice) -> Result<(), Box<dyn std::error::Error>> {
    println!("\n📋 Setting up default configuration values...");

    // Server defaults
    spice_instance.set_default("server.host", ConfigValue::from("localhost"))?;
    spice_instance.set_default("server.port", ConfigValue::from(8080i64))?;
    spice_instance.set_default("server.workers", ConfigValue::from(4i64))?;
    spice_instance.set_default("server.ssl.enabled", ConfigValue::from(false))?;
    spice_instance.set_default("server.timeouts.read", ConfigValue::from(30i64))?;
    spice_instance.set_default("server.timeouts.write", ConfigValue::from(30i64))?;
    spice_instance.set_default("server.timeouts.idle", ConfigValue::from(120i64))?;

    // Database defaults
    spice_instance.set_default("database.host", ConfigValue::from("localhost"))?;
    spice_instance.set_default("database.port", ConfigValue::from(5432i64))?;
    spice_instance.set_default("database.database", ConfigValue::from("myapp"))?;
    spice_instance.set_default("database.username", ConfigValue::from("postgres"))?;
    spice_instance.set_default("database.password", ConfigValue::from("password"))?;
    spice_instance.set_default("database.max_connections", ConfigValue::from(100i64))?;
    spice_instance.set_default("database.connection_timeout", ConfigValue::from(30i64))?;
    spice_instance.set_default("database.ssl_mode", ConfigValue::from("prefer"))?;

    // Logging defaults
    spice_instance.set_default("logging.level", ConfigValue::from("info"))?;
    spice_instance.set_default("logging.format", ConfigValue::from("json"))?;
    spice_instance.set_default("logging.rotation.enabled", ConfigValue::from(false))?;
    spice_instance.set_default("logging.rotation.max_size", ConfigValue::from("100MB"))?;
    spice_instance.set_default("logging.rotation.max_files", ConfigValue::from(10i64))?;

    // Application defaults
    spice_instance.set_default("name", ConfigValue::from("web-server"))?;
    spice_instance.set_default("version", ConfigValue::from("1.0.0"))?;
    spice_instance.set_default("debug", ConfigValue::from(false))?;
    spice_instance.set_default("environment", ConfigValue::from("development"))?;

    // Metrics defaults
    spice_instance.set_default("metrics.enabled", ConfigValue::from(false))?;
    spice_instance.set_default("metrics.port", ConfigValue::from(9090i64))?;
    spice_instance.set_default("metrics.path", ConfigValue::from("/metrics"))?;

    // Feature flags defaults
    spice_instance.set_default("features.auth", ConfigValue::from(true))?;
    spice_instance.set_default("features.rate_limiting", ConfigValue::from(false))?;
    spice_instance.set_default("features.caching", ConfigValue::from(true))?;

    println!("   ✓ Default values configured");
    Ok(())
}

fn load_config_files(spice_instance: &mut Spice) -> Result<(), Box<dyn std::error::Error>> {
    println!("\n📁 Loading configuration files...");

    // Set up configuration search paths
    spice_instance.add_config_path(".");
    spice_instance.add_config_path("./config");
    spice_instance.add_config_path("/etc/webserver");

    // Try to load environment-specific config
    let env = env::var("ENVIRONMENT").unwrap_or_else(|_| "development".to_string());

    // Try to load base configuration
    spice_instance.set_config_name("config");
    match spice_instance.read_in_config() {
        Ok(()) => println!("   ✓ Loaded base configuration file"),
        Err(_) => {
            println!("   ⚠ No base configuration file found, using defaults");
            create_sample_config_file()?;
        }
    }

    // Try to load environment-specific configuration
    spice_instance.set_config_name(&format!("config.{}", env));
    match spice_instance.read_in_config() {
        Ok(()) => println!("   ✓ Loaded {}-specific configuration", env),
        Err(_) => println!("   ⚠ No {}-specific configuration found", env),
    }

    Ok(())
}

fn setup_environment_variables(
    spice_instance: &mut Spice,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("\n🌍 Setting up environment variable bindings...");

    // Add environment layer with APP prefix
    let env_layer = EnvConfigLayer::new(Some("APP".to_string()), true);
    spice_instance.add_layer(Box::new(env_layer));

    // Show some example environment variables that would be recognized
    println!("   Environment variables that will be recognized:");
    println!("   - APP_SERVER_HOST -> server.host");
    println!("   - APP_SERVER_PORT -> server.port");
    println!("   - APP_DATABASE_HOST -> database.host");
    println!("   - APP_DATABASE_PASSWORD -> database.password");
    println!("   - APP_DEBUG -> debug");
    println!("   - APP_ENVIRONMENT -> environment");

    // Check if any relevant environment variables are set
    let env_vars = [
        "APP_SERVER_HOST",
        "APP_SERVER_PORT",
        "APP_DATABASE_HOST",
        "APP_DATABASE_PASSWORD",
        "APP_DEBUG",
        "APP_ENVIRONMENT",
    ];

    let mut found_vars = Vec::new();
    for var in &env_vars {
        if let Ok(value) = env::var(var) {
            found_vars.push(format!("{}={}", var, value));
        }
    }

    if found_vars.is_empty() {
        println!("   ⚠ No relevant environment variables found");
    } else {
        println!("   ✓ Found environment variables:");
        for var in found_vars {
            println!("     - {}", var);
        }
    }

    Ok(())
}

fn display_configuration(config: &AppConfig) {
    println!("\n🔧 Final Configuration:");
    println!("======================");

    println!("Application:");
    println!("  Name: {}", config.name);
    println!("  Version: {}", config.version);
    println!("  Environment: {}", config.environment);
    println!("  Debug: {}", config.debug);

    println!("\nServer:");
    println!("  Host: {}", config.server.host);
    println!("  Port: {}", config.server.port);
    println!("  Workers: {}", config.server.workers);
    println!("  SSL Enabled: {}", config.server.ssl.enabled);
    if let Some(cert) = &config.server.ssl.cert_file {
        println!("  SSL Cert: {}", cert);
    }
    println!(
        "  Timeouts: read={}s, write={}s, idle={}s",
        config.server.timeouts.read, config.server.timeouts.write, config.server.timeouts.idle
    );

    println!("\nDatabase:");
    println!("  Host: {}", config.database.host);
    println!("  Port: {}", config.database.port);
    println!("  Database: {}", config.database.database);
    println!("  Username: {}", config.database.username);
    println!("  Password: [REDACTED]");
    println!("  Max Connections: {}", config.database.max_connections);
    println!("  SSL Mode: {}", config.database.ssl_mode);

    println!("\nLogging:");
    println!("  Level: {}", config.logging.level);
    println!("  Format: {}", config.logging.format);
    if let Some(file) = &config.logging.file {
        println!("  File: {}", file);
    }
    println!(
        "  Rotation: enabled={}, max_size={}, max_files={}",
        config.logging.rotation.enabled,
        config.logging.rotation.max_size,
        config.logging.rotation.max_files
    );

    if let Some(redis) = &config.redis {
        println!("\nRedis:");
        println!("  Host: {}", redis.host);
        println!("  Port: {}", redis.port);
        println!("  Database: {}", redis.database);
        println!("  Pool Size: {}", redis.pool_size);
    }

    println!("\nMetrics:");
    println!("  Enabled: {}", config.metrics.enabled);
    if config.metrics.enabled {
        println!("  Port: {}", config.metrics.port);
        println!("  Path: {}", config.metrics.path);
    }

    if !config.features.is_empty() {
        println!("\nFeature Flags:");
        for (feature, enabled) in &config.features {
            println!("  {}: {}", feature, enabled);
        }
    }
}

fn demonstrate_individual_access(spice_instance: &mut Spice) -> Result<(), Box<dyn std::error::Error>> {
    println!("\n🔍 Demonstrating individual configuration access:");
    println!("================================================");

    // Access individual values with type conversion
    if let Some(host) = spice_instance.get_string("server.host")? {
        println!("Server host: {}", host);
    }

    if let Some(port) = spice_instance.get_int("server.port")? {
        println!("Server port: {}", port);
    }

    if let Some(debug) = spice_instance.get_bool("debug")? {
        println!("Debug mode: {}", debug);
    }

    // Access nested values
    if let Some(db_host) = spice_instance.get_string("database.host")? {
        println!("Database host: {}", db_host);
    }

    // Access with fallback
    let log_level = spice_instance
        .get_string("logging.level")?
        .unwrap_or_else(|| "info".to_string());
    println!("Log level: {}", log_level);

    // Check if optional sections exist
    if spice_instance.get("redis").is_ok() {
        println!("Redis configuration is available");
    } else {
        println!("Redis configuration is not configured");
    }

    Ok(())
}

fn demonstrate_config_watching(
    spice_instance: &mut Spice,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("\n👀 Configuration watching setup:");
    println!("================================");

    // In a real application, you would set up file watching like this:
    match spice_instance.watch_config() {
        Ok(()) => {
            println!("✓ Configuration file watching enabled");

            // Set up a callback for configuration changes
            spice_instance.on_config_change(Box::new(|| {
                println!("🔄 Configuration file changed! Reloading...");
                // In a real app, you might want to reload specific components
            }))?;

            println!("✓ Change callback registered");
        }
        Err(e) => {
            println!("⚠ Could not enable file watching: {}", e);
        }
    }

    Ok(())
}

fn create_sample_config_file() -> Result<(), Box<dyn std::error::Error>> {
    let sample_config = r#"{
  "name": "my-web-server",
  "version": "1.2.0",
  "environment": "production",
  "debug": false,
  "server": {
    "host": "0.0.0.0",
    "port": 8080,
    "workers": 8,
    "ssl": {
      "enabled": true,
      "cert_file": "/etc/ssl/server.crt",
      "key_file": "/etc/ssl/server.key"
    },
    "timeouts": {
      "read": 60,
      "write": 60,
      "idle": 300
    }
  },
  "database": {
    "host": "db.example.com",
    "port": 5432,
    "database": "production_db",
    "username": "app_user",
    "password": "secure_password",
    "max_connections": 200,
    "connection_timeout": 30,
    "ssl_mode": "require"
  },
  "logging": {
    "level": "warn",
    "format": "json",
    "file": "/var/log/webserver.log",
    "rotation": {
      "enabled": true,
      "max_size": "500MB",
      "max_files": 30
    }
  },
  "redis": {
    "host": "redis.example.com",
    "port": 6379,
    "database": 0,
    "pool_size": 20
  },
  "metrics": {
    "enabled": true,
    "port": 9090,
    "path": "/metrics"
  },
  "features": {
    "auth": true,
    "rate_limiting": true,
    "caching": true,
    "analytics": false
  }
}"#;

    fs::write("config.json", sample_config)?;
    println!("   ✓ Created sample config.json file");
    Ok(())
}