tier 0.1.14

Rust configuration library for layered TOML, env, and CLI settings
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
use super::*;

impl ConfigMetadata {
    /// Creates an empty metadata set.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates metadata from a list of field entries.
    #[must_use]
    pub fn from_fields<I>(fields: I) -> Self
    where
        I: IntoIterator<Item = FieldMetadata>,
    {
        let mut metadata = Self::default();
        metadata.extend_fields(fields);
        metadata
    }

    /// Returns all merged field metadata entries.
    #[must_use]
    pub fn fields(&self) -> &[FieldMetadata] {
        &self.fields
    }

    /// Returns all normalized cross-field validation checks.
    #[must_use]
    pub fn checks(&self) -> &[ValidationCheck] {
        &self.checks
    }

    /// Returns the metadata entry for a normalized configuration path or alias.
    #[must_use]
    pub fn field(&self, path: &str) -> Option<&FieldMetadata> {
        let normalized = try_normalize_metadata_path(path).ok()?;
        let mut best = None::<(MetadataMatchScore, &FieldMetadata)>;
        for field in &self.fields {
            for candidate in
                std::iter::once(field.path.as_str()).chain(field.aliases.iter().map(String::as_str))
            {
                let Some(score) = metadata_match_score(&normalized, candidate) else {
                    continue;
                };

                match &mut best {
                    Some((best_score, best_field)) if score > *best_score => {
                        *best_score = score;
                        *best_field = field;
                    }
                    None => best = Some((score, field)),
                    _ => {}
                }
            }
        }

        best.map(|(_, field)| field)
    }

    pub(crate) fn matching_fields_for_path(&self, path: &str) -> Vec<&FieldMetadata> {
        let normalized = match try_normalize_metadata_path(path) {
            Ok(normalized) => normalized,
            Err(_) => return Vec::new(),
        };

        let mut matches = self
            .fields
            .iter()
            .filter_map(|field| {
                let best = std::iter::once(field.path.as_str())
                    .chain(field.aliases.iter().map(String::as_str))
                    .filter_map(|candidate| metadata_match_score(&normalized, candidate))
                    .max();
                best.map(|score| (score, field))
            })
            .collect::<Vec<_>>();
        matches.sort_by(|left, right| {
            left.0
                .cmp(&right.0)
                .then_with(|| left.1.path.cmp(&right.1.path))
        });
        matches.into_iter().map(|(_, field)| field).collect()
    }

    pub(crate) fn effective_source_policy_for(&self, path: &str) -> Option<EffectiveSourcePolicy> {
        let mut policy = EffectiveSourcePolicy::default();
        let mut has_policy = false;

        for field in self.matching_fields_for_path(path) {
            if field.allowed_sources.is_some() || field.denied_sources.is_some() {
                has_policy = true;
                policy.apply_field(field);
            }
        }

        has_policy.then_some(policy)
    }

    pub(crate) fn effective_validations_for(&self, path: &str) -> Vec<EffectiveValidation> {
        let Some(field) = self.effective_field_for(path) else {
            return Vec::new();
        };

        field
            .validations
            .iter()
            .cloned()
            .map(|rule| EffectiveValidation {
                field: field.clone(),
                rule,
            })
            .collect()
    }

    pub(crate) fn effective_field_for(&self, path: &str) -> Option<FieldMetadata> {
        let mut matches = self.matching_fields_for_path(path).into_iter();
        let mut effective = matches.next()?.clone();
        for field in matches {
            effective.merge_from(field.clone());
        }
        Some(effective)
    }

    /// Returns metadata entries keyed by normalized path.
    #[must_use]
    pub fn fields_by_path(&self) -> BTreeMap<String, FieldMetadata> {
        self.fields
            .iter()
            .cloned()
            .map(|field| (field.path.clone(), field))
            .collect()
    }

    /// Adds a field metadata entry and merges duplicates by path.
    pub fn push(&mut self, field: FieldMetadata) {
        self.fields.push(field);
        self.normalize();
    }

    /// Extends the metadata with additional field entries.
    pub fn extend_fields<I>(&mut self, fields: I)
    where
        I: IntoIterator<Item = FieldMetadata>,
    {
        self.fields.extend(fields);
        self.normalize();
    }

