tcvectordb_rust/
filter.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
4pub struct Filter {
5    condition: String,
6}
7
8impl Filter {
9    pub fn new(condition: impl Into<String>) -> Self {
10        Self {
11            condition: condition.into(),
12        }
13    }
14
15    pub fn and(mut self, condition: impl Into<String>) -> Self {
16        self.condition = format!("({}) and ({})", self.condition, condition.into());
17        self
18    }
19
20    pub fn or(mut self, condition: impl Into<String>) -> Self {
21        self.condition = format!("({}) or ({})", self.condition, condition.into());
22        self
23    }
24
25    pub fn and_not(mut self, condition: impl Into<String>) -> Self {
26        self.condition = format!("({}) and not ({})", self.condition, condition.into());
27        self
28    }
29
30    pub fn or_not(mut self, condition: impl Into<String>) -> Self {
31        self.condition = format!("({}) or not ({})", self.condition, condition.into());
32        self
33    }
34
35    pub fn include(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
36        let key = key.into();
37        let values: Vec<String> = values.into_iter().map(|v| {
38            let val = v.into();
39            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
40                val
41            } else {
42                format!("\"{}\"", val)
43            }
44        }).collect();
45        format!("{} include ({})", key, values.join(","))
46    }
47
48    pub fn exclude(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
49        let key = key.into();
50        let values: Vec<String> = values.into_iter().map(|v| {
51            let val = v.into();
52            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
53                val
54            } else {
55                format!("\"{}\"", val)
56            }
57        }).collect();
58        format!("{} exclude ({})", key, values.join(","))
59    }
60
61    pub fn include_all(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
62        let key = key.into();
63        let values: Vec<String> = values.into_iter().map(|v| {
64            let val = v.into();
65            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
66                val
67            } else {
68                format!("\"{}\"", val)
69            }
70        }).collect();
71        format!("{} include all ({})", key, values.join(","))
72    }
73
74    pub fn in_values(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
75        let key = key.into();
76        let values: Vec<String> = values.into_iter().map(|v| {
77            let val = v.into();
78            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
79                val
80            } else {
81                format!("\"{}\"", val)
82            }
83        }).collect();
84        format!("{} in ({})", key, values.join(","))
85    }
86
87    pub fn not_in(key: impl Into<String>, values: Vec<impl Into<String>>) -> String {
88        let key = key.into();
89        let values: Vec<String> = values.into_iter().map(|v| {
90            let val = v.into();
91            if val.parse::<f64>().is_ok() || val.parse::<i64>().is_ok() {
92                val
93            } else {
94                format!("\"{}\"", val)
95            }
96        }).collect();
97        format!("{} not in ({})", key, values.join(","))
98    }
99
100    pub fn condition(&self) -> &str {
101        &self.condition
102    }
103}
104
105impl From<&str> for Filter {
106    fn from(condition: &str) -> Self {
107        Self::new(condition)
108    }
109}
110
111impl From<String> for Filter {
112    fn from(condition: String) -> Self {
113        Self::new(condition)
114    }
115}