toasty_core/stmt/expr_map.rs
1use super::Expr;
2
3/// A map/transform operation over a collection.
4///
5/// [`ExprMap`] applies a transformation expression to each item in a base
6/// collection. Within the `map` expression, `Expr::arg(n)` refers to elements
7/// of each item:
8///
9/// - For simple values, `arg(0)` is the item itself.
10/// - For records, `arg(0)` is field 0, `arg(1)` is field 1, etc.
11///
12/// # Examples
13///
14/// ## Simple values
15///
16/// ```text
17/// map([1, 2, 3], x => x == field)
18/// ```
19///
20/// Here `base` is `[1, 2, 3]` and `map` is `arg(0) == field`.
21///
22/// ## Records
23///
24/// ```text
25/// map([{1, 2}, {3, 4}], r => r.0 + r.1)
26/// ```
27///
28/// Here each item is a record with two fields. `arg(0)` refers to the first
29/// field and `arg(1)` refers to the second field of each record.
30#[derive(Debug, Clone, PartialEq)]
31pub struct ExprMap {
32 /// The collection expression to iterate over.
33 pub base: Box<Expr>,
34
35 /// The transformation to apply to each item. Use `Expr::arg(n)` to
36 /// reference elements of the current item being mapped.
37 pub map: Box<Expr>,
38}
39
40impl Expr {
41 pub fn map(base: impl Into<Self>, map: impl Into<Self>) -> Self {
42 ExprMap {
43 base: Box::new(base.into()),
44 map: Box::new(map.into()),
45 }
46 .into()
47 }
48
49 pub fn as_map(&self) -> &ExprMap {
50 match self {
51 Self::Map(expr) => expr,
52 _ => todo!(),
53 }
54 }
55}
56
57impl From<ExprMap> for Expr {
58 fn from(value: ExprMap) -> Self {
59 Self::Map(value)
60 }
61}