use crate::{SubstitutionError, SubstitutionPolicy};
#[derive(Debug)]
pub struct PrefixWhitelistPolicy {
prefixes: Vec<String>,
}
impl PrefixWhitelistPolicy {
pub fn new(prefixes: Vec<String>) -> Self {
Self { prefixes }
}
pub fn prefixes(&self) -> &[String] {
&self.prefixes
}
}
impl SubstitutionPolicy for PrefixWhitelistPolicy {
fn validate(&self, var_name: &str) -> Result<(), SubstitutionError> {
if self
.prefixes()
.iter()
.any(|p| var_name.starts_with(p.as_str()))
{
Ok(())
} else {
Err(SubstitutionError::VariableRejected {
var_name: var_name.to_string(),
reason: format!(
"does not match any allowed prefix: {}",
self.prefixes().join(", ")
),
policy: self.description(),
})
}
}
fn description(&self) -> String {
format!("PrefixWhitelist({})", self.prefixes().join(", "))
}
}