Skip to content

Commit 2a66d98

Browse files
committed
address comments and implement before and after clauses
1 parent 6608d04 commit 2a66d98

File tree

2 files changed

+1022
-197
lines changed

2 files changed

+1022
-197
lines changed

packages/rs-drive/src/query/conditions.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,52 @@ impl<'a> WhereClause {
217217
self.field == "$id"
218218
}
219219

220+
/// Evaluate this clause against a provided `Value`
221+
pub fn matches_value(&self, value: &Value) -> bool {
222+
match &self.operator {
223+
WhereOperator::Equal => value == &self.value,
224+
WhereOperator::GreaterThan => value > &self.value,
225+
WhereOperator::GreaterThanOrEquals => value >= &self.value,
226+
WhereOperator::LessThan => value < &self.value,
227+
WhereOperator::LessThanOrEquals => value <= &self.value,
228+
WhereOperator::In => match &self.value {
229+
Value::Array(array) => array.contains(value),
230+
_ => false,
231+
},
232+
WhereOperator::Between => match &self.value {
233+
Value::Array(bounds) if bounds.len() == 2 => {
234+
value >= &bounds[0] && value <= &bounds[1]
235+
}
236+
_ => false,
237+
},
238+
WhereOperator::BetweenExcludeBounds => match &self.value {
239+
Value::Array(bounds) if bounds.len() == 2 => {
240+
value > &bounds[0] && value < &bounds[1]
241+
}
242+
_ => false,
243+
},
244+
WhereOperator::BetweenExcludeLeft => match &self.value {
245+
Value::Array(bounds) if bounds.len() == 2 => {
246+
value > &bounds[0] && value <= &bounds[1]
247+
}
248+
_ => false,
249+
},
250+
WhereOperator::BetweenExcludeRight => match &self.value {
251+
Value::Array(bounds) if bounds.len() == 2 => {
252+
value >= &bounds[0] && value < &bounds[1]
253+
}
254+
_ => false,
255+
},
256+
WhereOperator::StartsWith => {
257+
if let (Value::Text(text), Value::Text(prefix)) = (value, &self.value) {
258+
text.starts_with(prefix.as_str())
259+
} else {
260+
false
261+
}
262+
}
263+
}
264+
}
265+
220266
/// Returns the where clause `in` values if they are an array of values, else an error
221267
pub fn in_values(&self) -> Result<Cow<Vec<Value>>, Error> {
222268
let in_values = match &self.value {

0 commit comments

Comments
 (0)