confers 0.4.0

Production-ready Rust configuration library with zero boilerplate
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
//! Source chain for combining multiple configuration sources.
//!
//! The source chain manages multiple sources with priority ordering
//! and merges their values according to merge strategies.

use crate::error::{ConfigError, ConfigResult};
use crate::impl_::merger::{MergeEngine, MergeStrategy};
use crate::interface::Source;
use crate::types::{AnnotatedValue, ConfigValue, SourceKind};
use indexmap::IndexMap;
use std::sync::Arc;

/// A chain of configuration sources with priority ordering.
///
/// Sources are collected and merged in order of priority.
/// Higher priority sources override values from lower priority sources.
pub struct SourceChain {
    /// Sources in the chain.
    sources: Vec<Box<dyn Source>>,
    /// Merge engine for combining values.
    merge_engine: MergeEngine,
    /// Whether to stop on first error.
    fail_fast: bool,
}

impl Default for SourceChain {
    fn default() -> Self {
        Self::new()
    }
}

impl SourceChain {
    /// Create a new empty source chain.
    pub fn new() -> Self {
        Self {
            sources: Vec::new(),
            merge_engine: MergeEngine::new(),
            fail_fast: true,
        }
    }

    /// Create a source chain with a default merge strategy.
    pub fn with_strategy(strategy: MergeStrategy) -> Self {
        Self {
            sources: Vec::new(),
            merge_engine: MergeEngine::new().with_default_strategy(strategy),
            fail_fast: true,
        }
    }

    /// Push a source to the chain.
    pub fn push(mut self, source: Box<dyn Source>) -> Self {
        self.sources.push(source);
        self
    }

    /// Add a source with explicit ordering.
    pub fn add_ordered(mut self, source: Box<dyn Source>) -> Self {
        // Insert in priority order (higher priority sources should be processed later)
        let priority = source.priority();
        let pos = self
            .sources
            .iter()
            .position(|s| s.priority() > priority)
            .unwrap_or(self.sources.len());
        self.sources.insert(pos, source);
        self
    }

    /// Set whether to stop on first error.
    pub fn fail_fast(mut self, fail_fast: bool) -> Self {
        self.fail_fast = fail_fast;
        self
    }

    /// Set a field-specific merge strategy.
    pub fn with_field_strategy(
        mut self,
        field: impl Into<Arc<str>>,
        strategy: MergeStrategy,
    ) -> Self {
        self.merge_engine = self.merge_engine.with_field_strategy(field, strategy);
        self
    }

    /// Get the number of sources.
    pub fn len(&self) -> usize {
        self.sources.len()
    }

    /// Check if the chain is empty.
    pub fn is_empty(&self) -> bool {
        self.sources.is_empty()
    }

    /// Get a reference to the sources in this chain.
    pub fn sources(&self) -> &[Box<dyn Source>] {
        &self.sources
    }

    /// Collect and merge all sources.
    pub fn collect(self) -> ConfigResult<AnnotatedValue> {
        let sources = self.sources;
        let merge_engine = self.merge_engine;
        let fail_fast = self.fail_fast;

        Self::collect_and_merge(sources, merge_engine, fail_fast)
    }

    fn collect_and_merge(
        sources: Vec<Box<dyn Source>>,
        merge_engine: MergeEngine,
        fail_fast: bool,
    ) -> ConfigResult<AnnotatedValue> {
        if sources.is_empty() {
            return Ok(AnnotatedValue::new(
                ConfigValue::Map(Arc::new(IndexMap::new())),
                crate::types::SourceId::new("empty"),
                "",
            ));
        }

        // Collect all source values
        let mut values: Vec<(String, ConfigResult<AnnotatedValue>)> = Vec::new();
        let mut errors: Vec<(String, ConfigError)> = Vec::new();

        for source in &sources {
            let name = source.name().to_string();
            let result = source.collect();

            match result {
                Ok(value) => values.push((name, Ok(value))),
                Err(e) => {
                    if fail_fast && !source.is_optional() {
                        return Err(e);
                    }
                    errors.push((name, e));
                }
            }
        }

        // Handle all errors case
        if values.is_empty() && !errors.is_empty() {
            let multi_err = crate::error::MultiSourceError::new(sources.len(), errors);
            return Err(ConfigError::MultiSource { source: multi_err });
        }

        // Sort by priority (lower priority first)
        let mut sorted_values: Vec<_> = values
            .into_iter()
            .filter_map(|(_, result)| result.ok())
            .collect();
        sorted_values.sort_by_key(|v| v.priority);

        // Merge all values
        let mut merged = AnnotatedValue::new(
            ConfigValue::Map(Arc::new(IndexMap::new())),
            crate::types::SourceId::new("merged"),
            "",
        );

        for value in sorted_values {
            merged = merge_engine.merge(&merged, &value)?;
        }

        Ok(merged)
    }

