use crate::ast::*;
#[derive(Debug, PartialEq, Clone)]
pub enum ConditionTree<'a> {
And(Box<Expression<'a>>, Box<Expression<'a>>),
Or(Box<Expression<'a>>, Box<Expression<'a>>),
Not(Box<Expression<'a>>),
Single(Box<Expression<'a>>),
NoCondition,
NegativeCondition,
}
impl<'a> ConditionTree<'a> {
#[inline]
pub fn and<E, J>(left: E, right: J) -> ConditionTree<'a>
where
E: Into<Expression<'a>>,
J: Into<Expression<'a>>,
{
ConditionTree::And(Box::new(left.into()), Box::new(right.into()))
}
#[inline]
pub fn or<E, J>(left: E, right: J) -> ConditionTree<'a>
where
E: Into<Expression<'a>>,
J: Into<Expression<'a>>,
{
ConditionTree::Or(Box::new(left.into()), Box::new(right.into()))
}
#[inline]
pub fn not<E>(left: E) -> ConditionTree<'a>
where
E: Into<Expression<'a>>,
{
ConditionTree::Not(Box::new(left.into()))
}
#[inline]
pub fn single<E>(left: E) -> ConditionTree<'a>
where
E: Into<Expression<'a>>,
{
ConditionTree::Single(Box::new(left.into()))
}
#[inline]
pub fn invert_if(self, invert: bool) -> ConditionTree<'a> {
if invert {
self.not()
} else {
self
}
}
}
impl<'a> Default for ConditionTree<'a> {
#[inline]
fn default() -> Self {
ConditionTree::NoCondition
}
}
impl<'a> From<ConditionTree<'a>> for Expression<'a> {
#[inline]
fn from(ct: ConditionTree<'a>) -> Self {
Expression::ConditionTree(ct)
}
}
impl<'a> From<Select<'a>> for ConditionTree<'a> {
#[inline]
fn from(sel: Select<'a>) -> Self {
ConditionTree::single(Expression::Value(Box::new(sel.into())))
}
}