Skip to content

Instantly share code, notes, and snippets.

@andria-dev
Created May 8, 2024 03:30
Show Gist options
  • Save andria-dev/fde8a9e14a155ad24f19adbbcffe89f3 to your computer and use it in GitHub Desktop.
Save andria-dev/fde8a9e14a155ad24f19adbbcffe89f3 to your computer and use it in GitHub Desktop.
use anyhow::bail;
pub struct Recipe {
template: String,
}
impl Recipe {
const VARIABLES: [&'static str; 4] = ["URL", "SHA", "VERSION", "NEW_CHECK"];
pub fn new(template: String) -> Self {
Recipe { template }
}
pub fn validate_listed(&self) -> anyhow::Result<()> {
let missing_variables = Recipe::VARIABLES
.iter()
.filter(|&variable| !self.template.contains(variable))
.copied()
.collect::<Vec<_>>()
.join(", ");
if missing_variables.len() > 0 {
bail!("Missing variables: {}.", missing_variables)
}
Ok(())
}
pub fn validate_immediate(&self) -> anyhow::Result<()> {
for variable in Recipe::VARIABLES {
if !self.template.contains(variable) {
bail!("Missing variable {}.", variable)
}
}
Ok(())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment