regex/
regex.rs

1use jsonfilter::{try_matches, FilterError};
2use serde_json::{json, Value};
3
4fn main() {
5    // You can filter text with regex
6    let filter = json!({"description": {"$regex": "(Rust|Python)"}});
7
8    let obj1 = json!({"name": "John Doe", "description": "Enthusiastic about programming in Rust"});
9    let obj2 = json!({"name": "Alice Smith", "description": "Experienced in web development"});
10    let obj3 = json!({"name": "Bob Brown", "description": "Loves to code in Python"});
11
12    println!("Filter:");
13    println!("{}", filter);
14    println!("Objects:");
15    println!("Object 1: {}", obj1);
16    println!("Object 2: {}", obj2);
17    println!("Object 3: {}", obj3);
18
19    match_objects(&filter, &obj1);
20    match_objects(&filter, &obj2);
21    match_objects(&filter, &obj3);
22}
23
24fn match_objects(filter: &Value, obj: &Value) {
25    match try_matches(filter, obj) {
26        Ok(result) => {
27            if result {
28                println!("Filter matches the object");
29            } else {
30                println!("Filter does not match the object");
31            }
32        }
33        Err(err) => match err {
34            FilterError::InvalidFilter => println!("Invalid filter"),
35            FilterError::UnknownOperator => println!("Unknown operator in filter"),
36            FilterError::KeyNotFound => println!("Key not found in object"),
37        },
38    }
39}