toasty_core/stmt/
expr_project.rs

1use crate::stmt::ExprArg;
2
3use super::{Expr, Projection};
4
5/// Projects a field or element from a base expression.
6///
7/// A [projection] extracts a nested value from a record, tuple, or other
8/// composite type using a path of field indices.
9///
10/// # Examples
11///
12/// ```text
13/// project(record, [0])     // extracts the first field
14/// project(record, [1, 2])  // extracts field 1, then field 2
15/// ```
16///
17/// [projection]: https://en.wikipedia.org/wiki/Projection_(relational_algebra)
18#[derive(Debug, Clone, PartialEq)]
19pub struct ExprProject {
20    /// The expression to project from.
21    pub base: Box<Expr>,
22
23    /// The path specifying which field(s) to extract.
24    pub projection: Projection,
25}
26
27impl Expr {
28    pub fn project(base: impl Into<Self>, projection: impl Into<Projection>) -> Self {
29        ExprProject {
30            base: Box::new(base.into()),
31            projection: projection.into(),
32        }
33        .into()
34    }
35
36    pub fn arg_project(expr_arg: impl Into<ExprArg>, projection: impl Into<Projection>) -> Self {
37        Self::project(Self::arg(expr_arg), projection)
38    }
39
40    pub fn is_project(&self) -> bool {
41        matches!(self, Self::Project(..))
42    }
43
44    pub fn as_project(&self) -> &ExprProject {
45        match self {
46            Self::Project(expr_project) => expr_project,
47            _ => panic!(),
48        }
49    }
50}
51
52impl ExprProject {}
53
54impl From<ExprProject> for Expr {
55    fn from(value: ExprProject) -> Self {
56        Self::Project(value)
57    }
58}