mik-sql 0.1.2

SQL query builder with Mongo-style filters - sql_read!, sql_create!, sql_update!, sql_delete!
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! Runtime JSON parsing for Mongo-style filters.
//!
//! Parse user-provided JSON into `FilterExpr` at runtime, using the same
//! Mongo-style syntax as the compile-time macros.
//!
//! # Quick Start
//!
//! ```
//! use mik_sql::prelude::*;
//!
//! // Parse from JSON string
//! let filter = parse_filter(r#"{"name": "Alice", "age": {"$gte": 18}}"#).unwrap();
//! ```
//!
//! # Usage with mik-sdk Request
//!
//! ```ignore
//! use mik_sdk::prelude::*;
//! use mik_sql::{parse_filter, sql_read};
//!
//! fn search(query: Pagination, req: &Request) -> Response {
//!     // Extract body as text, early return if missing
//!     let body = ensure!(req.text(), 400, "Filter body required");
//!
//!     // Parse as filter, early return if invalid
//!     let filter = ensure!(parse_filter(body), 400, "Invalid filter");
//!
//!     // Merge with trusted filter, validate against whitelist
//!     let (sql, params) = ensure!(sql_read!(users {
//!         select: [id, name, email],
//!         filter: { active: true },
//!         merge: filter,
//!         allow: [name, email, status],
//!         page: query.page,
//!         limit: query.limit,
//!     }), 400, "Invalid filter field");
//!
//!     // Execute query...
//!     ok!({ "sql": sql })
//! }
//! ```
//!
//! # Supported Syntax
//!
//! | Syntax | Example | SQL |
//! |--------|---------|-----|
//! | Implicit `$eq` | `{"name": "Alice"}` | `name = 'Alice'` |
//! | Explicit operator | `{"age": {"$gte": 18}}` | `age >= 18` |
//! | Multiple fields | `{"a": 1, "b": 2}` | `a = 1 AND b = 2` |
//! | `$and` | `{"$and": [{...}, {...}]}` | `(...) AND (...)` |
//! | `$or` | `{"$or": [{...}, {...}]}` | `(...) OR (...)` |
//! | `$not` | `{"$not": {...}}` | `NOT (...)` |
//! | `$in` | `{"status": {"$in": ["a", "b"]}}` | `status IN ('a', 'b')` |
//! | `$between` | `{"age": {"$between": [18, 65]}}` | `age BETWEEN 18 AND 65` |

use super::types::{CompoundFilter, Filter, FilterExpr, Operator, Value};
use miniserde::json::{Number, Value as JsonValue};
use std::fmt;

/// Error type for JSON filter parsing.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
    /// Invalid JSON syntax or encoding.
    InvalidJson,
    /// Unknown operator (e.g., `$foo`).
    UnknownOperator(String),
    /// Expected an object but got something else.
    ExpectedObject,
    /// Expected an array but got something else.
    ExpectedArray,
    /// Expected a value but got something else.
    ExpectedValue,
    /// Field name is empty.
    EmptyFieldName,
    /// Filter object is empty.
    EmptyFilter,
    /// Invalid operator value type.
    InvalidOperatorValue {
        /// The operator that had the wrong value type.
        op: String,
        /// Description of what was expected.
        expected: &'static str,
    },
    /// $not requires exactly one condition.
    NotRequiresOneCondition,
}

/// Parse a Mongo-style filter from a JSON string.
///
/// This is a convenience function that calls [`FilterExpr::parse`].
///
/// # Example
///
/// ```
/// use mik_sql::prelude::*;
///
/// // Simple filter
/// let filter = parse_filter(r#"{"active": true}"#).unwrap();
///
/// // Complex filter with operators
/// let filter = parse_filter(r#"{
///     "status": {"$in": ["active", "pending"]},
///     "age": {"$gte": 18}
/// }"#).unwrap();
///
/// // Logical operators
/// let filter = parse_filter(r#"{
///     "$or": [
///         {"role": "admin"},
///         {"role": "moderator"}
///     ]
/// }"#).unwrap();
/// ```
///
/// # Errors
///
/// Returns `ParseError` if the JSON is invalid.
pub fn parse_filter(json_str: &str) -> Result<FilterExpr, ParseError> {
    FilterExpr::parse(json_str)
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidJson => write!(f, "Invalid JSON syntax or encoding"),
            Self::UnknownOperator(op) => write!(f, "Unknown operator '{op}'"),
            Self::ExpectedObject => write!(f, "Expected JSON object"),
            Self::ExpectedArray => write!(f, "Expected JSON array"),
            Self::ExpectedValue => write!(f, "Expected a value"),
            Self::EmptyFieldName => write!(f, "Field name cannot be empty"),
            Self::EmptyFilter => write!(f, "Filter object cannot be empty"),
            Self::InvalidOperatorValue { op, expected } => {
                write!(f, "Operator '{op}' expects {expected}")
            },
            Self::NotRequiresOneCondition => {
                write!(f, "$not requires exactly one condition")
            },
        }
    }
}

