refactor; add order() fn; add examples
This commit is contained in:
parent
d1419a2198
commit
6db5f79743
11 changed files with 388 additions and 11 deletions
38
examples/array.rs
Normal file
38
examples/array.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use jsonfilter::{try_matches, FilterError};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
fn main() {
|
||||
let filter = json!({"$or": [{"tags": {"$in": "programming"}},{"tags": {"$in": "rust"}}]});
|
||||
|
||||
let obj1 = json!({"name": "John Doe", "tags": ["rust", "programming", "development"]});
|
||||
let obj2 = json!({"name": "Alice Smith", "tags": ["web", "development", "javascript"]});
|
||||
let obj3 = json!({"name": "Bob Brown", "tags": ["python", "programming", "machine learning"]});
|
||||
|
||||
println!("Filter:");
|
||||
println!("{}", filter);
|
||||
println!("Objects:");
|
||||
println!("Object 1: {}", obj1);
|
||||
println!("Object 2: {}", obj2);
|
||||
println!("Object 3: {}", obj3);
|
||||
|
||||
match_objects(&filter, &obj1);
|
||||
match_objects(&filter, &obj2);
|
||||
match_objects(&filter, &obj3);
|
||||
}
|
||||
|
||||
fn match_objects(filter: &Value, obj: &Value) {
|
||||
match try_matches(filter, obj) {
|
||||
Ok(result) => {
|
||||
if result {
|
||||
println!("Filter matches the object");
|
||||
} else {
|
||||
println!("Filter does not match the object");
|
||||
}
|
||||
}
|
||||
Err(err) => match err {
|
||||
FilterError::InvalidFilter => println!("Invalid filter"),
|
||||
FilterError::UnknownOperator => println!("Unknown operator in filter"),
|
||||
FilterError::KeyNotFound => println!("Key not found in object"),
|
||||
},
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue