Skip to main content

rest_sql/
filter.rs

1use crate::ast::{Ast, Constraint, Operator, Value};
2
3fn leaf(field: &str, operator: Operator, value: Value) -> Ast {
4    Ast::Constraint(Constraint {
5        field: field.to_owned(),
6        operator,
7        value,
8    })
9}
10
11pub fn eq(field: &str, value: impl Into<Value>) -> Ast {
12    leaf(field, Operator::Eq, value.into())
13}
14
15pub fn neq(field: &str, value: impl Into<Value>) -> Ast {
16    leaf(field, Operator::Neq, value.into())
17}
18
19pub fn lt(field: &str, value: impl Into<Value>) -> Ast {
20    leaf(field, Operator::Lt, value.into())
21}
22
23pub fn lte(field: &str, value: impl Into<Value>) -> Ast {
24    leaf(field, Operator::Lte, value.into())
25}
26
27pub fn gt(field: &str, value: impl Into<Value>) -> Ast {
28    leaf(field, Operator::Gt, value.into())
29}
30
31pub fn gte(field: &str, value: impl Into<Value>) -> Ast {
32    leaf(field, Operator::Gte, value.into())
33}
34
35pub fn in_<V: Into<Value>>(field: &str, values: impl IntoIterator<Item = V>) -> Ast {
36    leaf(
37        field,
38        Operator::In,
39        Value::List(values.into_iter().map(Into::into).collect()),
40    )
41}
42
43pub fn out<V: Into<Value>>(field: &str, values: impl IntoIterator<Item = V>) -> Ast {
44    leaf(
45        field,
46        Operator::Out,
47        Value::List(values.into_iter().map(Into::into).collect()),
48    )
49}
50
51pub fn between(field: &str, lo: impl Into<Value>, hi: impl Into<Value>) -> Ast {
52    leaf(
53        field,
54        Operator::Between,
55        Value::List(vec![lo.into(), hi.into()]),
56    )
57}
58
59pub fn null(field: &str) -> Ast {
60    leaf(field, Operator::Null, Value::Bool(true))
61}
62
63pub fn not_null(field: &str) -> Ast {
64    leaf(field, Operator::NotNull, Value::Bool(true))
65}
66
67pub fn like(field: &str, pattern: impl Into<String>) -> Ast {
68    leaf(field, Operator::Like, Value::String(pattern.into()))
69}
70
71pub fn ilike(field: &str, pattern: impl Into<String>) -> Ast {
72    leaf(field, Operator::Ilike, Value::String(pattern.into()))
73}