    /// Get a list of source names.
    pub fn source_names(&self) -> Vec<&str> {
        self.sources.iter().map(|s| s.name()).collect()
    }

    /// Get source kinds.
    pub fn source_kinds(&self) -> Vec<SourceKind> {
        self.sources.iter().map(|s| s.source_kind()).collect()
    }
}

/// Builder for creating source chains with a fluent API.
pub struct SourceChainBuilder {
    chain: SourceChain,
    /// Whether to allow absolute paths for file sources.
    allow_absolute_paths: bool,
}

impl Default for SourceChainBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl SourceChainBuilder {
    /// Create a new builder.
    pub fn new() -> Self {
        Self {
            chain: SourceChain::new(),
            allow_absolute_paths: false,
        }
    }

    /// Add a source.
    pub fn source(mut self, source: Box<dyn Source>) -> Self {
        self.chain = self.chain.push(source);
        self
    }

    /// Add a file source.
    pub fn file(self, path: impl Into<std::path::PathBuf>) -> Self {
        use super::source::FileSource;
        let mut source = FileSource::new(path);
        if self.allow_absolute_paths {
            source = source.allow_absolute_paths();
        }
        self.source(Box::new(source))
    }

    /// Add an optional file source.
    pub fn file_optional(self, path: impl Into<std::path::PathBuf>) -> Self {
        use super::source::FileSource;
        let mut source = FileSource::new(path).optional();
        if self.allow_absolute_paths {
            source = source.allow_absolute_paths();
        }
        self.source(Box::new(source))
    }

    /// Allow absolute paths for file sources (use with caution, mainly for testing).
    pub fn allow_absolute_paths(mut self) -> Self {
        self.allow_absolute_paths = true;
        self
    }

    /// Add an environment source.
    pub fn env(self) -> Self {
        use super::source::EnvSource;
        self.source(Box::new(EnvSource::new()))
    }

    /// Add an environment source with prefix.
    pub fn env_with_prefix(self, prefix: impl Into<String>) -> Self {
        use super::source::EnvSource;
        self.source(Box::new(EnvSource::with_prefix(prefix)))
    }

    /// Add a default source.
    pub fn defaults(self, defaults: std::collections::HashMap<String, ConfigValue>) -> Self {
        use super::source::DefaultSource;
        self.source(Box::new(DefaultSource::with_defaults(defaults)))
    }

    /// Add a memory source.
    pub fn memory(self, values: std::collections::HashMap<String, ConfigValue>) -> Self {
        use super::source::MemorySource;
        self.source(Box::new(MemorySource::with_values(values)))
    }

    /// Add a memory source with custom priority.
    pub fn memory_with_priority(
        self,
        values: std::collections::HashMap<String, ConfigValue>,
        priority: u8,
    ) -> Self {
        use super::source::MemorySource;
        self.source(Box::new(
            MemorySource::with_values(values).with_priority(priority),
        ))
    }

    /// Set merge strategy.
    pub fn strategy(mut self, strategy: MergeStrategy) -> Self {
        self.chain.merge_engine = self.chain.merge_engine.with_default_strategy(strategy);
        self
    }

    /// Set field-specific merge strategy.
    pub fn field_strategy(mut self, field: impl Into<Arc<str>>, strategy: MergeStrategy) -> Self {
        self.chain = self.chain.with_field_strategy(field, strategy);
        self
    }

    /// Set fail fast mode.
    pub fn fail_fast(mut self, fail_fast: bool) -> Self {
        self.chain = self.chain.fail_fast(fail_fast);
        self
    }

    /// Build the source chain.
    pub fn build(self) -> SourceChain {
        self.chain
    }

