Skip to main content

configbuilder_pattern_substitution/
error.rs

1use std::fmt;
2
3/// Error type for environment variable substitution failures.
4#[derive(Debug, Clone, PartialEq)]
5pub enum SubstitutionError {
6    /// A referenced environment variable does not exist in the process environment.
7    VariableNotFound {
8        /// Name of the missing variable.
9        var_name: String,
10        /// Config file or section where the placeholder appeared.
11        location: String,
12    },
13    /// A variable was found but rejected by the active substitution policy.
14    VariableRejected {
15        /// Name of the rejected variable.
16        var_name: String,
17        /// Human-readable reason the policy rejected the variable.
18        reason: String,
19        /// Description of the policy that rejected the variable.
20        policy: String,
21    },
22    /// The substitution syntax in the config value is malformed.
23    InvalidSubstitutionSyntax {
24        /// The raw config value containing the malformed placeholder.
25        value: String,
26        /// Description of the syntax error.
27        reason: String,
28    },
29}
30
31impl fmt::Display for SubstitutionError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            SubstitutionError::VariableNotFound { var_name, location } => write!(
35                f,
36                "Environment variable '{}' referenced in config (at {}) but not found",
37                var_name, location
38            ),
39            SubstitutionError::VariableRejected {
40                var_name,
41                reason,
42                policy,
43            } => write!(
44                f,
45                "Environment variable '{}' not allowed by validation policy ({}): {}",
46                var_name, policy, reason
47            ),
48            SubstitutionError::InvalidSubstitutionSyntax { value, reason } => {
49                write!(f, "Invalid substitution syntax in '{}': {}", value, reason)
50            }
51        }
52    }
53}
54
55impl std::error::Error for SubstitutionError {}