use cel::{Context, Program as CelProgram, Value};
#[derive(Debug, Clone)]
pub(crate) struct Program {
source: String,
}
impl Program {
pub(crate) fn compile(expr: &str) -> Result<Program, String> {
CelProgram::compile(expr).map_err(|e| e.to_string())?;
Ok(Program {
source: expr.to_string(),
})
}
pub(crate) fn eval(&self, files: &[String], subdirs: &[String]) -> bool {
let program = match CelProgram::compile(&self.source) {
Ok(p) => p,
Err(_) => return false,
};
let mut ctx = Context::default();
if ctx.add_variable("files", files.to_vec()).is_err() {
return false;
}
if ctx.add_variable("subdirs", subdirs.to_vec()).is_err() {
return false;
}
matches!(program.execute(&ctx), Ok(Value::Bool(true)))
}
}