reinhardt_urls/proxy/
query.rs1use crate::proxy::ScalarValue;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
8pub enum FilterOp {
9 Eq,
11 Ne,
13 Lt,
15 Le,
17 Gt,
19 Ge,
21 In,
23 NotIn,
25 Contains,
27 StartsWith,
29 EndsWith,
31}
32
33#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct FilterCondition {
36 pub field: String,
38 pub op: FilterOp,
40 pub value: String,
42}
43
44impl FilterCondition {
45 pub fn new(field: String, op: FilterOp, value: String) -> Self {
47 Self { field, op, value }
48 }
49
50 pub fn matches(&self, scalar: &ScalarValue) -> bool {
52 let scalar_str = match scalar {
53 ScalarValue::String(s) => s.clone(),
54 ScalarValue::Integer(i) => i.to_string(),
55 ScalarValue::Float(f) => f.to_string(),
56 ScalarValue::Boolean(b) => b.to_string(),
57 ScalarValue::Null => "null".to_string(),
58 };
59
60 match self.op {
61 FilterOp::Eq => scalar_str == self.value,
62 FilterOp::Ne => scalar_str != self.value,
63 FilterOp::Lt => scalar_str < self.value,
64 FilterOp::Le => scalar_str <= self.value,
65 FilterOp::Gt => scalar_str > self.value,
66 FilterOp::Ge => scalar_str >= self.value,
67 FilterOp::In => self.value.contains(&scalar_str),
68 FilterOp::NotIn => !self.value.contains(&scalar_str),
69 FilterOp::Contains => scalar_str.contains(&self.value),
70 FilterOp::StartsWith => scalar_str.starts_with(&self.value),
71 FilterOp::EndsWith => scalar_str.ends_with(&self.value),
72 }
73 }
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct QueryFilter {
79 pub conditions: Vec<FilterCondition>,
81}
82
83impl QueryFilter {
84 pub fn new() -> Self {
86 Self {
87 conditions: Vec::new(),
88 }
89 }
90
91 pub fn add_condition(&mut self, condition: FilterCondition) {
93 self.conditions.push(condition);
94 }
95}
96
97impl Default for QueryFilter {
98 fn default() -> Self {
99 Self::new()
100 }
101}