    /// Extends the metadata with another metadata set.
    pub fn extend(&mut self, other: Self) {
        self.fields.extend(other.fields);
        self.checks.extend(other.checks);
        self.normalize();
    }

    /// Adds a cross-field validation check.
    pub fn push_check(&mut self, check: ValidationCheck) {
        self.checks.push(check);
        self.normalize();
    }

    /// Extends the metadata with additional cross-field validation checks.
    pub fn extend_checks<I>(&mut self, checks: I)
    where
        I: IntoIterator<Item = ValidationCheck>,
    {
        self.checks.extend(checks);
        self.normalize();
    }

    /// Adds a cross-field validation check in builder style.
    #[must_use]
    pub fn check(mut self, check: ValidationCheck) -> Self {
        self.push_check(check);
        self
    }

    /// Requires that at least one of the given paths is configured.
    #[must_use]
    pub fn at_least_one_of<I, S>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.check(ValidationCheck::AtLeastOneOf {
            paths: paths.into_iter().map(Into::into).collect(),
        })
    }

    /// Requires that exactly one of the given paths is configured.
    #[must_use]
    pub fn exactly_one_of<I, S>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.check(ValidationCheck::ExactlyOneOf {
            paths: paths.into_iter().map(Into::into).collect(),
        })
    }

    /// Requires that at most one of the given paths is configured.
    #[must_use]
    pub fn mutually_exclusive<I, S>(self, paths: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.check(ValidationCheck::MutuallyExclusive {
            paths: paths.into_iter().map(Into::into).collect(),
        })
    }

    /// Requires one or more paths whenever `path` is configured.
    #[must_use]
    pub fn required_with<I, S>(self, path: impl Into<String>, requires: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.check(ValidationCheck::RequiredWith {
            path: path.into(),
            requires: requires.into_iter().map(Into::into).collect(),
        })
    }

    /// Requires one or more paths whenever `path` equals `equals`.
    #[must_use]
    pub fn required_if<I, S, V>(self, path: impl Into<String>, equals: V, requires: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
        V: Into<ValidationValue>,
    {
        self.check(ValidationCheck::RequiredIf {
            path: path.into(),
            equals: equals.into(),
            requires: requires.into_iter().map(Into::into).collect(),
        })
    }

    /// Returns all normalized secret paths.
    #[must_use]
    pub fn secret_paths(&self) -> Vec<String> {
        self.fields
            .iter()
            .filter(|field| field.secret)
            .map(|field| field.path.clone())
            .collect()
    }

    pub(crate) fn canonicalize_env_decoder_paths(&mut self) -> Result<(), ConfigError> {
        let alias_source_fields = self
            .fields
            .iter()
            .filter(|field| !field.is_env_decoder_only())
            .cloned()
            .collect::<Vec<_>>();
        let aliases = ConfigMetadata {
            fields: alias_source_fields,
            checks: Vec::new(),
        }
        .alias_overrides()?;

        let mut seen = BTreeMap::<String, (String, EnvDecoder)>::new();
        for field in &mut self.fields {
            if !field.is_env_decoder_only() {
                continue;
            }

            let original_path = field.path.clone();
            let canonical = canonicalize_path_with_aliases(&original_path, &aliases);
            let decoder = field
                .env_decode
                .expect("decoder-only fields must have a decoder");
            if let Some((first_path, first_decoder)) = seen.get(&canonical)
                && (first_path != &original_path || *first_decoder != decoder)
            {
                return Err(ConfigError::MetadataConflict {
                    kind: "environment decoder",
                    name: canonical,
                    first_path: first_path.clone(),
                    second_path: original_path,
                });
            }

            seen.insert(canonical.clone(), (original_path, decoder));
            field.path = canonical;
        }

        self.normalize();
        Ok(())
    }

    /// Returns explicit environment variable name overrides keyed by env name.
    pub fn env_overrides(&self) -> Result<BTreeMap<String, String>, ConfigError> {
        let aliases = self.alias_overrides()?;
        let mut envs = BTreeMap::new();
        let mut canonical_targets = BTreeMap::<String, String>::new();
        for field in &self.fields {
            let Some(env) = &field.env else {
                continue;
            };
            if env.is_empty() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "explicit environment variable names cannot be empty".to_owned(),
                });
            }
            validate_metadata_path(&field.path)?;
            if field.path.is_empty() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "explicit environment variable names cannot target the root path"
                        .to_owned(),
                });
            }
            if field.path.split('.').any(|segment| segment == "*") {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "explicit environment variable names cannot target wildcard paths"
                        .to_owned(),
                });
            }
            let canonical = canonicalize_path_with_aliases(&field.path, &aliases);
            if let Some(first_env) = canonical_targets.insert(canonical.clone(), env.clone())
                && first_env != *env
            {
                return Err(ConfigError::MetadataConflict {
                    kind: "environment override target",
                    name: canonical,
                    first_path: first_env,
                    second_path: env.clone(),
                });
            }
            if let Some(first_path) = envs.insert(env.clone(), field.path.clone())
                && first_path != field.path
            {
                return Err(ConfigError::MetadataConflict {
                    kind: "environment variable",
                    name: env.clone(),
                    first_path,
                    second_path: field.path.clone(),
                });
            }
        }
        Ok(envs)
    }

    /// Returns explicit path aliases keyed by alias path.
    pub fn alias_overrides(&self) -> Result<BTreeMap<String, String>, ConfigError> {
        let mut aliases = BTreeMap::<String, String>::new();
        let canonical_paths = self
            .fields
            .iter()
            .map(|field| field.path.clone())
            .collect::<BTreeSet<_>>();

        for field in &self.fields {
            validate_metadata_path(&field.path)?;
            for alias in &field.aliases {
                validate_metadata_path(alias)?;
                if alias.is_empty() {
                    return Err(ConfigError::MetadataInvalid {
                        path: alias.clone(),
                        message: "aliases cannot target the root path".to_owned(),
                    });
                }
                if field.path.is_empty() {
                    return Err(ConfigError::MetadataInvalid {
                        path: alias.clone(),
                        message: "aliases cannot rewrite the root path".to_owned(),
                    });
                }
                if !alias_mapping_is_lossless(alias, &field.path) {
                    return Err(ConfigError::MetadataInvalid {
                        path: alias.clone(),
                        message: format!(
                            "alias `{alias}` must preserve wildcard positions and cannot be deeper than canonical path `{}`",
                            field.path
                        ),
                    });
                }
                if canonical_paths.contains(alias) && alias != &field.path {
                    return Err(ConfigError::MetadataConflict {
                        kind: "alias",
                        name: alias.clone(),
                        first_path: alias.clone(),
                        second_path: field.path.clone(),
                    });
                }
                if let Some(first_path) = aliases.get(alias)
                    && first_path != &field.path
                {
                    return Err(ConfigError::MetadataConflict {
                        kind: "alias",
                        name: alias.clone(),
                        first_path: first_path.clone(),
                        second_path: field.path.clone(),
                    });
                }
                if let Some((other_alias, sample_path)) =
                    aliases.iter().find_map(|(other_alias, other_canonical)| {
                        alias_patterns_are_ambiguous(
                            alias,
                            &field.path,
                            other_alias,
                            other_canonical,
                        )
                        .then(|| {
                            (
                                other_alias.clone(),
                                alias_overlap_sample_path(alias, other_alias),
                            )
                        })
                    })
                {
                    return Err(ConfigError::MetadataInvalid {
                        path: alias.clone(),
                        message: format!(
                            "alias `{alias}` overlaps ambiguously with `{other_alias}` for concrete path `{sample_path}`"
                        ),
                    });
                }
                aliases.insert(alias.clone(), field.path.clone());
            }
        }
        Ok(aliases)
    }

    /// Returns explicitly declared field merge strategies keyed by normalized path.
    #[must_use]
    pub fn merge_strategies(&self) -> BTreeMap<String, MergeStrategy> {
        self.fields
            .iter()
            .filter(|field| field.merge_explicit)
            .map(|field| (field.path.clone(), field.merge))
            .collect()
    }

    /// Resolves the effective merge strategy for a concrete configuration path.
    #[must_use]
    pub fn merge_strategy_for(&self, path: &str) -> Option<MergeStrategy> {
        self.effective_field_for(path).map(|field| field.merge)
    }

    pub(crate) fn validate_paths(&self) -> Result<(), ConfigError> {
        let _ = self.env_overrides()?;

        for field in &self.fields {
            validate_metadata_path(&field.path)?;
            if field.path.is_empty() && !field.aliases.is_empty() {
                let alias = field.aliases.first().cloned().unwrap_or_default();
                return Err(ConfigError::MetadataInvalid {
                    path: alias,
                    message: "aliases cannot rewrite the root path".to_owned(),
                });
            }
            if field.path.is_empty() && field.merge_explicit {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "merge strategies cannot target the root path".to_owned(),
                });
            }
            if field.path.is_empty() && field.allowed_sources.is_some() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "source policies cannot target the root path".to_owned(),
                });
            }
            if field.path.is_empty() && field.denied_sources.is_some() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "source policies cannot target the root path".to_owned(),
                });
            }
            if let Some(allowed_sources) = &field.allowed_sources
                && allowed_sources.is_empty()
            {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "source policies must allow at least one source kind".to_owned(),
                });
            }
            if let Some(denied_sources) = &field.denied_sources
                && let Some(allowed_sources) = &field.allowed_sources
            {
                let overlap = allowed_sources
                    .intersection(denied_sources)
                    .copied()
                    .collect::<Vec<_>>();
                if !overlap.is_empty() {
                    return Err(ConfigError::MetadataInvalid {
                        path: field.path.clone(),
                        message: format!(
                            "source policies cannot both allow and deny the same source kinds: {}",
                            overlap
                                .iter()
                                .map(ToString::to_string)
                                .collect::<Vec<_>>()
                                .join(", ")
                        ),
                    });
                }
            }
            if field.path.is_empty() && !field.validations.is_empty() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "validation rules cannot target the root path".to_owned(),
                });
            }
            if field.path.is_empty() && !field.validation_configs.is_empty() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "validation rules cannot target the root path".to_owned(),
                });
            }
            if field.path.is_empty() && field.secret {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "secret metadata cannot target the root path".to_owned(),
                });
            }
            let effective_rule_codes = self
                .effective_field_for(&field.path)
                .map(|field| {
                    field
                        .validations
                        .iter()
                        .map(ValidationRule::code)
                        .collect::<BTreeSet<_>>()
                })
                .unwrap_or_default();
            for rule_code in field.validation_configs.keys() {
                if !effective_rule_codes.contains(rule_code.as_str()) {
                    return Err(ConfigError::MetadataInvalid {
                        path: field.path.clone(),
                        message: format!(
                            "validation config references unknown rule `{rule_code}` for this field"
                        ),
                    });
                }
            }
            if field.path.is_empty() && field.deprecated.is_some() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "deprecation metadata cannot target the root path".to_owned(),
                });
            }
            if field.env_decode.is_some() && field.path.is_empty() {
                return Err(ConfigError::MetadataInvalid {
                    path: field.path.clone(),
                    message: "environment decoder paths cannot target the root path".to_owned(),
                });
            }
            for alias in &field.aliases {
                validate_metadata_path(alias)?;
                if alias.is_empty() {
                    return Err(ConfigError::MetadataInvalid {
                        path: alias.clone(),
                        message: "aliases cannot target the root path".to_owned(),
                    });
                }
            }
        }

        for check in &self.checks {
            match check {
                ValidationCheck::AtLeastOneOf { paths }
                | ValidationCheck::ExactlyOneOf { paths }
                | ValidationCheck::MutuallyExclusive { paths } => {
                    for path in paths {
                        validate_check_path(path)?;
                    }
                }
                ValidationCheck::RequiredWith { path, requires }
                | ValidationCheck::RequiredIf { path, requires, .. } => {
                    validate_check_path(path)?;
                    for required in requires {
                        validate_check_path(required)?;
                    }
                }
            }
        }

        Ok(())
    }

    pub(super) fn normalize(&mut self) {
        let mut merged = BTreeMap::<String, FieldMetadata>::new();
        for mut field in self.fields.drain(..) {
            field.path = normalize_metadata_path(&field.path);
            field.aliases = field
                .aliases
                .into_iter()
                .map(|alias| normalize_metadata_path(&alias))
                .filter(|alias| alias != &field.path)
                .collect();
            field.aliases.sort();
            field.aliases.dedup();
            match merged.get_mut(&field.path) {
                Some(existing) => existing.merge_from(field),
                None => {
                    merged.insert(field.path.clone(), field);
                }
            }
        }
        self.fields = merged.into_values().collect();
        self.checks = normalize_checks(self.checks.drain(..));
    }
}