baserow_rs/filter.rs
1/// Filter operations available for querying Baserow tables
2///
3/// This enum provides all the possible filter operations that can be used
4/// when querying table rows. The filters are grouped by type (text, date, number, etc.)
5/// and provide rich comparison capabilities.
6///
7/// # Example
8/// ```no_run
9/// use baserow_rs::{ConfigBuilder, Baserow, BaserowTableOperations, filter::Filter, api::client::BaserowClient};
10/// use std::collections::HashMap;
11/// use serde_json::Value;
12///
13/// #[tokio::main]
14/// async fn main() {
15/// let config = ConfigBuilder::new()
16/// .base_url("https://api.baserow.io")
17/// .api_key("your-api-key")
18/// .build();
19///
20/// let baserow = Baserow::with_configuration(config);
21/// let table = baserow.table_by_id(1234);
22///
23/// // Query with multiple filters
24/// let results = table.query()
25/// .filter_by("Status", Filter::Equal, "Active")
26/// .filter_by("Age", Filter::HigherThan, "18")
27/// .filter_by("Name", Filter::Contains, "John")
28/// .get::<HashMap<String, Value>>()
29/// .await
30/// .unwrap();
31/// }
32/// ```
33#[derive(Debug, Clone)]
34pub enum Filter {
35 /// Exact match comparison
36 /// Field value must exactly match the provided value
37 /// Example: `.filter_by("Status", Filter::Equal, "Active")`
38 Equal,
39 /// Inverse of Equal
40 /// Field value must not match the provided value
41 NotEqual,
42
43 // Date Filters
44 /// Date field matches exact date
45 DateIs,
46 /// Date field does not match exact date
47 DateIsNot,
48 /// Date field is before specified date
49 DateIsBefore,
50 /// Date field is on or before specified date
51 DateIsOnOrBefore,
52 /// Date field is after specified date
53 DateIsAfter,
54 /// Date field is on or after specified date
55 DateIsOnOrAfter,
56 /// Date field is within specified range
57 DateIsWithin,
58 /// Legacy exact date match
59 DateEqual,
60 /// Legacy date not equal
61 DateNotEqual,
62 /// Date field is today
63 DateEqualsToday,
64 /// Date field is before today
65 DateBeforeToday,
66 /// Date field is after today
67 DateAfterToday,
68 /// Date is within specified number of days
69 DateWithinDays,
70 /// Date is within specified number of weeks
71 DateWithinWeeks,
72 /// Date is within specified number of months
73 DateWithinMonths,
74 /// Date is exactly specified number of days ago
75 DateEqualsDaysAgo,
76 /// Date is exactly specified number of months ago
77 DateEqualsMonthsAgo,
78 /// Date is exactly specified number of years ago
79 DateEqualsYearsAgo,
80 /// Date is in specified week
81 DateEqualsWeek,
82 /// Date is in specified month
83 DateEqualsMonth,
84 /// Date is in specified year
85 DateEqualsYear,
86 /// Date matches specific day of month
87 DateEqualsDayOfMonth,
88 /// Legacy before date
89 DateBefore,
90 /// Legacy before or equal date
91 DateBeforeOrEqual,
92 /// Legacy after date
93 DateAfter,
94 /// Legacy after or equal date
95 DateAfterOrEqual,
96 /// Date is after specified number of days ago
97 DateAfterDaysAgo,
98
99 // Value Presence Filters
100 /// Field has empty value
101 HasEmptyValue,
102 /// Field has non-empty value
103 HasNotEmptyValue,
104 /// Field has specific value
105 HasValueEqual,
106 /// Field does not have specific value
107 HasNotValueEqual,
108 /// Field value contains substring
109 HasValueContains,
110 /// Field value does not contain substring
111 HasNotValueContains,
112 /// Field value contains specific word
113 HasValueContainsWord,
114 /// Field value does not contain specific word
115 HasNotValueContainsWord,
116 /// Field value length is less than specified
117 HasValueLengthIsLowerThan,
118 /// All values in field equal specified value
119 HasAllValuesEqual,
120 /// Any select option matches value
121 HasAnySelectOptionEqual,
122 /// No select option matches value
123 HasNoneSelectOptionEqual,
124
125 // Text Filters
126 /// Text contains substring
127 /// Example: `.filter_by("Name", Filter::Contains, "John")`
128 Contains,
129 /// Text does not contain substring
130 ContainsNot,
131 /// Text contains whole word
132 ContainsWord,
133 /// Text does not contain whole word
134 DoesntContainWord,
135
136 // File Filters
137 /// Filename contains substring
138 FilenameContains,
139 /// File is of specified type
140 HasFileType,
141 /// Number of files is less than specified
142 FilesLowerThan,
143 /// Text length is less than specified
144 LengthIsLowerThan,
145
146 // Numeric Filters
147 /// Number is greater than value
148 /// Example: `.filter_by("Age", Filter::HigherThan, "18")`
149 HigherThan,
150 /// Number is greater than or equal to value
151 HigherThanOrEqual,
152 /// Number is less than value
153 LowerThan,
154 /// Number is less than or equal to value
155 LowerThanOrEqual,
156 /// Number is even and whole
157 IsEvenAndWhole,
158
159 // Select Filters
160 /// Single select matches value
161 SingleSelectEqual,
162 /// Single select does not match value
163 SingleSelectNotEqual,
164 /// Single select matches any of values
165 SingleSelectIsAnyOf,
166 /// Single select matches none of values
167 SingleSelectIsNoneOf,
168 /// Boolean field matches value
169 Boolean,
170
171 // Link Row Filters
172 /// Linked row exists
173 LinkRowHas,
174 /// Linked row does not exist
175 LinkRowHasNot,
176 /// Linked row contains value
177 LinkRowContains,
178 /// Linked row does not contain value
179 LinkRowNotContains,
180
181 // Multiple Select Filters
182 /// Multiple select includes value
183 MultipleSelectHas,
184 /// Multiple select does not include value
185 MultipleSelectHasNot,
186 /// Multiple collaborators includes user
187 MultipleCollaboratorsHas,
188 /// Multiple collaborators does not include user
189 MultipleCollaboratorsHasNot,
190
191 // Null Filters
192 /// Field is empty/null
193 Empty,
194 /// Field is not empty/null
195 NotEmpty,
196
197 // User Filters
198 /// Field matches specific user
199 UserIs,
200 /// Field does not match specific user
201 UserIsNot,
202}
203
204impl Filter {
205 /// Converts the filter to its string representation for API requests
206 pub fn as_str(&self) -> &'static str {
207 match self {
208 Filter::Equal => "equal",
209 Filter::NotEqual => "not_equal",
210 Filter::DateIs => "date_is",
211 Filter::DateIsNot => "date_is_not",
212 Filter::DateIsBefore => "date_is_before",
213 Filter::DateIsOnOrBefore => "date_is_on_or_before",
214 Filter::DateIsAfter => "date_is_after",
215 Filter::DateIsOnOrAfter => "date_is_on_or_after",
216 Filter::DateIsWithin => "date_is_within",
217 Filter::DateEqual => "date_equal",
218 Filter::DateNotEqual => "date_not_equal",
219 Filter::DateEqualsToday => "date_equals_today",
220 Filter::DateBeforeToday => "date_before_today",
221 Filter::DateAfterToday => "date_after_today",
222 Filter::DateWithinDays => "date_within_days",
223 Filter::DateWithinWeeks => "date_within_weeks",
224 Filter::DateWithinMonths => "date_within_months",
225 Filter::DateEqualsDaysAgo => "date_equals_days_ago",
226 Filter::DateEqualsMonthsAgo => "date_equals_months_ago",
227 Filter::DateEqualsYearsAgo => "date_equals_years_ago",
228 Filter::DateEqualsWeek => "date_equals_week",
229 Filter::DateEqualsMonth => "date_equals_month",
230 Filter::DateEqualsYear => "date_equals_year",
231 Filter::DateEqualsDayOfMonth => "date_equals_day_of_month",
232 Filter::DateBefore => "date_before",
233 Filter::DateBeforeOrEqual => "date_before_or_equal",
234 Filter::DateAfter => "date_after",
235 Filter::DateAfterOrEqual => "date_after_or_equal",
236 Filter::DateAfterDaysAgo => "date_after_days_ago",
237 Filter::HasEmptyValue => "has_empty_value",
238 Filter::HasNotEmptyValue => "has_not_empty_value",
239 Filter::HasValueEqual => "has_value_equal",
240 Filter::HasNotValueEqual => "has_not_value_equal",
241 Filter::HasValueContains => "has_value_contains",
242 Filter::HasNotValueContains => "has_not_value_contains",
243 Filter::HasValueContainsWord => "has_value_contains_word",
244 Filter::HasNotValueContainsWord => "has_not_value_contains_word",
245 Filter::HasValueLengthIsLowerThan => "has_value_length_is_lower_than",
246 Filter::HasAllValuesEqual => "has_all_values_equal",
247 Filter::HasAnySelectOptionEqual => "has_any_select_option_equal",
248 Filter::HasNoneSelectOptionEqual => "has_none_select_option_equal",
249 Filter::Contains => "contains",
250 Filter::ContainsNot => "contains_not",
251 Filter::ContainsWord => "contains_word",
252 Filter::DoesntContainWord => "doesnt_contain_word",
253 Filter::FilenameContains => "filename_contains",
254 Filter::HasFileType => "has_file_type",
255 Filter::FilesLowerThan => "files_lower_than",
256 Filter::LengthIsLowerThan => "length_is_lower_than",
257 Filter::HigherThan => "higher_than",
258 Filter::HigherThanOrEqual => "higher_than_or_equal",
259 Filter::LowerThan => "lower_than",
260 Filter::LowerThanOrEqual => "lower_than_or_equal",
261 Filter::IsEvenAndWhole => "is_even_and_whole",
262 Filter::SingleSelectEqual => "single_select_equal",
263 Filter::SingleSelectNotEqual => "single_select_not_equal",
264 Filter::SingleSelectIsAnyOf => "single_select_is_any_of",
265 Filter::SingleSelectIsNoneOf => "single_select_is_none_of",
266 Filter::Boolean => "boolean",
267 Filter::LinkRowHas => "link_row_has",
268 Filter::LinkRowHasNot => "link_row_has_not",
269 Filter::LinkRowContains => "link_row_contains",
270 Filter::LinkRowNotContains => "link_row_not_contains",
271 Filter::MultipleSelectHas => "multiple_select_has",
272 Filter::MultipleSelectHasNot => "multiple_select_has_not",
273 Filter::MultipleCollaboratorsHas => "multiple_collaborators_has",
274 Filter::MultipleCollaboratorsHasNot => "multiple_collaborators_has_not",
275 Filter::Empty => "empty",
276 Filter::NotEmpty => "not_empty",
277 Filter::UserIs => "user_is",
278 Filter::UserIsNot => "user_is_not",
279 }
280 }
281}
282
283/// Internal structure for representing a filter condition
284///
285/// Combines a field name, filter operation, and value into a single filter condition
286/// that can be applied to a table query.
287#[derive(Clone, Debug)]
288pub struct FilterTriple {
289 /// The name of the field to filter on
290 pub field: String,
291 /// The filter operation to apply
292 pub filter: Filter,
293 /// The value to compare against
294 pub value: String,
295}