impl std::error::Error for ParseError {}

impl Operator {
    /// Parse from Mongo-style operator string (e.g., "$eq", "$gte").
    ///
    /// Accepts both with and without the `$` prefix.
    ///
    /// # Example
    ///
    /// ```
    /// use mik_sql::Operator;
    ///
    /// assert_eq!(Operator::from_mongo("$eq"), Some(Operator::Eq));
    /// assert_eq!(Operator::from_mongo("gte"), Some(Operator::Gte));
    /// assert_eq!(Operator::from_mongo("$unknown"), None);
    /// ```
    #[must_use]
    pub fn from_mongo(s: &str) -> Option<Self> {
        // Strip leading $ if present
        let s = s.strip_prefix('$').unwrap_or(s);

        match s {
            "eq" => Some(Self::Eq),
            "ne" => Some(Self::Ne),
            "gt" => Some(Self::Gt),
            "gte" => Some(Self::Gte),
            "lt" => Some(Self::Lt),
            "lte" => Some(Self::Lte),
            "in" => Some(Self::In),
            "nin" => Some(Self::NotIn),
            "like" => Some(Self::Like),
            "ilike" => Some(Self::ILike),
            "regex" => Some(Self::Regex),
            "startsWith" | "starts_with" => Some(Self::StartsWith),
            "endsWith" | "ends_with" => Some(Self::EndsWith),
            "contains" => Some(Self::Contains),
            "between" => Some(Self::Between),
            _ => None,
        }
    }
}

impl Value {
    /// Convert from miniserde JSON value.
    ///
    /// # Example
    ///
    /// ```
    /// use mik_sql::Value;
    /// use miniserde::json::{Value as JsonValue, Number};
    ///
    /// let json = JsonValue::String("hello".to_string());
    /// assert_eq!(Value::from_json(&json), Some(Value::String("hello".to_string())));
    ///
    /// let json = JsonValue::Number(Number::I64(42));
    /// assert_eq!(Value::from_json(&json), Some(Value::Int(42)));
    /// ```
    #[must_use]
    pub fn from_json(json: &JsonValue) -> Option<Self> {
        match json {
            JsonValue::Null => Some(Self::Null),
            JsonValue::Bool(b) => Some(Self::Bool(*b)),
            JsonValue::Number(n) => match n {
                Number::I64(i) => Some(Self::Int(*i)),
                Number::U64(u) => i64::try_from(*u).ok().map(Self::Int),
                Number::F64(f) => Some(Self::Float(*f)),
            },
            JsonValue::String(s) => Some(Self::String(s.clone())),
            JsonValue::Array(arr) => {
                let values: Option<Vec<Self>> = arr.iter().map(Self::from_json).collect();
                values.map(Self::Array)
            },
            JsonValue::Object(_) => None, // Objects are not valid filter values
        }
    }
}

impl FilterExpr {
    /// Parse a Mongo-style filter from a JSON string.
    ///
    /// This is the recommended way to parse user-provided filters.
    ///
    /// # Example
    ///
    /// ```
    /// use mik_sql::FilterExpr;
    ///
    /// let filter = FilterExpr::parse(r#"{"name": "Alice", "active": true}"#).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the JSON is invalid or malformed.
    pub fn parse(json_str: &str) -> Result<Self, ParseError> {
        let json: JsonValue =
            miniserde::json::from_str(json_str).map_err(|_| ParseError::InvalidJson)?;
        Self::from_json(&json)
    }

