lsor_core/
expr.rs

1use crate::driver::PushPrql;
2
3pub fn add<LHS, RHS>(lhs: LHS, rhs: RHS) -> Add<LHS, RHS> {
4    Add { lhs, rhs }
5}
6
7pub fn avg<Expr>(expr: Expr) -> Avg<Expr> {
8    Avg { expr }
9}
10
11pub fn case<const N: usize, Cond, Then>(
12    cases: impl Into<[WhenThen<Cond, Then>; N]>,
13) -> Case<N, Cond, Then> {
14    Case {
15        cases: cases.into(),
16    }
17}
18
19pub fn count() -> Count {
20    Count {}
21}
22
23pub fn sub<LHS, RHS>(lhs: LHS, rhs: RHS) -> Sub<LHS, RHS> {
24    Sub { lhs, rhs }
25}
26
27pub fn sum<Expr>(expr: Expr) -> Sum<Expr> {
28    Sum { expr }
29}
30
31pub fn when<Cond>(cond: Cond) -> When<Cond> {
32    When { cond }
33}
34
35pub struct Add<LHS, RHS> {
36    pub lhs: LHS,
37    pub rhs: RHS,
38}
39
40impl<LHS, RHS> PushPrql for Add<LHS, RHS>
41where
42    LHS: PushPrql,
43    RHS: PushPrql,
44{
45    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
46        self.lhs.push_to_driver(driver);
47        driver.push(" + ");
48        self.rhs.push_to_driver(driver);
49    }
50}
51
52pub struct Avg<Expr> {
53    pub expr: Expr,
54}
55
56impl<Expr> PushPrql for Avg<Expr>
57where
58    Expr: PushPrql,
59{
60    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
61        driver.push("average ");
62        self.expr.push_to_driver(driver);
63    }
64}
65
66pub struct Case<const N: usize, Cond, Then> {
67    pub cases: [WhenThen<Cond, Then>; N],
68}
69
70impl<const N: usize, Cond, Then> Case<N, Cond, Then> {
71    pub fn otherwise<Otherwise>(
72        self,
73        otherwise: Otherwise,
74    ) -> CaseOtherwise<N, Cond, Then, Otherwise> {
75        CaseOtherwise {
76            cases: self.cases,
77            otherwise,
78        }
79    }
80}
81
82impl<const N: usize, Cond, Then> PushPrql for Case<N, Cond, Then>
83where
84    Cond: PushPrql,
85    Then: PushPrql,
86{
87    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
88        driver.push("case [");
89        for (i, case) in self.cases.iter().enumerate() {
90            if i > 0 {
91                driver.push(',');
92            }
93            driver.push(' ');
94            case.push_to_driver(driver);
95        }
96        driver.push(" ]");
97    }
98}
99
100pub struct CaseOtherwise<const N: usize, Cond, Then, Otherwise> {
101    pub cases: [WhenThen<Cond, Then>; N],
102    pub otherwise: Otherwise,
103}
104
105impl<const N: usize, Cond, Then, Otherwise> PushPrql for CaseOtherwise<N, Cond, Then, Otherwise>
106where
107    Cond: PushPrql,
108    Then: PushPrql,
109    Otherwise: PushPrql,
110{
111    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
112        driver.push("case [");
113        for (i, case) in self.cases.iter().enumerate() {
114            if i > 0 {
115                driver.push(',');
116            }
117            driver.push(' ');
118            case.push_to_driver(driver);
119            driver.push(',');
120        }
121        driver.push(" true => ");
122        self.otherwise.push_to_driver(driver);
123        driver.push(" ]");
124    }
125}
126
127pub struct Count {}
128
129impl PushPrql for Count {
130    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
131        driver.push("count []");
132    }
133}
134
135pub struct Sub<LHS, RHS> {
136    pub lhs: LHS,
137    pub rhs: RHS,
138}
139
140impl<LHS, RHS> PushPrql for Sub<LHS, RHS>
141where
142    LHS: PushPrql,
143    RHS: PushPrql,
144{
145    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
146        self.lhs.push_to_driver(driver);
147        driver.push(" - ");
148        self.rhs.push_to_driver(driver);
149    }
150}
151
152pub struct Sum<Expr> {
153    pub expr: Expr,
154}
155
156impl<Expr> PushPrql for Sum<Expr>
157where
158    Expr: PushPrql,
159{
160    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
161        driver.push("sum ");
162        self.expr.push_to_driver(driver);
163    }
164}
165
166pub struct When<Cond> {
167    pub cond: Cond,
168}
169
170impl<Cond> When<Cond> {
171    pub fn then<Then>(self, then: Then) -> WhenThen<Cond, Then> {
172        WhenThen {
173            cond: self.cond,
174            then,
175        }
176    }
177}
178
179pub struct WhenThen<Cond, Then> {
180    pub cond: Cond,
181    pub then: Then,
182}
183
184impl<Cond, Then> PushPrql for WhenThen<Cond, Then>
185where
186    Cond: PushPrql,
187    Then: PushPrql,
188{
189    fn push_to_driver(&self, driver: &mut crate::driver::Driver) {
190        self.cond.push_to_driver(driver);
191        driver.push(" => ");
192        self.then.push_to_driver(driver);
193    }
194}