bql 0.1.0

BQL — Bridge Query Language parser and formatter (Rust, generated).
Documentation
//! Generated ergonomic constructors. Do not edit by hand.
//!
//! `new_borrowed` / `<variant>_borrowed` build the borrowed (`Cow::Borrowed`)
//! form; `new_owned` / `<variant>_owned` build the owned (`Cow::Owned`, `'static`)
//! form. `Expr` also gets `_expr` variant constructors plus `and` / `or` builder
//! combinators that flatten into the n-ary boolean nodes.

use std::borrow::Cow;

use crate::ast::*;

impl<'a> And<'a> {
    /// Build `And` from already-boxed operands.
    pub fn new_boxed(operands: Vec<Box<Expr<'a>>>) -> Self {
        And { operands }
    }
    /// Build `And` from operands (boxed for you).
    pub fn new(operands: Vec<Expr<'a>>) -> Self {
        And {
            operands: operands.into_iter().map(Box::new).collect(),
        }
    }
}

impl<'a> FromIterator<Expr<'a>> for And<'a> {
    fn from_iter<I: IntoIterator<Item = Expr<'a>>>(iter: I) -> Self {
        And {
            operands: iter.into_iter().map(Box::new).collect(),
        }
    }
}

impl<'a> Between<'a> {
    /// Build `Between` borrowing its text from the input (`Cow::Borrowed`).
    pub fn new_borrowed<S0: AsRef<str> + ?Sized>(
        field: &'a S0,
        lo: Value<'a>,
        hi: Value<'a>,
    ) -> Self {
        Between {
            field: Cow::Borrowed(field.as_ref()),
            lo,
            hi,
        }
    }
}

impl Between<'static> {
    /// Build an owned `Between` from owned strings (`Cow::Owned`).
    pub fn new_owned<S0: Into<String>>(field: S0, lo: Value<'static>, hi: Value<'static>) -> Self {
        Between {
            field: Cow::Owned(field.into()),
            lo,
            hi,
        }
    }
}

impl<'a> Compare<'a> {
    /// Build `Compare` borrowing its text from the input (`Cow::Borrowed`).
    pub fn new_borrowed<S0: AsRef<str> + ?Sized>(
        field: &'a S0,
        op: BinOp,
        value: Value<'a>,
    ) -> Self {
        Compare {
            field: Cow::Borrowed(field.as_ref()),
            op,
            value,
        }
    }
}

impl Compare<'static> {
    /// Build an owned `Compare` from owned strings (`Cow::Owned`).
    pub fn new_owned<S0: Into<String>>(field: S0, op: BinOp, value: Value<'static>) -> Self {
        Compare {
            field: Cow::Owned(field.into()),
            op,
            value,
        }
    }
}

impl<'a> Empty<'a> {
    /// Build `Empty` borrowing its text from the input (`Cow::Borrowed`).
    pub fn new_borrowed<S0: AsRef<str> + ?Sized>(field: &'a S0) -> Self {
        Empty {
            field: Cow::Borrowed(field.as_ref()),
        }
    }
}

impl Empty<'static> {
    /// Build an owned `Empty` from owned strings (`Cow::Owned`).
    pub fn new_owned<S0: Into<String>>(field: S0) -> Self {
        Empty {
            field: Cow::Owned(field.into()),
        }
    }
}

