qraft-core 0.1.2

Core type system, query model, decoding, and SQL lowering primitives for qraft.
Documentation
//! Unary predicate expressions.

use std::marker::PhantomData;

use crate::{
    Boolean, LowerCompatible,
    expression::Expression,
    lower::{Instructions, LowerCtx},
};

/// Postfix predicates lowered as unary instruction variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PostfixOperator {
    Null { negated: bool },
    False,
    True,
}

/// Prefix unary operators currently supported by the builder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnaryOperator {
    Not,
}

/// A typed unary expression.
pub struct Unary<L, T> {
    lhs: L,
    op: UnaryOperator,
    marker: PhantomData<T>,
}

/// Builds a `not ...` expression for boolean values.
pub fn not<E, T>(e: E) -> Unary<E, T>
where
    T: Boolean,
    E: LowerCompatible<T>,
{
    Unary {
        lhs: e,
        op: UnaryOperator::Not,
        marker: PhantomData,
    }
}

#[qraft_expression_macro::as_expression]
impl<L, T> Expression for Unary<L, T>
where
    T: Boolean,
    L: LowerCompatible<T>,
{
    type Type = T;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let rhs = self.lhs.lower_compatible(ctx);
        ctx.instrs.push_unary(self.op, rhs);
        rhs + 1
    }
}