toasty_core/stmt/
expr_any.rs

1use super::Expr;
2
3/// Returns `true` if any item in a collection evaluates to `true`.
4///
5/// Evaluates its inner expression and returns `true` if at least one item is
6/// truthy. Returns `false` for an empty collection.
7///
8/// # Examples
9///
10/// ```text
11/// any([true, false, false])  // returns `true`
12/// any([false, false])        // returns `false`
13/// any([])                    // returns `false`
14/// ```
15#[derive(Debug, Clone, PartialEq)]
16pub struct ExprAny {
17    /// Expression that evaluates to a list.
18    pub expr: Box<Expr>,
19}
20
21impl Expr {
22    /// Creates an `Any` expression that returns true if any item in the list evaluates to true.
23    ///
24    /// Returns false if the list is empty (matching Rust's `[].iter().any()` semantics).
25    pub fn any(expr: impl Into<Expr>) -> Self {
26        ExprAny {
27            expr: Box::new(expr.into()),
28        }
29        .into()
30    }
31
32    /// Returns true if this is an `Any` expression
33    pub fn is_any(&self) -> bool {
34        matches!(self, Self::Any(_))
35    }
36}
37
38impl From<ExprAny> for Expr {
39    fn from(value: ExprAny) -> Self {
40        Self::Any(value)
41    }
42}