toasty_core/stmt/expr_match.rs
1use super::{Expr, Value};
2
3/// A match expression that dispatches on a subject expression.
4///
5/// Each arm matches the subject against a constant value pattern and evaluates
6/// the corresponding expression. `Expr::Match` is never serialized to SQL — it
7/// is either evaluated in the engine (for writes) or eliminated by the
8/// simplifier before the plan stage (for reads/queries).
9#[derive(Debug, Clone, PartialEq)]
10pub struct ExprMatch {
11 /// The expression to dispatch on.
12 pub subject: Box<Expr>,
13
14 /// The match arms, in order.
15 pub arms: Vec<MatchArm>,
16
17 /// Fallback expression evaluated when no arm matches.
18 pub else_expr: Box<Expr>,
19}
20
21/// A single arm in a match expression.
22#[derive(Debug, Clone, PartialEq)]
23pub struct MatchArm {
24 /// The constant value pattern this arm matches against.
25 pub pattern: Value,
26
27 /// The expression to evaluate when the pattern matches.
28 pub expr: Expr,
29}
30
31impl Expr {
32 /// Creates a `Match` expression that dispatches on `subject`.
33 pub fn match_expr(
34 subject: impl Into<Self>,
35 arms: Vec<MatchArm>,
36 else_expr: impl Into<Self>,
37 ) -> Self {
38 ExprMatch {
39 subject: Box::new(subject.into()),
40 arms,
41 else_expr: Box::new(else_expr.into()),
42 }
43 .into()
44 }
45}
46
47impl From<ExprMatch> for Expr {
48 fn from(value: ExprMatch) -> Self {
49 Self::Match(value)
50 }
51}