mod conditional;
mod constant;
mod holder;
pub use conditional::*;
use crate::{ExprTrait, expr::Expr, types::LogicalChainOper};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConditionType {
Any,
All,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Condition {
pub(crate) negate: bool,
pub(crate) condition_type: ConditionType,
pub(crate) conditions: Vec<ConditionExpression>,
}
pub type Cond = Condition;
impl From<Expr> for Condition {
fn from(expr: Expr) -> Self {
Condition {
negate: false,
condition_type: ConditionType::All,
conditions: vec![ConditionExpression::Expr(expr)],
}
}
}
impl From<ConditionExpression> for Condition {
fn from(ce: ConditionExpression) -> Self {
Condition {
negate: false,
condition_type: ConditionType::All,
conditions: vec![ce],
}
}
}
pub trait IntoCondition: Into<Condition> {
#[inline(always)]
fn into_condition(self) -> Condition {
self.into()
}
}
impl<T> IntoCondition for T where T: Into<Condition> {}
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum ConditionExpression {
Condition(Condition),
Expr(Expr),
}
#[derive(Default, Debug, Clone, PartialEq)]
pub enum ConditionHolderContents {
#[default]
Empty,
Chain(Vec<LogicalChainOper>),
Condition(Condition),
}
#[derive(Default, Debug, Clone, PartialEq)]
pub struct ConditionHolder {
pub contents: ConditionHolderContents,
}
impl Condition {
#[allow(clippy::should_implement_trait)]
pub fn add<C>(mut self, condition: C) -> Self
where
C: Into<Condition>,
{
let condition: Condition = condition.into();
let condition: ConditionExpression = condition.into();
self.conditions.push(condition);
self
}
#[allow(clippy::should_implement_trait)]
pub fn add_option<C>(self, other: Option<C>) -> Self
where
C: Into<Condition>,
{
if let Some(other) = other {
self.add(other)
} else {
self
}
}
pub fn any() -> Condition {
Condition {
negate: false,
condition_type: ConditionType::Any,
conditions: Vec::new(),
}
}
pub fn all() -> Condition {
Condition {
negate: false,
condition_type: ConditionType::All,
conditions: Vec::new(),
}
}
#[allow(clippy::should_implement_trait)]
pub fn not(mut self) -> Self {
self.negate = !self.negate;
self
}
pub fn is_empty(&self) -> bool {
self.conditions.is_empty()
}
pub fn len(&self) -> usize {
self.conditions.len()
}
}
impl From<Condition> for Expr {
fn from(cond: Condition) -> Self {
let mut inner_exprs = vec![];
for ce in cond.conditions {
inner_exprs.push(match ce {
ConditionExpression::Condition(c) => c.into(),
ConditionExpression::Expr(e) => e,
});
}
let mut inner_exprs_into_iter = inner_exprs.into_iter();
let expr = if let Some(first_expr) = inner_exprs_into_iter.next() {
let mut out_expr = first_expr;
for e in inner_exprs_into_iter {
out_expr = match cond.condition_type {
ConditionType::Any => out_expr.or(e),
ConditionType::All => out_expr.and(e),
};
}
out_expr
} else {
Expr::Constant(match cond.condition_type {
ConditionType::Any => false.into(),
ConditionType::All => true.into(),
})
};
if cond.negate { expr.not() } else { expr }
}
}
impl From<ConditionExpression> for Expr {
fn from(ce: ConditionExpression) -> Self {
match ce {
ConditionExpression::Condition(c) => c.into(),
ConditionExpression::Expr(e) => e,
}
}
}
impl From<Condition> for ConditionExpression {
fn from(condition: Condition) -> Self {
ConditionExpression::Condition(condition)
}
}
impl From<Expr> for ConditionExpression {
fn from(condition: Expr) -> Self {
ConditionExpression::Expr(condition)
}
}
#[macro_export]
macro_rules! any {
( $( $x:expr ),* $(,)?) => {
{
let mut tmp = $crate::Condition::any();
$(
tmp = tmp.add($x);
)*
tmp
}
};
}
#[macro_export]
macro_rules! all {
( $( $x:expr ),* $(,)?) => {
{
let mut tmp = $crate::Condition::all();
$(
tmp = tmp.add($x);
)*
tmp
}
};
}