    /// Parse a Mongo-style filter from JSON bytes.
    ///
    /// Useful when working with raw request bodies.
    ///
    /// # Example
    ///
    /// ```
    /// use mik_sql::FilterExpr;
    ///
    /// let bytes = br#"{"status": {"$in": ["active", "pending"]}}"#;
    /// let filter = FilterExpr::parse_bytes(bytes).unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the bytes are not valid UTF-8 or valid JSON.
    pub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError> {
        let s = std::str::from_utf8(bytes).map_err(|_| ParseError::InvalidJson)?;
        Self::parse(s)
    }

    /// Parse a Mongo-style filter from a parsed JSON value.
    ///
    /// Use this when you already have a parsed `miniserde::json::Value`.
    /// For most cases, prefer [`parse`](Self::parse) or [`parse_bytes`](Self::parse_bytes).
    ///
    /// # Errors
    ///
    /// Returns `ParseError` if the JSON structure is invalid.
    pub fn from_json(json: &JsonValue) -> Result<Self, ParseError> {
        let obj = match json {
            JsonValue::Object(o) => o,
            _ => return Err(ParseError::ExpectedObject),
        };

        if obj.is_empty() {
            return Err(ParseError::EmptyFilter);
        }

        let mut filters = Vec::new();

        for (key, value) in obj {
            if key.is_empty() {
                return Err(ParseError::EmptyFieldName);
            }

            // Check for logical operators
            if key.starts_with('$') {
                match key.as_str() {
                    "$and" => {
                        let exprs = parse_filter_array(value)?;
                        filters.push(Self::Compound(CompoundFilter::and(exprs)));
                    },
                    "$or" => {
                        let exprs = parse_filter_array(value)?;
                        filters.push(Self::Compound(CompoundFilter::or(exprs)));
                    },
                    "$not" => {
                        let inner = Self::from_json(value)?;
                        filters.push(Self::Compound(CompoundFilter::not(inner)));
                    },
                    _ => return Err(ParseError::UnknownOperator(key.clone())),
                }
            } else {
                // Field filter
                let filter = parse_field_filter(key, value)?;
                filters.push(filter);
            }
        }

        // Combine multiple filters with implicit AND
        Ok(match filters.len() {
            0 => return Err(ParseError::EmptyFilter),
            1 => filters.remove(0),
            _ => Self::Compound(CompoundFilter::and(filters)),
        })
    }
}

/// Parse an array of filter expressions (for $and/$or).
fn parse_filter_array(json: &JsonValue) -> Result<Vec<FilterExpr>, ParseError> {
    let arr = match json {
        JsonValue::Array(a) => a,
        _ => return Err(ParseError::ExpectedArray),
    };

    arr.iter().map(FilterExpr::from_json).collect()
}

/// Parse a field filter: `{"$op": value}` or just `value` (implicit $eq).
fn parse_field_filter(field: &str, value: &JsonValue) -> Result<FilterExpr, ParseError> {
    // Check for operator syntax: {"$eq": value}
    if let JsonValue::Object(obj) = value {
        if let Some((op_key, op_value)) = obj.iter().next()
            && op_key.starts_with('$')
        {
            let op = Operator::from_mongo(op_key)
                .ok_or_else(|| ParseError::UnknownOperator(op_key.clone()))?;

            let val = parse_operator_value(op, op_value)?;

            return Ok(FilterExpr::Simple(Filter {
                field: field.to_string(),
                op,
                value: val,
            }));
        }
        // Not an operator object, treat as error
        return Err(ParseError::ExpectedValue);
    }

    // Implicit $eq
    let val = Value::from_json(value).ok_or(ParseError::ExpectedValue)?;
    Ok(FilterExpr::Simple(Filter {
        field: field.to_string(),
        op: Operator::Eq,
        value: val,
    }))
}

