use crate::ast::{Ast, Constraint, Operator, Value};
fn leaf(field: &str, operator: Operator, value: Value) -> Ast {
Ast::Constraint(Constraint {
field: field.to_owned(),
operator,
value,
})
}
pub fn eq(field: &str, value: impl Into<Value>) -> Ast {
leaf(field, Operator::Eq, value.into())
}
pub fn neq(field: &str, value: impl Into<Value>) -> Ast {
leaf(field, Operator::Neq, value.into())
}
pub fn lt(field: &str, value: impl Into<Value>) -> Ast {
leaf(field, Operator::Lt, value.into())
}
pub fn lte(field: &str, value: impl Into<Value>) -> Ast {
leaf(field, Operator::Lte, value.into())
}
pub fn gt(field: &str, value: impl Into<Value>) -> Ast {
leaf(field, Operator::Gt, value.into())
}
pub fn gte(field: &str, value: impl Into<Value>) -> Ast {
leaf(field, Operator::Gte, value.into())
}
pub fn in_<V: Into<Value>>(field: &str, values: impl IntoIterator<Item = V>) -> Ast {
leaf(
field,
Operator::In,
Value::List(values.into_iter().map(Into::into).collect()),
)
}
pub fn out<V: Into<Value>>(field: &str, values: impl IntoIterator<Item = V>) -> Ast {
leaf(
field,
Operator::Out,
Value::List(values.into_iter().map(Into::into).collect()),
)
}
pub fn between(field: &str, lo: impl Into<Value>, hi: impl Into<Value>) -> Ast {
leaf(
field,
Operator::Between,
Value::List(vec![lo.into(), hi.into()]),
)
}
pub fn null(field: &str) -> Ast {
leaf(field, Operator::Null, Value::Bool(true))
}
pub fn not_null(field: &str) -> Ast {
leaf(field, Operator::NotNull, Value::Bool(true))
}
pub fn like(field: &str, pattern: impl Into<String>) -> Ast {
leaf(field, Operator::Like, Value::String(pattern.into()))
}
pub fn ilike(field: &str, pattern: impl Into<String>) -> Ast {
leaf(field, Operator::Ilike, Value::String(pattern.into()))
}