toasty-core 0.2.0

Core types, schema representations, and driver interface for Toasty
Documentation
use super::Expr;

use std::ops;

/// A logical "and" of multiple expressions.
///
/// Returns `true` only if all operands evaluate to `true`. An `ExprAnd` always
/// has at least two operands; use [`Expr::and_from_vec`] which returns
/// `Expr::Value(true)` for empty input and unwraps single-element input.
///
/// # Examples
///
/// ```text
/// and(a, b, c)  // returns `true` if `a`, `b`, and `c` are all `true`
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct ExprAnd {
    /// The expressions to "and" together.
    pub operands: Vec<Expr>,
}

impl Expr {
    /// Creates an AND expression from two operands.
    ///
    /// Flattens nested ANDs: `and(and(a, b), c)` produces `and(a, b, c)`.
    /// Short-circuits on `true`: `and(true, x)` returns `x`.
    pub fn and(lhs: impl Into<Self>, rhs: impl Into<Self>) -> Self {
        let mut lhs = lhs.into();
        let rhs = rhs.into();

        match (&mut lhs, rhs) {
            (expr, rhs) if expr.is_true() => rhs,
            (_, expr) if expr.is_true() => lhs,
            (Self::And(lhs_and), Self::And(rhs_and)) => {
                lhs_and.operands.extend(rhs_and.operands);
                lhs
            }
            (Self::And(lhs_and), rhs) => {
                lhs_and.operands.push(rhs);
                lhs
            }
            (_, Self::And(mut rhs_and)) => {
                rhs_and.operands.push(lhs);
                rhs_and.into()
            }
            (_, rhs) => ExprAnd {
                operands: vec![lhs, rhs],
            }
            .into(),
        }
    }

    /// Creates an AND expression from a vector of operands.
    ///
    /// Returns `Expr::Value(true)` for an empty vector and unwraps
    /// single-element vectors into the element itself.
    pub fn and_from_vec(operands: Vec<Self>) -> Self {
        if operands.is_empty() {
            return true.into();
        }

        if operands.len() == 1 {
            return operands.into_iter().next().unwrap();
        }

        ExprAnd { operands }.into()
    }
}

impl ops::Deref for ExprAnd {
    type Target = [Expr];

    fn deref(&self) -> &Self::Target {
        self.operands.deref()
    }
}

impl<'a> IntoIterator for &'a ExprAnd {
    type IntoIter = std::slice::Iter<'a, Expr>;
    type Item = &'a Expr;

    fn into_iter(self) -> Self::IntoIter {
        self.operands.iter()
    }
}

impl<'a> IntoIterator for &'a mut ExprAnd {
    type IntoIter = std::slice::IterMut<'a, Expr>;
    type Item = &'a mut Expr;

    fn into_iter(self) -> Self::IntoIter {
        self.operands.iter_mut()
    }
}

impl From<ExprAnd> for Expr {
    fn from(value: ExprAnd) -> Self {
        Self::And(value)
    }
}