qail_core/ast/builders/
cast.rs

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