impl<'a> Expr<'a> {
    /// `Expr::And` from already-boxed operands.
    pub fn and_expr_boxed(operands: Vec<Box<Expr<'a>>>) -> Self {
        Expr::And(Box::new(And::new_boxed(operands)))
    }
    /// `Expr::And` from operands (boxed for you).
    pub fn and_expr(operands: Vec<Expr<'a>>) -> Self {
        Expr::And(Box::new(And::new(operands)))
    }
    /// `Expr::Or` from already-boxed operands.
    pub fn or_expr_boxed(operands: Vec<Box<Expr<'a>>>) -> Self {
        Expr::Or(Box::new(Or::new_boxed(operands)))
    }
    /// `Expr::Or` from operands (boxed for you).
    pub fn or_expr(operands: Vec<Expr<'a>>) -> Self {
        Expr::Or(Box::new(Or::new(operands)))
    }
    /// `Expr::Not` wrapping an operand.
    pub fn not_expr(inner: Expr<'a>) -> Self {
        Expr::Not(Box::new(inner))
    }
    /// `Expr::Compare`.
    pub fn compare_expr(v: Compare<'a>) -> Self {
        Expr::Compare(v)
    }
    /// `Expr::Between`.
    pub fn between_expr(v: Between<'a>) -> Self {
        Expr::Between(v)
    }
    /// `Expr::NotBetween`.
    pub fn not_between_expr(v: Between<'a>) -> Self {
        Expr::NotBetween(v)
    }
    /// `Expr::In`.
    pub fn in_expr(v: In<'a>) -> Self {
        Expr::In(v)
    }
    /// `Expr::NotIn`.
    pub fn not_in_expr(v: In<'a>) -> Self {
        Expr::NotIn(v)
    }
    /// `Expr::IsEmpty`.
    pub fn is_empty_expr(v: Empty<'a>) -> Self {
        Expr::IsEmpty(v)
    }
    /// `Expr::IsNull`.
    pub fn is_null_expr(v: Empty<'a>) -> Self {
        Expr::IsNull(v)
    }
    /// `Expr::IsNotEmpty`.
    pub fn is_not_empty_expr(v: Empty<'a>) -> Self {
        Expr::IsNotEmpty(v)
    }
    /// `Expr::IsNotNull`.
    pub fn is_not_null_expr(v: Empty<'a>) -> Self {
        Expr::IsNotNull(v)
    }
    /// Combine with `other` under `Or`, flattening either side already `Or`.
    pub fn or(self, other: Expr<'a>) -> Self {
        let mut operands = match self {
            Expr::Or(a) => a.operands,
            lhs => vec![Box::new(lhs)],
        };
        match other {
            Expr::Or(b) => operands.extend(b.operands),
            rhs => operands.push(Box::new(rhs)),
        }
        Expr::Or(Box::new(Or { operands }))
    }
    /// Combine with `other` under `And`, flattening either side already `And`.
    pub fn and(self, other: Expr<'a>) -> Self {
        let mut operands = match self {
            Expr::And(a) => a.operands,
            lhs => vec![Box::new(lhs)],
        };
        match other {
            Expr::And(b) => operands.extend(b.operands),
            rhs => operands.push(Box::new(rhs)),
        }
        Expr::And(Box::new(And { operands }))
    }
}

impl<'a> In<'a> {
    /// Build `In` borrowing its text from the input (`Cow::Borrowed`).
    pub fn new_borrowed<S0: AsRef<str> + ?Sized>(field: &'a S0, values: Vec<Value<'a>>) -> Self {
        In {
            field: Cow::Borrowed(field.as_ref()),
            values,
        }
    }
}

impl In<'static> {
    /// Build an owned `In` from owned strings (`Cow::Owned`).
    pub fn new_owned<S0: Into<String>>(field: S0, values: Vec<Value<'static>>) -> Self {
        In {
            field: Cow::Owned(field.into()),
            values,
        }
    }
}

impl Now {
    /// Construct a `Now`.
    pub fn new() -> Self {
        Now {}
    }
}

impl<'a> Or<'a> {
    /// Build `Or` from already-boxed operands.
    pub fn new_boxed(operands: Vec<Box<Expr<'a>>>) -> Self {
        Or { operands }
    }
    /// Build `Or` from operands (boxed for you).
    pub fn new(operands: Vec<Expr<'a>>) -> Self {
        Or {
            operands: operands.into_iter().map(Box::new).collect(),
        }
    }
}

impl<'a> FromIterator<Expr<'a>> for Or<'a> {
    fn from_iter<I: IntoIterator<Item = Expr<'a>>>(iter: I) -> Self {
        Or {
            operands: iter.into_iter().map(Box::new).collect(),
        }
    }
}

impl<'a> Participant<'a> {
    /// Build `Participant` borrowing its text from the input (`Cow::Borrowed`).
    pub fn new_borrowed<S0: AsRef<str> + ?Sized, S1: AsRef<str> + ?Sized>(
        r#type: &'a S0,
        id: &'a S1,
    ) -> Self {
        Participant {
            r#type: Cow::Borrowed(r#type.as_ref()),
            id: Cow::Borrowed(id.as_ref()),
        }
    }
}