    /// Get file paths from file sources for watching.
    pub fn get_watch_paths(&self) -> Vec<std::path::PathBuf> {
        self.chain
            .sources
            .iter()
            .filter(|s| s.source_kind() == SourceKind::File)
            .filter_map(|s| s.file_path().map(|p| p.to_path_buf()))
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::impl_::config::{DefaultSource, MemorySource};

    #[test]
    fn test_empty_chain() {
        let chain = SourceChain::new();
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_single_source() {
        let chain = SourceChain::new().push(Box::new(
            MemorySource::new().set("key", ConfigValue::string("value")),
        ));

        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_multiple_sources() {
        let chain = SourceChain::new()
            .push(Box::new(
                DefaultSource::new().set("key", ConfigValue::string("default")),
            ))
            .push(Box::new(
                MemorySource::new()
                    .set("key", ConfigValue::string("override"))
                    .with_priority(50),
            ));

        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_builder() {
        let chain = SourceChainBuilder::new()
            .defaults(std::collections::HashMap::from([(
                "key".to_string(),
                ConfigValue::string("default"),
            )]))
            .memory(std::collections::HashMap::from([(
                "key".to_string(),
                ConfigValue::string("memory"),
            )]))
            .build();

        assert!(!chain.is_empty());
        assert_eq!(chain.len(), 2);
    }

    #[test]
    fn test_source_names() {
        let chain = SourceChain::new()
            .push(Box::new(MemorySource::new().with_name("first")))
            .push(Box::new(MemorySource::new().with_name("second")));

        let names = chain.source_names();
        assert_eq!(names, vec!["first", "second"]);
    }

    #[test]
    fn test_fail_fast_optional() {
        let chain = SourceChain::new()
            .fail_fast(false)
            .push(Box::new(
                crate::impl_::config::FileSource::new("/nonexistent.toml").optional(),
            ))
            .push(Box::new(
                MemorySource::new().set("key", ConfigValue::string("value")),
            ));

        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_chain_default_trait() {
        let chain = SourceChain::default();
        assert!(chain.is_empty());
    }

    #[test]
    fn test_chain_is_empty() {
        let chain = SourceChain::new();
        assert!(chain.is_empty());
        let chain2 = SourceChain::new().push(Box::new(MemorySource::new()));
        assert!(!chain2.is_empty());
    }

    #[test]
    fn test_chain_len() {
        let chain = SourceChain::new();
        assert_eq!(chain.len(), 0);
        let chain2 = SourceChain::new()
            .push(Box::new(MemorySource::new()))
            .push(Box::new(MemorySource::new()));
        assert_eq!(chain2.len(), 2);
    }

    #[test]
    fn test_chain_with_strategy() {
        let chain = SourceChain::with_strategy(MergeStrategy::Append);
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_chain_add_ordered() {
        let chain = SourceChain::new()
            .add_ordered(Box::new(MemorySource::new().with_priority(10)))
            .add_ordered(Box::new(MemorySource::new().with_priority(50)))
            .add_ordered(Box::new(MemorySource::new().with_priority(30)));
        assert_eq!(chain.len(), 3);
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_chain_with_field_strategy() {
        let chain = SourceChain::new()
            .with_field_strategy("name", MergeStrategy::Replace)
            .push(Box::new(
                MemorySource::new().set("name", ConfigValue::string("x")),
            ));
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_chain_sources_accessor() {
        let chain = SourceChain::new()
            .push(Box::new(MemorySource::new()))
            .push(Box::new(DefaultSource::new()));
        assert_eq!(chain.sources().len(), 2);
    }

    #[test]
    fn test_chain_source_kinds() {
        let chain = SourceChain::new()
            .push(Box::new(MemorySource::new()))
            .push(Box::new(DefaultSource::new()));
        let kinds = chain.source_kinds();
        assert_eq!(kinds, vec![SourceKind::Memory, SourceKind::Default]);
    }

    #[test]
    fn test_chain_override_behavior() {
        // Higher priority source overrides lower priority
        let chain = SourceChain::new()
            .push(Box::new(
                DefaultSource::new().set("key", ConfigValue::string("default_val")),
            ))
            .push(Box::new(
                MemorySource::new()
                    .set("key", ConfigValue::string("override_val"))
                    .with_priority(50),
            ));
        let result = chain.collect().unwrap();
        assert!(result.is_map());
        if let ConfigValue::Map(map) = &result.inner {
            let val = map.get("key").expect("key should exist");
            if let ConfigValue::String(s) = &val.inner {
                assert_eq!(s, "override_val");
            } else {
                panic!("expected String value");
            }
        } else {
            panic!("expected map");
        }
    }

    #[test]
    fn test_chain_fail_fast_required_error() {
        // fail_fast=true + required source fails → immediate error
        let chain = SourceChain::new().fail_fast(true).push(Box::new(
            crate::impl_::config::FileSource::new("/nonexistent.toml"),
        ));
        let result = chain.collect();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ConfigError::FileNotFound { .. }
        ));
    }

    #[test]
    fn test_chain_multi_source_error() {
        // fail_fast=false + all required sources fail → MultiSource error
        let chain = SourceChain::new()
            .fail_fast(false)
            .push(Box::new(crate::impl_::config::FileSource::new(
                "/nonexistent1.toml",
            )))
            .push(Box::new(crate::impl_::config::FileSource::new(
                "/nonexistent2.toml",
            )));
        let result = chain.collect();
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            ConfigError::MultiSource { .. }
        ));
    }

    #[test]
    fn test_builder_default_trait() {
        let builder = SourceChainBuilder::default();
        let chain = builder.build();
        assert!(chain.is_empty());
    }

    #[test]
    fn test_builder_source_method() {
        let chain = SourceChainBuilder::new()
            .source(Box::new(
                MemorySource::new().set("k", ConfigValue::string("v")),
            ))
            .build();
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_builder_file_method() {
        let chain = SourceChainBuilder::new().file("config.toml").build();
        assert_eq!(chain.len(), 1);
        assert_eq!(chain.source_kinds(), vec![SourceKind::File]);
    }

    #[test]
    fn test_builder_file_optional_method() {
        let chain = SourceChainBuilder::new()
            .file_optional("missing.toml")
            .build();
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_builder_env_method() {
        let chain = SourceChainBuilder::new().env().build();
        assert_eq!(chain.len(), 1);
        assert_eq!(chain.source_kinds(), vec![SourceKind::Environment]);
    }

    #[test]
    fn test_builder_env_with_prefix_method() {
        let chain = SourceChainBuilder::new().env_with_prefix("X_").build();
        assert_eq!(chain.len(), 1);
        assert_eq!(chain.source_kinds(), vec![SourceKind::Environment]);
    }

    #[test]
    fn test_builder_memory_with_priority() {
        let chain = SourceChainBuilder::new()
            .memory_with_priority(
                std::collections::HashMap::from([("k".to_string(), ConfigValue::string("v"))]),
                99,
            )
            .build();
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_builder_strategy_method() {
        let chain = SourceChainBuilder::new()
            .strategy(MergeStrategy::Append)
            .build();
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_builder_field_strategy_method() {
        let chain = SourceChainBuilder::new()
            .field_strategy("key", MergeStrategy::Replace)
            .build();
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_builder_fail_fast_method() {
        let chain = SourceChainBuilder::new().fail_fast(false).build();
        let result = chain.collect().unwrap();
        assert!(result.is_map());
    }

    #[test]
    fn test_builder_allow_absolute_paths_method() {
        let chain = SourceChainBuilder::new()
            .allow_absolute_paths()
            .file("/absolute/path.toml")
            .build();
        assert_eq!(chain.len(), 1);
    }

    #[test]
    fn test_builder_get_watch_paths() {
        let builder = SourceChainBuilder::new()
            .file("config1.toml")
            .file("config2.json")
            .env();
        let paths = builder.get_watch_paths();
        // Only file sources contribute paths (env source has none)
        assert_eq!(paths.len(), 2);
    }

    #[test]
    fn test_chain_source_names_multi() {
        let chain = SourceChain::new()
            .push(Box::new(MemorySource::new().with_name("alpha")))
            .push(Box::new(MemorySource::new().with_name("beta")))
            .push(Box::new(DefaultSource::new()));
        let names = chain.source_names();
        assert_eq!(names, vec!["alpha", "beta", "default"]);
    }
}