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
use std::fmt;
/// Error type for environment variable substitution failures.
#[derive(Debug, Clone, PartialEq)]
pub enum SubstitutionError {
/// A referenced environment variable does not exist in the process environment.
VariableNotFound {
/// Name of the missing variable.
var_name: String,
/// Config file or section where the placeholder appeared.
location: String,
},
/// A variable was found but rejected by the active substitution policy.
VariableRejected {
/// Name of the rejected variable.
var_name: String,
/// Human-readable reason the policy rejected the variable.
reason: String,
/// Description of the policy that rejected the variable.
policy: String,
},
/// The substitution syntax in the config value is malformed.
InvalidSubstitutionSyntax {
/// The raw config value containing the malformed placeholder.
value: String,
/// Description of the syntax error.
reason: String,
},
}
impl fmt::Display for SubstitutionError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
SubstitutionError::VariableNotFound { var_name, location } => write!(
f,
"Environment variable '{}' referenced in config (at {}) but not found",
var_name, location
),
SubstitutionError::VariableRejected {
var_name,
reason,
policy,
} => write!(
f,
"Environment variable '{}' not allowed by validation policy ({}): {}",
var_name, policy, reason
),
SubstitutionError::InvalidSubstitutionSyntax { value, reason } => {
write!(f, "Invalid substitution syntax in '{}': {}", value, reason)
}
}
}
}
impl std::error::Error for SubstitutionError {}