configbuilder_pattern_substitution/
prefix_whitelist_policy.rs1use crate::{SubstitutionError, SubstitutionPolicy};
2
3#[derive(Debug)]
5pub struct PrefixWhitelistPolicy {
6 prefixes: Vec<String>,
7}
8
9impl PrefixWhitelistPolicy {
10 pub fn new(prefixes: Vec<String>) -> Self {
12 Self { prefixes }
13 }
14
15 pub fn prefixes(&self) -> &[String] {
17 &self.prefixes
18 }
19}
20
21impl SubstitutionPolicy for PrefixWhitelistPolicy {
22 fn validate(&self, var_name: &str) -> Result<(), SubstitutionError> {
23 if self
24 .prefixes()
25 .iter()
26 .any(|p| var_name.starts_with(p.as_str()))
27 {
28 Ok(())
29 } else {
30 Err(SubstitutionError::VariableRejected {
31 var_name: var_name.to_string(),
32 reason: format!(
33 "does not match any allowed prefix: {}",
34 self.prefixes().join(", ")
35 ),
36 policy: self.description(),
37 })
38 }
39 }
40
41 fn description(&self) -> String {
42 format!("PrefixWhitelist({})", self.prefixes().join(", "))
43 }
44}