use syntax::ast::{Cond, CurrencySign, Expr, Operand, RelOp, Upon};
pub fn numval_currency(signs: &[CurrencySign]) -> String {
match signs {
[only] => only.value.clone(),
_ => "$".to_owned(),
}
}
pub fn flatten_and<'c>(cond: &'c Cond, out: &mut Vec<&'c Cond>) {
match cond {
Cond::And(a, b) => {
flatten_and(a, out);
flatten_and(b, out);
}
other => out.push(other),
}
}
pub fn key_term<'c>(terms: &[&'c Cond], key: &str) -> Option<(&'c Expr, &'c Expr)> {
let is_key = |e: &Expr| matches!(e, Expr::Operand(Operand::Ref(r)) if r.name == key);
terms.iter().find_map(|t| match t {
Cond::Rel(a, RelOp::Eq, b) if is_key(a) => Some((a, b)),
Cond::Rel(a, RelOp::Eq, b) if is_key(b) => Some((b, a)),
_ => None,
})
}
pub fn upon_console(upon: Option<&Upon>) -> bool {
upon.is_some_and(|u| u.device == "CONSOLE")
}