toasty_core/stmt/
expr_is_variant.rs

1use crate::schema::app::VariantId;
2
3use super::Expr;
4
5/// Tests whether an expression evaluates to a specific enum variant.
6///
7/// This is an application-level check that abstracts over the storage format
8/// (unit enums stored as bare I64 vs data-carrying enums stored as Records).
9/// The lowerer translates this into the appropriate DB-level comparison.
10///
11/// # Examples
12///
13/// ```text
14/// is_variant(expr, VariantId(2))  // true if expr is variant 2
15/// ```
16#[derive(Debug, Clone, PartialEq)]
17pub struct ExprIsVariant {
18    /// Expression evaluating to an enum value.
19    pub expr: Box<Expr>,
20    /// Identifies the variant to check against.
21    pub variant: VariantId,
22}
23
24impl Expr {
25    /// Creates a variant check expression testing whether `expr` is the given
26    /// `variant`.
27    pub fn is_variant(expr: impl Into<Self>, variant: VariantId) -> Self {
28        ExprIsVariant {
29            expr: Box::new(expr.into()),
30            variant,
31        }
32        .into()
33    }
34}
35
36impl From<ExprIsVariant> for Expr {
37    fn from(value: ExprIsVariant) -> Self {
38        Self::IsVariant(value)
39    }
40}