Skip to main content

alien_permissions/
variables.rs

1use crate::{
2    error::{ErrorData, Result},
3    PermissionContext,
4};
5
6/// Utility functions for variable interpolation
7pub struct VariableInterpolator;
8
9impl VariableInterpolator {
10    /// Interpolate variables in a string template
11    pub fn interpolate_variables(template: &str, context: &PermissionContext) -> Result<String> {
12        let mut result = template.to_string();
13
14        // Find all variables in the format ${variableName}
15        let re = regex::Regex::new(r"\$\{([^}]+)\}").unwrap();
16
17        for captures in re.captures_iter(template) {
18            let full_match = &captures[0];
19            let variable_name = &captures[1];
20
21            let value = context.get_variable(variable_name).ok_or_else(|| {
22                alien_error::AlienError::new(ErrorData::VariableNotFound {
23                    variable: variable_name.to_string(),
24                })
25            })?;
26
27            result = result.replace(full_match, value);
28        }
29
30        Ok(result)
31    }
32
33    /// Interpolate variables in a list of strings
34    pub fn interpolate_string_list(
35        templates: &[String],
36        context: &PermissionContext,
37    ) -> Result<Vec<String>> {
38        templates
39            .iter()
40            .map(|template| Self::interpolate_variables(template, context))
41            .collect()
42    }
43}