spicex 0.1.0

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
//! Integration tests for struct deserialization functionality

use serde::{Deserialize, Serialize};
use spicex::{ConfigValue, Spice};
use std::collections::HashMap;
use std::fs;
use tempfile::TempDir;

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct DatabaseConfig {
    host: String,
    port: u16,
    #[serde(default)]
    ssl: bool,
    #[serde(default = "default_timeout")]
    timeout: u32,
    credentials: Option<Credentials>,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Credentials {
    username: String,
    password: String,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct ServerConfig {
    name: String,
    port: u16,
    #[serde(default)]
    enabled: bool,
}

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct AppConfig {
    name: String,
    version: String,
    #[serde(default)]
    debug: bool,
    database: DatabaseConfig,
    servers: Vec<ServerConfig>,
    #[serde(default)]
    features: HashMap<String, bool>,
}

fn default_timeout() -> u32 {
    30
}

#[test]
fn test_complete_struct_deserialization() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    let config_content = r#"{
        "name": "test-app",
        "version": "1.0.0",
        "debug": true,
        "database": {
            "host": "localhost",
            "port": 5432,
            "ssl": true,
            "credentials": {
                "username": "admin",
                "password": "secret"
            }
        },
        "servers": [
            {
                "name": "web1",
                "port": 8080,
                "enabled": true
            },
            {
                "name": "web2",
                "port": 8081,
                "enabled": false
            }
        ],
        "features": {
            "auth": true,
            "logging": false,
            "metrics": true
        }
    }"#;

    let config_path = temp_dir.path().join("app_config.json");
    fs::write(&config_path, config_content).expect("Failed to write config file");

    let mut spice_instance = Spice::new();
    spice_instance.set_config_file(&config_path).unwrap();

    // Test complete deserialization
    let app_config: AppConfig = spice_instance.unmarshal().unwrap();

    assert_eq!(app_config.name, "test-app");
    assert_eq!(app_config.version, "1.0.0");
    assert_eq!(app_config.debug, true);

    // Test database config
    assert_eq!(app_config.database.host, "localhost");
    assert_eq!(app_config.database.port, 5432);
    assert_eq!(app_config.database.ssl, true);
    assert_eq!(app_config.database.timeout, 30); // default value

    // Test credentials
    let credentials = app_config.database.credentials.unwrap();
    assert_eq!(credentials.username, "admin");
    assert_eq!(credentials.password, "secret");

    // Test servers array
    assert_eq!(app_config.servers.len(), 2);
    assert_eq!(app_config.servers[0].name, "web1");
    assert_eq!(app_config.servers[0].port, 8080);
    assert_eq!(app_config.servers[0].enabled, true);
    assert_eq!(app_config.servers[1].name, "web2");
    assert_eq!(app_config.servers[1].enabled, false);

    // Test features map
    assert_eq!(app_config.features.get("auth"), Some(&true));
    assert_eq!(app_config.features.get("logging"), Some(&false));
    assert_eq!(app_config.features.get("metrics"), Some(&true));
}

#[test]
fn test_partial_struct_deserialization() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    let config_content = r#"{
        "name": "partial-app",
        "version": "2.0.0",
        "database": {
            "host": "db-server",
            "port": 3306
        },
        "servers": [],
        "extra_field": "should_be_ignored"
    }"#;

    let config_path = temp_dir.path().join("partial_config.json");
    fs::write(&config_path, config_content).expect("Failed to write config file");

    let mut spice_instance = Spice::new();
    spice_instance.set_config_file(&config_path).unwrap();

    // Test deserialization with missing and default fields
    let app_config: AppConfig = spice_instance.unmarshal().unwrap();

    assert_eq!(app_config.name, "partial-app");
    assert_eq!(app_config.version, "2.0.0");
    assert_eq!(app_config.debug, false); // default value

    // Test database with defaults
    assert_eq!(app_config.database.host, "db-server");
    assert_eq!(app_config.database.port, 3306);
    assert_eq!(app_config.database.ssl, false); // default value
    assert_eq!(app_config.database.timeout, 30); // default function
    assert!(app_config.database.credentials.is_none()); // optional field

    // Test empty servers array
    assert_eq!(app_config.servers.len(), 0);

    // Test empty features map (default)
    assert_eq!(app_config.features.len(), 0);
}

