use std::cmp::Ordering;
use crate::version::version_cmp;
use super::Env;
use super::expr::{self, Expr, Operand, RelOp};
pub(super) fn holds(when: Option<&str>, env: &Env, matched: Option<bool>) -> bool {
match when {
None => true,
Some(source) => match expr::parse(source) {
Ok(expr) => eval(&expr, env, matched),
Err(_) => false,
},
}
}
fn eval(expr: &Expr, env: &Env, matched: Option<bool>) -> bool {
match expr {
Expr::Or(left, right) => eval(left, env, matched) || eval(right, env, matched),
Expr::And(left, right) => eval(left, env, matched) && eval(right, env, matched),
Expr::Not(inner) => !eval(inner, env, matched),
Expr::Matched => matched.unwrap_or(false),
Expr::Bound(name) => env.contains_key(name),
Expr::Unbound(name) => !env.contains_key(name),
Expr::Compare { left, op, right } => compare(left, *op, right, env),
}
}
fn compare(left: &Operand, op: RelOp, right: &Operand, env: &Env) -> bool {
let (Some(left), Some(right)) = (resolve(left, env), resolve(right, env)) else {
return false;
};
match op {
RelOp::Eq => left.equals(&right),
RelOp::Ne => !left.equals(&right),
RelOp::Lt => left.order(&right) == Ordering::Less,
RelOp::Le => left.order(&right) != Ordering::Greater,
RelOp::Gt => left.order(&right) == Ordering::Greater,
RelOp::Ge => left.order(&right) != Ordering::Less,
}
}
fn resolve(operand: &Operand, env: &Env) -> Option<Value> {
match operand {
Operand::Int(value) => Some(Value::Number(*value)),
Operand::Text(text) => Some(Value::Text(text.clone())),
Operand::Var(name) => env.get(name).map(|value| Value::Text(value.clone())),
}
}
enum Value {
Number(i64),
Text(String),
}
impl Value {
fn equals(&self, other: &Value) -> bool {
match (self, other) {
(Value::Number(a), Value::Number(b)) => a == b,
_ => self.text() == other.text(),
}
}
fn order(&self, other: &Value) -> Ordering {
match (self, other) {
(Value::Number(a), Value::Number(b)) => a.cmp(b),
_ => version_cmp(&self.text(), &other.text()),
}
}
fn text(&self) -> String {
match self {
Value::Number(value) => value.to_string(),
Value::Text(text) => text.clone(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn env(pairs: &[(&str, &str)]) -> Env {
pairs
.iter()
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect()
}
#[test]
fn an_absent_guard_always_holds() {
assert!(holds(None, &Env::new(), Some(false)));
}
#[test]
fn matched_reads_the_step_result_only_where_one_is_in_scope() {
assert!(holds(Some("matched"), &Env::new(), Some(true)));
assert!(!holds(Some("matched"), &Env::new(), Some(false)));
assert!(!holds(Some("matched"), &Env::new(), None));
}
#[test]
fn bound_and_unbound_test_presence() {
let env = env(&[("version", "7.2.4")]);
assert!(holds(Some("bound(version)"), &env, None));
assert!(!holds(Some("unbound(version)"), &env, None));
assert!(!holds(Some("bound(missing)"), &env, None));
assert!(holds(Some("unbound(missing)"), &env, None));
}
#[test]
fn ordered_comparison_ranks_versions_numerically_not_lexically() {
let affected = env(&[("version", "8.2.0")]);
assert!(holds(Some("version < '8.3.1'"), &affected, None));
let patched = env(&[("version", "8.10.0")]);
assert!(!holds(Some("version < '8.3.1'"), &patched, None));
}
#[test]
fn equality_is_numeric_between_integers_and_textual_otherwise() {
assert!(holds(Some("3 == 3"), &Env::new(), None));
assert!(holds(Some("3 != 4"), &Env::new(), None));
let counts = env(&[("count", "3")]);
assert!(holds(Some("count == 3"), &counts, None));
let names = env(&[("name", "apache")]);
assert!(holds(Some("name != 'nginx'"), &names, None));
}
#[test]
fn a_comparison_on_an_unbound_variable_fails_closed() {
assert!(!holds(Some("version < '2'"), &Env::new(), None));
assert!(!holds(
Some("bound(version) and version < '2'"),
&Env::new(),
None
));
}
#[test]
fn boolean_combinators_short_circuit_with_the_right_precedence() {
let env = env(&[("version", "8.2.0")]);
assert!(holds(
Some("not matched and bound(version)"),
&env,
Some(false)
));
assert!(!holds(
Some("not matched and bound(version)"),
&env,
Some(true)
));
assert!(holds(
Some("matched and unbound(version) or bound(version)"),
&env,
Some(true)
));
}
#[test]
fn an_unparseable_guard_is_treated_as_unmet() {
assert!(!holds(Some("this is not a guard"), &Env::new(), Some(true)));
}
}