radiate-expr 1.3.1

A Rust library for genetic algorithms and artificial evolution.
Documentation
use super::ops::TrinaryOp;
use crate::{Expr, ExprNode};

#[derive(Clone, Debug, PartialEq)]
pub struct When {
    pub(crate) cond: Expr,
}

impl When {
    pub fn new(cond: impl Into<Expr>) -> Self {
        Self { cond: cond.into() }
    }

    pub fn then(self, then_expr: impl Into<Expr>) -> Then {
        Then {
            cond: self.cond,
            then_expr: then_expr.into(),
        }
    }
}

impl From<When> for Expr {
    fn from(val: When) -> Self {
        val.then(true).otherwise(false)
    }
}

pub struct Then {
    pub(crate) cond: Expr,
    pub(crate) then_expr: Expr,
}

impl Then {
    pub fn otherwise(self, else_expr: impl Into<Expr>) -> Expr {
        Expr::new(ExprNode::Trinary {
            first: Box::new(self.cond),
            second: Box::new(self.then_expr),
            third: Box::new(else_expr.into()),
            op: TrinaryOp::If,
        })
    }
}