qail_core/ast/builders/
binary.rs

1//! Binary expression builders.
2
3use crate::ast::{BinaryOp, Expr};
4
5/// Create a binary expression (left op right)
6pub fn binary(left: impl Into<Expr>, op: BinaryOp, right: impl Into<Expr>) -> BinaryBuilder {
7    BinaryBuilder {
8        left: left.into(),
9        op,
10        right: right.into(),
11        alias: None,
12    }
13}
14
15/// Builder for binary expressions
16#[derive(Debug, Clone)]
17pub struct BinaryBuilder {
18    pub(crate) left: Expr,
19    pub(crate) op: BinaryOp,
20    pub(crate) right: Expr,
21    pub(crate) alias: Option<String>,
22}
23
24impl BinaryBuilder {
25    /// Add alias (AS name)
26    pub fn alias(mut self, name: &str) -> Expr {
27        self.alias = Some(name.to_string());
28        self.build()
29    }
30
31    /// Build the final Expr
32    pub fn build(self) -> Expr {
33        Expr::Binary {
34            left: Box::new(self.left),
35            op: self.op,
36            right: Box::new(self.right),
37            alias: self.alias,
38        }
39    }
40}
41
42impl From<BinaryBuilder> for Expr {
43    fn from(builder: BinaryBuilder) -> Self {
44        builder.build()
45    }
46}