#[test]
fn test_struct_deserialization_with_multiple_sources() {
    // Clean up any existing environment variables
    std::env::remove_var("MYAPP_DATABASE_HOST");
    std::env::remove_var("MYAPP_DATABASE_SSL");
    std::env::remove_var("MYAPP_DEBUG");

    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    // Base config file
    let base_config = r#"{
        "name": "multi-source-app",
        "version": "1.0.0",
        "database": {
            "host": "localhost",
            "port": 5432
        },
        "servers": [
            {
                "name": "web1",
                "port": 8080
            }
        ]
    }"#;

    let base_path = temp_dir.path().join("base.json");
    fs::write(&base_path, base_config).expect("Failed to write base config");

    let mut spice_instance = Spice::new();

    // Set defaults
    spice_instance
        .set_default("debug", ConfigValue::from(false))
        .unwrap();
    spice_instance
        .set_default("database.ssl", ConfigValue::from(true))
        .unwrap();
    spice_instance
        .set_default("database.timeout", ConfigValue::from(60i64))
        .unwrap();

    // Load base config
    spice_instance.set_config_file(&base_path).unwrap();

    // Set explicit overrides
    spice_instance
        .set("database.host", ConfigValue::from("prod-db"))
        .unwrap();
    spice_instance
        .set("debug", ConfigValue::from(true))
        .unwrap();

    // Add features through explicit setting
    let mut features = HashMap::new();
    features.insert("auth".to_string(), ConfigValue::from(true));
    features.insert("logging".to_string(), ConfigValue::from(false));
    spice_instance
        .set("features", ConfigValue::Object(features))
        .unwrap();

    // Test deserialization with merged sources
    let app_config: AppConfig = spice_instance.unmarshal().unwrap();

    assert_eq!(app_config.name, "multi-source-app"); // from file
    assert_eq!(app_config.version, "1.0.0"); // from file
    assert_eq!(app_config.debug, true); // explicit override

    // Database config from multiple sources
    assert_eq!(app_config.database.host, "prod-db"); // explicit override (highest precedence)
    assert_eq!(app_config.database.port, 5432); // from file
    assert_eq!(app_config.database.ssl, true); // from default
    assert_eq!(app_config.database.timeout, 60); // from default

    // Servers from file
    assert_eq!(app_config.servers.len(), 1);
    assert_eq!(app_config.servers[0].name, "web1");
    assert_eq!(app_config.servers[0].port, 8080);
    assert_eq!(app_config.servers[0].enabled, false); // default

    // Features from explicit setting
    assert_eq!(app_config.features.get("auth"), Some(&true));
    assert_eq!(app_config.features.get("logging"), Some(&false));
}

#[test]
fn test_unmarshal_key_specific_section() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    let config_content = r#"{
        "app": {
            "name": "section-test",
            "version": "1.0.0"
        },
        "database": {
            "host": "db-host",
            "port": 5432,
            "ssl": false,
            "credentials": {
                "username": "dbuser",
                "password": "dbpass"
            }
        },
        "other_section": {
            "value": "ignored"
        }
    }"#;

    let config_path = temp_dir.path().join("section_config.json");
    fs::write(&config_path, config_content).expect("Failed to write config file");

    let mut spice_instance = Spice::new();
    spice_instance.set_config_file(&config_path).unwrap();

    // Test unmarshaling specific section
    let db_config: DatabaseConfig = spice_instance.unmarshal_key("database").unwrap();

    assert_eq!(db_config.host, "db-host");
    assert_eq!(db_config.port, 5432);
    assert_eq!(db_config.ssl, false);
    assert_eq!(db_config.timeout, 30); // default value

    let credentials = db_config.credentials.unwrap();
    assert_eq!(credentials.username, "dbuser");
    assert_eq!(credentials.password, "dbpass");
}

