spin_templates/
constraints.rs

1use regex::Regex;
2
3#[derive(Clone, Debug)]
4pub(crate) struct StringConstraints {
5    pub regex: Option<Regex>,
6    pub allowed_values: Option<Vec<String>>,
7}
8
9impl StringConstraints {
10    pub fn validate(&self, text: String) -> anyhow::Result<String> {
11        if let Some(regex) = self.regex.as_ref() {
12            if !regex.is_match(&text) {
13                anyhow::bail!("Input '{}' does not match pattern '{}'", text, regex);
14            }
15        }
16        if let Some(allowed_values) = self.allowed_values.as_ref() {
17            if !allowed_values.contains(&text) {
18                anyhow::bail!(
19                    "Input '{}' is not one of the allowed values ({})",
20                    text,
21                    allowed_values.join(", ")
22                );
23            }
24        }
25        Ok(text)
26    }
27}