impl Participant<'static> {
    /// Build an owned `Participant` from owned strings (`Cow::Owned`).
    pub fn new_owned<S0: Into<String>, S1: Into<String>>(r#type: S0, id: S1) -> Self {
        Participant {
            r#type: Cow::Owned(r#type.into()),
            id: Cow::Owned(id.into()),
        }
    }
}

impl<'a> Value<'a> {
    /// `Value::String` borrowing from the input.
    pub fn string_borrowed<S: AsRef<str> + ?Sized>(s: &'a S) -> Self {
        Value::String(Cow::Borrowed(s.as_ref()))
    }
    /// `Value::Enum` borrowing from the input.
    pub fn enum_borrowed<S: AsRef<str> + ?Sized>(s: &'a S) -> Self {
        Value::Enum(Cow::Borrowed(s.as_ref()))
    }
    /// `Value::Integer` borrowing from the input.
    pub fn integer_borrowed<S: AsRef<str> + ?Sized>(s: &'a S) -> Self {
        Value::Integer(Cow::Borrowed(s.as_ref()))
    }
    /// `Value::Float` borrowing from the input.
    pub fn float_borrowed<S: AsRef<str> + ?Sized>(s: &'a S) -> Self {
        Value::Float(Cow::Borrowed(s.as_ref()))
    }
    /// `Value::Boolean`.
    pub fn boolean(v: Boolean) -> Self {
        Value::Boolean(v)
    }
    /// `Value::Date` borrowing from the input.
    pub fn date_borrowed<S: AsRef<str> + ?Sized>(s: &'a S) -> Self {
        Value::Date(Cow::Borrowed(s.as_ref()))
    }
    /// `Value::DateTime` borrowing from the input.
    pub fn date_time_borrowed<S: AsRef<str> + ?Sized>(s: &'a S) -> Self {
        Value::DateTime(Cow::Borrowed(s.as_ref()))
    }
    /// `Value::Participant`.
    pub fn participant(v: Participant<'a>) -> Self {
        Value::Participant(v)
    }
    /// `Value::Now`.
    pub fn now() -> Self {
        Value::Now(Now {})
    }
}

impl Value<'static> {
    /// Owned `Value::String`.
    pub fn string_owned<S: Into<String>>(s: S) -> Self {
        Value::String(Cow::Owned(s.into()))
    }
    /// Owned `Value::Enum`.
    pub fn enum_owned<S: Into<String>>(s: S) -> Self {
        Value::Enum(Cow::Owned(s.into()))
    }
    /// Owned `Value::Integer`.
    pub fn integer_owned<S: Into<String>>(s: S) -> Self {
        Value::Integer(Cow::Owned(s.into()))
    }
    /// Owned `Value::Float`.
    pub fn float_owned<S: Into<String>>(s: S) -> Self {
        Value::Float(Cow::Owned(s.into()))
    }
    /// Owned `Value::Date`.
    pub fn date_owned<S: Into<String>>(s: S) -> Self {
        Value::Date(Cow::Owned(s.into()))
    }
    /// Owned `Value::DateTime`.
    pub fn date_time_owned<S: Into<String>>(s: S) -> Self {
        Value::DateTime(Cow::Owned(s.into()))
    }
    /// `Value::Integer` from a native `i32` (the classified integer range).
    pub fn integer(v: i32) -> Self {
        Value::Integer(Cow::Owned(v.to_string()))
    }
    /// `Value::Float` from a native `f64`, rendered as a canonical BQL float
    /// lexeme (always carrying a decimal point or exponent so it never
    /// re-lexes as an integer). Errors for a non-finite value, which has
    /// no BQL representation.
    pub fn float(v: f64) -> crate::error::BqlResult<Self> {
        if !v.is_finite() {
            return Err(crate::error::BqlError::new(
                0,
                "non-finite float has no BQL representation",
            ));
        }
        let mut s = format!("{v}");
        if !s.bytes().any(|b| matches!(b, b'.' | b'e' | b'E')) {
            s.push_str(".0");
        }
        Ok(Value::Float(Cow::Owned(s)))
    }
}