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