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
//! Configuration layer abstractions and priority management.
use crate::error::ConfigResult;
use crate::value::ConfigValue;
/// Trait for configuration layers that provide key-value access.
pub trait ConfigLayer: Send + Sync {
/// Gets a configuration value by key.
fn get(&self, key: &str) -> ConfigResult<Option<ConfigValue>>;
/// Sets a configuration value by key.
fn set(&mut self, key: &str, value: ConfigValue) -> ConfigResult<()>;
/// Returns all available keys in this layer.
fn keys(&self) -> Vec<String>;
/// Returns a human-readable name for this configuration source.
fn source_name(&self) -> &str;
/// Returns the priority of this layer for precedence resolution.
fn priority(&self) -> LayerPriority;
/// Returns a reference to the layer as Any for downcasting.
fn as_any(&self) -> &dyn std::any::Any;
/// Returns a mutable reference to the layer as Any for downcasting.
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}
/// Priority levels for configuration layers.
/// Lower numeric values have higher precedence.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LayerPriority {
/// Explicit set() calls - highest precedence
Explicit = 0,
/// Command line flags
Flags = 1,
/// Environment variables
Environment = 2,
/// Configuration files
ConfigFile = 3,
/// Remote key-value stores
KeyValue = 4,
/// Default values - lowest precedence
Defaults = 5,
}
impl LayerPriority {
/// Returns a human-readable description of the priority level.
pub fn description(&self) -> &'static str {
match self {
LayerPriority::Explicit => "Explicit calls",
LayerPriority::Flags => "Command line flags",
LayerPriority::Environment => "Environment variables",
LayerPriority::ConfigFile => "Configuration files",
LayerPriority::KeyValue => "Key-value stores",
LayerPriority::Defaults => "Default values",
}
}
}
/// Layer management utilities for sorting and merging configuration layers.
pub mod utils {
use super::*;
use std::collections::HashMap;
/// Sorts configuration layers by priority (highest precedence first).
///
/// # Arguments
/// * `layers` - A mutable slice of configuration layers to sort
///
/// # Example
/// ```
/// use spice::layer::{ConfigLayer, LayerPriority, utils::sort_layers_by_priority};
/// use spice::value::ConfigValue;
/// use spice::error::ConfigResult;
/// use std::collections::HashMap;
///
/// // Mock layer for example
/// struct MockLayer { priority: LayerPriority }
/// impl ConfigLayer for MockLayer {
/// fn get(&self, _key: &str) -> ConfigResult<Option<ConfigValue>> { Ok(None) }
/// fn set(&mut self, _key: &str, _value: ConfigValue) -> ConfigResult<()> { Ok(()) }
/// fn keys(&self) -> Vec<String> { vec![] }
/// fn source_name(&self) -> &str { "mock" }
/// fn priority(&self) -> LayerPriority { self.priority }
/// fn as_any(&self) -> &dyn std::any::Any { self }
/// fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
/// }
///
/// let mut layers: Vec<Box<dyn ConfigLayer>> = vec![
/// Box::new(MockLayer { priority: LayerPriority::Defaults }),
/// Box::new(MockLayer { priority: LayerPriority::Explicit }),
/// ];
/// // layers will be sorted with highest precedence (lowest numeric value) first
/// sort_layers_by_priority(&mut layers);
/// assert_eq!(layers[0].priority(), LayerPriority::Explicit);
/// ```
pub fn sort_layers_by_priority(layers: &mut [Box<dyn ConfigLayer>]) {
layers.sort_by_key(|a| a.priority());
}
/// Merges configuration values from multiple layers according to precedence.
/// Returns the first non-None value found when searching layers in priority order.
///
/// # Arguments
/// * `layers` - Slice of configuration layers sorted by priority (highest first)
/// * `key` - The configuration key to search for
///
/// # Returns
/// * `ConfigResult<Option<ConfigValue>>` - The merged value or None if not found
///
/// # Example
/// ```
/// use spice::layer::{ConfigLayer, LayerPriority, utils::merge_value_from_layers};
/// use spice::value::ConfigValue;
/// use spice::error::ConfigResult;
/// use std::collections::HashMap;
///
/// // Mock layer for example
/// struct MockLayer { data: HashMap<String, ConfigValue> }
/// impl ConfigLayer for MockLayer {
/// fn get(&self, key: &str) -> ConfigResult<Option<ConfigValue>> {
/// Ok(self.data.get(key).cloned())
/// }
/// fn set(&mut self, _key: &str, _value: ConfigValue) -> ConfigResult<()> { Ok(()) }
/// fn keys(&self) -> Vec<String> { self.data.keys().cloned().collect() }
/// fn source_name(&self) -> &str { "mock" }
/// fn priority(&self) -> LayerPriority { LayerPriority::ConfigFile }
/// fn as_any(&self) -> &dyn std::any::Any { self }
/// fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
/// }
///
/// let mut data = HashMap::new();
/// data.insert("database.host".to_string(), ConfigValue::String("localhost".to_string()));
/// let layers: Vec<Box<dyn ConfigLayer>> = vec![Box::new(MockLayer { data })];
/// let value = merge_value_from_layers(&layers, "database.host").unwrap();
/// assert_eq!(value, Some(ConfigValue::String("localhost".to_string())));
/// ```
pub fn merge_value_from_layers(
layers: &[Box<dyn ConfigLayer>],
key: &str,
) -> ConfigResult<Option<ConfigValue>> {
for layer in layers {
match layer.get(key)? {
Some(value) => return Ok(Some(value)),
None => continue,
}
}
Ok(None)
}
/// Collects all unique keys from multiple configuration layers.
///
/// # Arguments
/// * `layers` - Slice of configuration layers
///
/// # Returns
/// * `Vec<String>` - Vector of all unique keys across all layers
///
/// # Example
/// ```
/// use spice::layer::{ConfigLayer, LayerPriority, utils::collect_all_keys};
/// use spice::value::ConfigValue;
/// use spice::error::ConfigResult;
/// use std::collections::HashMap;
///
/// // Mock layer for example
/// struct MockLayer { keys: Vec<String> }
/// impl ConfigLayer for MockLayer {
/// fn get(&self, _key: &str) -> ConfigResult<Option<ConfigValue>> { Ok(None) }
/// fn set(&mut self, _key: &str, _value: ConfigValue) -> ConfigResult<()> { Ok(()) }
/// fn keys(&self) -> Vec<String> { self.keys.clone() }
/// fn source_name(&self) -> &str { "mock" }
/// fn priority(&self) -> LayerPriority { LayerPriority::ConfigFile }
/// fn as_any(&self) -> &dyn std::any::Any { self }
/// fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
/// }
///
/// let layers: Vec<Box<dyn ConfigLayer>> = vec![
/// Box::new(MockLayer { keys: vec!["key1".to_string(), "key2".to_string()] }),
/// Box::new(MockLayer { keys: vec!["key2".to_string(), "key3".to_string()] }),
/// ];
/// let all_keys = collect_all_keys(&layers);
/// assert_eq!(all_keys.len(), 3); // key1, key2, key3
/// ```
pub fn collect_all_keys(layers: &[Box<dyn ConfigLayer>]) -> Vec<String> {
let mut all_keys = std::collections::HashSet::new();
for layer in layers {
for key in layer.keys() {
all_keys.insert(key);
}
}
let mut keys: Vec<String> = all_keys.into_iter().collect();
keys.sort();
keys
}
/// Creates a merged view of all configuration values from multiple layers.
/// Values from higher precedence layers override those from lower precedence layers.
///
/// # Arguments
/// * `layers` - Slice of configuration layers sorted by priority (highest first)
///
/// # Returns
/// * `ConfigResult<HashMap<String, ConfigValue>>` - Merged configuration map
///
/// # Example
/// ```
/// use spice::layer::{ConfigLayer, LayerPriority, utils::merge_all_layers};
/// use spice::value::ConfigValue;
/// use spice::error::ConfigResult;
/// use std::collections::HashMap;
///
/// // Mock layer for example
/// struct MockLayer { data: HashMap<String, ConfigValue> }
/// impl ConfigLayer for MockLayer {
/// fn get(&self, key: &str) -> ConfigResult<Option<ConfigValue>> {
/// Ok(self.data.get(key).cloned())
/// }
/// fn set(&mut self, _key: &str, _value: ConfigValue) -> ConfigResult<()> { Ok(()) }
/// fn keys(&self) -> Vec<String> { self.data.keys().cloned().collect() }
/// fn source_name(&self) -> &str { "mock" }
/// fn priority(&self) -> LayerPriority { LayerPriority::ConfigFile }
/// fn as_any(&self) -> &dyn std::any::Any { self }
/// fn as_any_mut(&mut self) -> &mut dyn std::any::Any { self }
/// }
///
/// let mut data = HashMap::new();
/// data.insert("key1".to_string(), ConfigValue::String("value1".to_string()));
/// let layers: Vec<Box<dyn ConfigLayer>> = vec![Box::new(MockLayer { data })];
/// let merged_config = merge_all_layers(&layers).unwrap();
/// assert_eq!(merged_config.len(), 1);
/// ```
pub fn merge_all_layers(
layers: &[Box<dyn ConfigLayer>],
) -> ConfigResult<HashMap<String, ConfigValue>> {
let mut merged = HashMap::new();
let all_keys = collect_all_keys(layers);
for key in all_keys {
if let Some(value) = merge_value_from_layers(layers, &key)? {
merged.insert(key, value);
}
}
Ok(merged)
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashMap;
// Mock implementation for testing
struct MockConfigLayer {
data: HashMap<String, ConfigValue>,
priority: LayerPriority,
name: String,
}
impl MockConfigLayer {
fn new(name: &str, priority: LayerPriority) -> Self {
Self {
data: HashMap::new(),
priority,
name: name.to_string(),
}
}
fn with_value(mut self, key: &str, value: ConfigValue) -> Self {
self.data.insert(key.to_string(), value);
self
}
}
impl ConfigLayer for MockConfigLayer {
fn get(&self, key: &str) -> ConfigResult<Option<ConfigValue>> {
Ok(self.data.get(key).cloned())
}
fn set(&mut self, key: &str, value: ConfigValue) -> ConfigResult<()> {
self.data.insert(key.to_string(), value);
Ok(())
}
fn keys(&self) -> Vec<String> {
self.data.keys().cloned().collect()
}
fn source_name(&self) -> &str {
&self.name
}
fn priority(&self) -> LayerPriority {
self.priority
}
fn as_any(&self) -> &dyn std::any::Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
}
#[test]
fn test_priority_ordering() {
assert!(LayerPriority::Explicit < LayerPriority::Flags);
assert!(LayerPriority::Flags < LayerPriority::Environment);
assert!(LayerPriority::Environment < LayerPriority::ConfigFile);
assert!(LayerPriority::ConfigFile < LayerPriority::KeyValue);
assert!(LayerPriority::KeyValue < LayerPriority::Defaults);
}
#[test]
fn test_priority_descriptions() {
assert_eq!(LayerPriority::Explicit.description(), "Explicit calls");
assert_eq!(LayerPriority::Defaults.description(), "Default values");
}
#[test]
fn test_sort_layers_by_priority() {
let mut layers: Vec<Box<dyn ConfigLayer>> = vec![
Box::new(MockConfigLayer::new("defaults", LayerPriority::Defaults)),
Box::new(MockConfigLayer::new("explicit", LayerPriority::Explicit)),
Box::new(MockConfigLayer::new("env", LayerPriority::Environment)),
Box::new(MockConfigLayer::new("config", LayerPriority::ConfigFile)),
];
utils::sort_layers_by_priority(&mut layers);
// Should be sorted by priority: Explicit, Environment, ConfigFile, Defaults
assert_eq!(layers[0].priority(), LayerPriority::Explicit);
assert_eq!(layers[1].priority(), LayerPriority::Environment);
assert_eq!(layers[2].priority(), LayerPriority::ConfigFile);
assert_eq!(layers[3].priority(), LayerPriority::Defaults);
}
#[test]
fn test_merge_value_from_layers_precedence() {
let layers: Vec<Box<dyn ConfigLayer>> = vec![
Box::new(
MockConfigLayer::new("explicit", LayerPriority::Explicit)
.with_value("key1", ConfigValue::String("explicit_value".to_string())),
),
Box::new(
MockConfigLayer::new("env", LayerPriority::Environment)
.with_value("key1", ConfigValue::String("env_value".to_string()))
.with_value("key2", ConfigValue::String("env_only".to_string())),
),
Box::new(
MockConfigLayer::new("defaults", LayerPriority::Defaults)
.with_value("key1", ConfigValue::String("default_value".to_string()))
.with_value("key3", ConfigValue::String("default_only".to_string())),
),
];
// key1 should return explicit value (highest precedence)
let result = utils::merge_value_from_layers(&layers, "key1").unwrap();
assert_eq!(
result,
Some(ConfigValue::String("explicit_value".to_string()))
);
// key2 should return env value (only available in env layer)
let result = utils::merge_value_from_layers(&layers, "key2").unwrap();
assert_eq!(result, Some(ConfigValue::String("env_only".to_string())));
// key3 should return default value (only available in defaults layer)
let result = utils::merge_value_from_layers(&layers, "key3").unwrap();
assert_eq!(
result,
Some(ConfigValue::String("default_only".to_string()))
);
// non-existent key should return None
let result = utils::merge_value_from_layers(&layers, "nonexistent").unwrap();
assert_eq!(result, None);
}
#[test]
fn test_collect_all_keys() {
let layers: Vec<Box<dyn ConfigLayer>> = vec![
Box::new(
MockConfigLayer::new("layer1", LayerPriority::Explicit)
.with_value("key1", ConfigValue::String("value1".to_string()))
.with_value("key2", ConfigValue::String("value2".to_string())),
),
Box::new(
MockConfigLayer::new("layer2", LayerPriority::Environment)
.with_value("key2", ConfigValue::String("value2_env".to_string()))
.with_value("key3", ConfigValue::String("value3".to_string())),
),
];
let keys = utils::collect_all_keys(&layers);
let mut expected = vec!["key1".to_string(), "key2".to_string(), "key3".to_string()];
expected.sort();
assert_eq!(keys, expected);
}
#[test]
fn test_merge_all_layers() {
let layers: Vec<Box<dyn ConfigLayer>> = vec![
Box::new(
MockConfigLayer::new("explicit", LayerPriority::Explicit)
.with_value("key1", ConfigValue::String("explicit_value".to_string())),
),
Box::new(
MockConfigLayer::new("env", LayerPriority::Environment)
.with_value("key1", ConfigValue::String("env_value".to_string()))
.with_value("key2", ConfigValue::String("env_only".to_string())),
),
Box::new(
MockConfigLayer::new("defaults", LayerPriority::Defaults)
.with_value("key3", ConfigValue::String("default_only".to_string())),
),
];
let merged = utils::merge_all_layers(&layers).unwrap();
// Should have 3 keys total
assert_eq!(merged.len(), 3);
// key1 should have explicit value (highest precedence)
assert_eq!(
merged.get("key1"),
Some(&ConfigValue::String("explicit_value".to_string()))
);
// key2 should have env value
assert_eq!(
merged.get("key2"),
Some(&ConfigValue::String("env_only".to_string()))
);
// key3 should have default value
assert_eq!(
merged.get("key3"),
Some(&ConfigValue::String("default_only".to_string()))
);
}
#[test]
fn test_layer_precedence_resolution() {
// Test the complete precedence chain
let mut layers: Vec<Box<dyn ConfigLayer>> = vec![
Box::new(
MockConfigLayer::new("defaults", LayerPriority::Defaults)
.with_value("shared_key", ConfigValue::String("default".to_string())),
),
Box::new(
MockConfigLayer::new("config", LayerPriority::ConfigFile)
.with_value("shared_key", ConfigValue::String("config".to_string())),
),
Box::new(
MockConfigLayer::new("env", LayerPriority::Environment)
.with_value("shared_key", ConfigValue::String("env".to_string())),
),
Box::new(
MockConfigLayer::new("flags", LayerPriority::Flags)
.with_value("shared_key", ConfigValue::String("flags".to_string())),
),
Box::new(
MockConfigLayer::new("explicit", LayerPriority::Explicit)
.with_value("shared_key", ConfigValue::String("explicit".to_string())),
),
];
// Sort layers by priority
utils::sort_layers_by_priority(&mut layers);
// The explicit layer should win
let result = utils::merge_value_from_layers(&layers, "shared_key").unwrap();
assert_eq!(result, Some(ConfigValue::String("explicit".to_string())));
// Remove explicit layer and flags should win
layers.remove(0);
let result = utils::merge_value_from_layers(&layers, "shared_key").unwrap();
assert_eq!(result, Some(ConfigValue::String("flags".to_string())));
// Remove flags layer and env should win
layers.remove(0);
let result = utils::merge_value_from_layers(&layers, "shared_key").unwrap();
assert_eq!(result, Some(ConfigValue::String("env".to_string())));
}
}