Skip to main content

radiate_expr/
logical.rs

1use super::ops::TrinaryOp;
2use crate::{Expr, ExprNode};
3
4#[derive(Clone, Debug, PartialEq)]
5pub struct When {
6    pub(crate) cond: Expr,
7}
8
9impl When {
10    pub fn new(cond: impl Into<Expr>) -> Self {
11        Self { cond: cond.into() }
12    }
13
14    pub fn then(self, then_expr: impl Into<Expr>) -> Then {
15        Then {
16            cond: self.cond,
17            then_expr: then_expr.into(),
18        }
19    }
20}
21
22impl From<When> for Expr {
23    fn from(val: When) -> Self {
24        val.then(true).otherwise(false)
25    }
26}
27
28pub struct Then {
29    pub(crate) cond: Expr,
30    pub(crate) then_expr: Expr,
31}
32
33impl Then {
34    pub fn otherwise(self, else_expr: impl Into<Expr>) -> Expr {
35        Expr::new(ExprNode::Trinary {
36            first: Box::new(self.cond),
37            second: Box::new(self.then_expr),
38            third: Box::new(else_expr.into()),
39            op: TrinaryOp::If,
40        })
41    }
42}