use serde_json::Value;
use super::{logic, Ambiguous, Data, Expression, PartialResult};
pub fn compute(args: &[Expression], data: &Data) -> Value {
let arr = match args.get(0).map(|arg| arg.compute(data)) {
Some(Value::Array(arr)) => arr,
_ => return Value::Bool(true),
};
let condition = match args.get(1) {
Some(expr) => expr,
None => return Value::Bool(true),
};
for elem in arr.iter() {
let result = condition.compute(&Data::from_json(&elem));
if logic::is_truthy(&result) {
return Value::Bool(false);
}
}
Value::Bool(true)
}
pub fn partial_compute(args: &[Expression], data: &Data) -> PartialResult {
let arr = match args
.get(0)
.map(|arg| arg.partial_compute(data))
.transpose()?
{
Some(Value::Array(arr)) => arr,
_ => return Ok(Value::Bool(true)),
};
let condition = match args.get(1) {
Some(expr) => expr,
None => return Ok(Value::Bool(true)),
};
let mut is_ambiguous = false;
for elem in arr.iter() {
match condition.partial_compute(&Data::from_json(&elem)) {
Err(Ambiguous) => is_ambiguous = true,
Ok(result) if logic::is_truthy(&result) => return Ok(Value::Bool(false)),
_ => (),
}
}
if is_ambiguous {
return Err(Ambiguous);
}
Ok(Value::Bool(true))
}