use crate::layer0::guard_grammar::{
DIAG_NEGATED_GUARD_PASSED, IDX_ARG_2, PRIM_AND, PRIM_NOT, PRIM_OR, PROFILE_GUARD, PROFILE_RULE, VERDICT_CHAIN, VERDICT_NOT,
};
pub use crate::layer0::guard_term_eval::compile_cache_key as compile_guard_cache_key;
pub use crate::layer0::guard_term_parser::compile_guard_term as compile_guard_to_term;
use crate::layer0::term::Term;
use crate::layer0::vocabulary::{
GUARD_OP_NOT, GUARD_OUTCOME_FAIL, GUARD_OUTCOME_PASS, GUARD_OUTCOME_UNMATCHED,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GuardVerdict {
Pass,
Fail(String),
Unmatched(String),
}
impl GuardVerdict {
pub fn outcome(&self) -> &'static str {
if matches!(self, GuardVerdict::Pass) {
GUARD_OUTCOME_PASS
} else if matches!(self, GuardVerdict::Fail(_)) {
GUARD_OUTCOME_FAIL
} else {
GUARD_OUTCOME_UNMATCHED
}
}
pub fn is_pass(&self) -> bool {
matches!(self, GuardVerdict::Pass)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum GuardAst {
Or(Box<GuardAst>, Box<GuardAst>),
And(Box<GuardAst>, Box<GuardAst>),
Not(Box<GuardAst>),
Atom(String),
}
fn term_to_guard_ast(term: &Term) -> GuardAst {
if let Term::Prim(name, args) = term {
if name == PRIM_OR && args.len() >= IDX_ARG_2 {
return GuardAst::Or(Box::new(term_to_guard_ast(&args[0])), Box::new(term_to_guard_ast(&args[1])));
}
if name == PRIM_AND && args.len() >= IDX_ARG_2 {
return GuardAst::And(Box::new(term_to_guard_ast(&args[0])), Box::new(term_to_guard_ast(&args[1])));
}
if name == PRIM_NOT && !args.is_empty() {
return GuardAst::Not(Box::new(term_to_guard_ast(&args[0])));
}
}
GuardAst::Atom(crate::layer0::term::render(term))
}
pub fn parse_guard_expression(source: &str, allow_word_ops: bool) -> Result<GuardAst, String> {
let profile = if allow_word_ops { PROFILE_RULE } else { PROFILE_GUARD };
compile_guard_to_term(source, profile)
.map(|t| term_to_guard_ast(&t))
.map_err(|_| source.to_string())
}
fn chain_rank(outcome: &str) -> usize {
VERDICT_CHAIN.iter().position(|&v| v == outcome).unwrap_or(1)
}
pub fn eval_kleene_not(verdict: GuardVerdict, label: &str) -> GuardVerdict {
let target = VERDICT_NOT
.iter()
.find(|&&(k, _)| k == verdict.outcome())
.map(|&(_, v)| v)
.unwrap_or(GUARD_OUTCOME_UNMATCHED);
if target == GUARD_OUTCOME_FAIL {
GuardVerdict::Fail([DIAG_NEGATED_GUARD_PASSED, label].concat())
} else if target == GUARD_OUTCOME_PASS {
GuardVerdict::Pass
} else {
verdict
}
}
pub fn eval_kleene_and<F: FnOnce() -> GuardVerdict>(left: GuardVerdict, eval_right: F) -> GuardVerdict {
if left.outcome() == GUARD_OUTCOME_FAIL {
return left;
}
let right = eval_right();
let target = VERDICT_CHAIN[chain_rank(left.outcome()).min(chain_rank(right.outcome()))];
if left.outcome() == target { left } else { right }
}
pub fn eval_kleene_or<F: FnOnce() -> GuardVerdict>(left: GuardVerdict, eval_right: F) -> GuardVerdict {
if left.outcome() == GUARD_OUTCOME_PASS {
return left;
}
let right = eval_right();
let target = VERDICT_CHAIN[chain_rank(left.outcome()).max(chain_rank(right.outcome()))];
if left.outcome() == target { left } else { right }
}
pub fn evaluate_guard_ast<F: Fn(&str) -> GuardVerdict>(ast: &GuardAst, eval_atom: &F) -> GuardVerdict {
match ast {
GuardAst::Atom(text) => eval_atom(text),
GuardAst::Not(inner) => {
let label = match inner.as_ref() {
GuardAst::Atom(t) => t.as_str(),
_ => GUARD_OP_NOT,
};
eval_kleene_not(evaluate_guard_ast(inner, eval_atom), label)
}
GuardAst::And(l, r) => eval_kleene_and(evaluate_guard_ast(l, eval_atom), || evaluate_guard_ast(r, eval_atom)),
GuardAst::Or(l, r) => eval_kleene_or(evaluate_guard_ast(l, eval_atom), || evaluate_guard_ast(r, eval_atom)),
}
}