/// Parse the value for an operator, with type validation.
fn parse_operator_value(op: Operator, value: &JsonValue) -> Result<Value, ParseError> {
    match op {
        // Array operators require arrays
        Operator::In | Operator::NotIn => match value {
            JsonValue::Array(arr) => {
                let values: Option<Vec<Value>> = arr.iter().map(Value::from_json).collect();
                values
                    .map(Value::Array)
                    .ok_or_else(|| ParseError::InvalidOperatorValue {
                        op: format!("${op:?}").to_lowercase(),
                        expected: "array of values",
                    })
            },
            _ => Err(ParseError::InvalidOperatorValue {
                op: "$in/$nin".to_string(),
                expected: "array",
            }),
        },

        // Between requires array of exactly 2 values
        Operator::Between => match value {
            JsonValue::Array(arr) if arr.len() == 2 => {
                let values: Option<Vec<Value>> = arr.iter().map(Value::from_json).collect();
                values
                    .map(Value::Array)
                    .ok_or_else(|| ParseError::InvalidOperatorValue {
                        op: "$between".to_string(),
                        expected: "array of 2 values",
                    })
            },
            JsonValue::Array(_) => Err(ParseError::InvalidOperatorValue {
                op: "$between".to_string(),
                expected: "array of exactly 2 values",
            }),
            _ => Err(ParseError::InvalidOperatorValue {
                op: "$between".to_string(),
                expected: "array of 2 values",
            }),
        },

        // All other operators accept scalar values
        _ => Value::from_json(value).ok_or(ParseError::ExpectedValue),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::LogicalOp;
    use miniserde::json::{self, Array as JsonArray};

    // =========================================================================
    // Operator::from_mongo tests
    // =========================================================================

    #[test]
    fn test_operator_from_mongo_with_prefix() {
        assert_eq!(Operator::from_mongo("$eq"), Some(Operator::Eq));
        assert_eq!(Operator::from_mongo("$ne"), Some(Operator::Ne));
        assert_eq!(Operator::from_mongo("$gt"), Some(Operator::Gt));
        assert_eq!(Operator::from_mongo("$gte"), Some(Operator::Gte));
        assert_eq!(Operator::from_mongo("$lt"), Some(Operator::Lt));
        assert_eq!(Operator::from_mongo("$lte"), Some(Operator::Lte));
        assert_eq!(Operator::from_mongo("$in"), Some(Operator::In));
        assert_eq!(Operator::from_mongo("$nin"), Some(Operator::NotIn));
        assert_eq!(Operator::from_mongo("$like"), Some(Operator::Like));
        assert_eq!(Operator::from_mongo("$ilike"), Some(Operator::ILike));
        assert_eq!(Operator::from_mongo("$regex"), Some(Operator::Regex));
        assert_eq!(Operator::from_mongo("$between"), Some(Operator::Between));
    }

    #[test]
    fn test_operator_from_mongo_without_prefix() {
        assert_eq!(Operator::from_mongo("eq"), Some(Operator::Eq));
        assert_eq!(Operator::from_mongo("gte"), Some(Operator::Gte));
    }

    #[test]
    fn test_operator_from_mongo_camel_case() {
        assert_eq!(
            Operator::from_mongo("$startsWith"),
            Some(Operator::StartsWith)
        );
        assert_eq!(
            Operator::from_mongo("$starts_with"),
            Some(Operator::StartsWith)
        );
        assert_eq!(Operator::from_mongo("$endsWith"), Some(Operator::EndsWith));
        assert_eq!(Operator::from_mongo("$ends_with"), Some(Operator::EndsWith));
    }

    #[test]
    fn test_operator_from_mongo_unknown() {
        assert_eq!(Operator::from_mongo("$unknown"), None);
        assert_eq!(Operator::from_mongo("$foo"), None);
    }

    // =========================================================================
    // Value::from_json tests
    // =========================================================================

    #[test]
    fn test_value_from_json_primitives() {
        assert_eq!(Value::from_json(&JsonValue::Null), Some(Value::Null));
        assert_eq!(
            Value::from_json(&JsonValue::Bool(true)),
            Some(Value::Bool(true))
        );
        assert_eq!(
            Value::from_json(&JsonValue::Number(Number::I64(42))),
            Some(Value::Int(42))
        );
        assert_eq!(
            Value::from_json(&JsonValue::Number(Number::F64(2.5))),
            Some(Value::Float(2.5))
        );
        assert_eq!(
            Value::from_json(&JsonValue::String("hello".into())),
            Some(Value::String("hello".into()))
        );
    }

    #[test]
    fn test_value_from_json_array() {
        let mut arr = JsonArray::new();
        arr.push(JsonValue::String("a".into()));
        arr.push(JsonValue::String("b".into()));
        let json_arr = JsonValue::Array(arr);
        assert_eq!(
            Value::from_json(&json_arr),
            Some(Value::Array(vec![
                Value::String("a".into()),
                Value::String("b".into()),
            ]))
        );
    }

    // =========================================================================
    // FilterExpr::from_json tests
    // =========================================================================

    #[test]
    fn test_simple_equality() {
        let json: JsonValue = json::from_str(r#"{"name": "Alice"}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Simple(Filter {
                ref field,
                op: Operator::Eq,
                value: Value::String(ref s),
            }) if field == "name" && s == "Alice"
        ));
    }

    #[test]
    fn test_explicit_operator() {
        let json: JsonValue = json::from_str(r#"{"age": {"$gte": 18}}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Simple(Filter {
                ref field,
                op: Operator::Gte,
                value: Value::Int(18),
            }) if field == "age"
        ));
    }

    #[test]
    fn test_multiple_fields_implicit_and() {
        let json: JsonValue = json::from_str(r#"{"name": "Alice", "age": 30}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Compound(CompoundFilter {
                op: LogicalOp::And,
                ..
            })
        ));
    }

    #[test]
    fn test_explicit_and() {
        let json: JsonValue =
            json::from_str(r#"{"$and": [{"name": "Alice"}, {"age": 30}]}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Compound(CompoundFilter {
                op: LogicalOp::And,
                ..
            })
        ));
    }

    #[test]
    fn test_explicit_or() {
        let json: JsonValue =
            json::from_str(r#"{"$or": [{"status": "active"}, {"status": "pending"}]}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Compound(CompoundFilter {
                op: LogicalOp::Or,
                ..
            })
        ));
    }

    #[test]
    fn test_explicit_not() {
        let json: JsonValue = json::from_str(r#"{"$not": {"deleted": true}}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Compound(CompoundFilter {
                op: LogicalOp::Not,
                ..
            })
        ));
    }

    #[test]
    fn test_in_operator() {
        let json: JsonValue = json::from_str(r#"{"status": {"$in": ["a", "b", "c"]}}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Simple(Filter {
                op: Operator::In,
                value: Value::Array(_),
                ..
            })
        ));
    }

    #[test]
    fn test_between_operator() {
        let json: JsonValue = json::from_str(r#"{"age": {"$between": [18, 65]}}"#).unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Simple(Filter {
                op: Operator::Between,
                value: Value::Array(ref arr),
                ..
            }) if arr.len() == 2
        ));
    }

    #[test]
    fn test_nested_logical() {
        let json: JsonValue = json::from_str(
            r#"{"$and": [{"active": true}, {"$or": [{"role": "admin"}, {"role": "mod"}]}]}"#,
        )
        .unwrap();
        let filter = FilterExpr::from_json(&json).unwrap();

        assert!(matches!(
            filter,
            FilterExpr::Compound(CompoundFilter {
                op: LogicalOp::And,
                ..
            })
        ));
    }

    // =========================================================================
    // Error cases
    // =========================================================================

    #[test]
    fn test_error_not_object() {
        let json: JsonValue = json::from_str(r"[1, 2, 3]").unwrap();
        assert!(matches!(
            FilterExpr::from_json(&json),
            Err(ParseError::ExpectedObject)
        ));
    }

    #[test]
    fn test_error_empty_filter() {
        let json: JsonValue = json::from_str(r"{}").unwrap();
        assert!(matches!(
            FilterExpr::from_json(&json),
            Err(ParseError::EmptyFilter)
        ));
    }

    #[test]
    fn test_error_unknown_operator() {
        let json: JsonValue = json::from_str(r#"{"field": {"$foo": 1}}"#).unwrap();
        assert!(matches!(
            FilterExpr::from_json(&json),
            Err(ParseError::UnknownOperator(_))
        ));
    }

    #[test]
    fn test_error_between_wrong_count() {
        let json: JsonValue = json::from_str(r#"{"age": {"$between": [18]}}"#).unwrap();
        assert!(matches!(
            FilterExpr::from_json(&json),
            Err(ParseError::InvalidOperatorValue { .. })
        ));
    }

    #[test]
    fn test_error_in_not_array() {
        let json: JsonValue = json::from_str(r#"{"status": {"$in": "not-array"}}"#).unwrap();
        assert!(matches!(
            FilterExpr::from_json(&json),
            Err(ParseError::InvalidOperatorValue { .. })
        ));
    }
}