#[test]
fn test_struct_deserialization_with_environment_overrides() {
    use std::env;

    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    let config_content = r#"{
        "name": "env-test-app",
        "version": "1.0.0",
        "database": {
            "host": "localhost",
            "port": 5432,
            "ssl": false
        },
        "servers": [
            {
                "name": "web1",
                "port": 8080,
                "enabled": true
            }
        ]
    }"#;

    let config_path = temp_dir.path().join("env_config.json");
    fs::write(&config_path, config_content).expect("Failed to write config file");

    // Clean up any existing environment variables first
    env::remove_var("MYAPP_DATABASE_HOST");
    env::remove_var("MYAPP_DATABASE_SSL");
    env::remove_var("MYAPP_DEBUG");

    // Set environment variables
    env::set_var("MYAPP_DATABASE_HOST", "env-db-host");
    env::set_var("MYAPP_DATABASE_SSL", "true");
    env::set_var("MYAPP_DEBUG", "true");

    let mut spice_instance = Spice::new();
    spice_instance.set_config_file(&config_path).unwrap();

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

    // Test deserialization with environment overrides
    let app_config: AppConfig = spice_instance.unmarshal().unwrap();

    assert_eq!(app_config.name, "env-test-app"); // from file
    assert_eq!(app_config.debug, true); // from environment

    // Database config with environment overrides
    assert_eq!(app_config.database.host, "env-db-host"); // from environment
    assert_eq!(app_config.database.port, 5432); // from file
    assert_eq!(app_config.database.ssl, true); // from environment

    // Clean up environment variables
    env::remove_var("MYAPP_DATABASE_HOST");
    env::remove_var("MYAPP_DATABASE_SSL");
    env::remove_var("MYAPP_DEBUG");
}

#[derive(Debug, Deserialize, PartialEq)]
struct NestedArrayConfig {
    groups: Vec<Group>,
}

#[derive(Debug, Deserialize, PartialEq)]
struct Group {
    name: String,
    members: Vec<Member>,
}

#[derive(Debug, Deserialize, PartialEq)]
struct Member {
    id: u32,
    name: String,
    #[serde(default)]
    active: bool,
}

#[test]
fn test_complex_nested_array_deserialization() {
    let temp_dir = TempDir::new().expect("Failed to create temp directory");

    let config_content = r#"{
        "groups": [
            {
                "name": "admins",
                "members": [
                    {
                        "id": 1,
                        "name": "Alice",
                        "active": true
                    },
                    {
                        "id": 2,
                        "name": "Bob"
                    }
                ]
            },
            {
                "name": "users",
                "members": [
                    {
                        "id": 3,
                        "name": "Charlie",
                        "active": false
                    }
                ]
            }
        ]
    }"#;

    let config_path = temp_dir.path().join("nested_config.json");
    fs::write(&config_path, config_content).expect("Failed to write config file");

    let mut spice_instance = Spice::new();
    spice_instance.set_config_file(&config_path).unwrap();

    // Test complex nested array deserialization
    let config: NestedArrayConfig = spice_instance.unmarshal().unwrap();

    assert_eq!(config.groups.len(), 2);

    // Test first group
    let admin_group = &config.groups[0];
    assert_eq!(admin_group.name, "admins");
    assert_eq!(admin_group.members.len(), 2);
    assert_eq!(admin_group.members[0].id, 1);
    assert_eq!(admin_group.members[0].name, "Alice");
    assert_eq!(admin_group.members[0].active, true);
    assert_eq!(admin_group.members[1].id, 2);
    assert_eq!(admin_group.members[1].name, "Bob");
    assert_eq!(admin_group.members[1].active, false); // default value

    // Test second group
    let user_group = &config.groups[1];
    assert_eq!(user_group.name, "users");
    assert_eq!(user_group.members.len(), 1);
    assert_eq!(user_group.members[0].id, 3);
    assert_eq!(user_group.members[0].name, "Charlie");
    assert_eq!(user_group.members[0].active, false);
}