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"),
|
||||
},
|
||||
}
|
||||
}
|
16
examples/comparison.rs
Normal file
16
examples/comparison.rs
Normal file
|
@ -0,0 +1,16 @@
|
|||
use jsonfilter::order;
|
||||
use serde_json::json;
|
||||
use std::cmp::Ordering;
|
||||
|
||||
fn main() {
|
||||
let a = json!(10);
|
||||
let b = json!(5);
|
||||
println!("Comparing JSON values:");
|
||||
println!("a: {}", a);
|
||||
println!("b: {}", b);
|
||||
match order(&a, &b) {
|
||||
Ordering::Greater => println!("a is greater than b"),
|
||||
Ordering::Less => println!("a is less than b"),
|
||||
Ordering::Equal => println!("a is equal to b"),
|
||||
}
|
||||
}
|
28
examples/error_handling.rs
Normal file
28
examples/error_handling.rs
Normal file
|
@ -0,0 +1,28 @@
|
|||
use jsonfilter::{try_matches, FilterError};
|
||||
use serde_json::json;
|
||||
|
||||
fn main() {
|
||||
let filter = json!({"name": "John", "other": "key"});
|
||||
let obj = json!({"name": "John", "age": 30});
|
||||
|
||||
println!("Applying filter:");
|
||||
println!("{}", filter);
|
||||
println!("To object:");
|
||||
println!("{}", obj);
|
||||
|
||||
match try_matches(&filter, &obj) {
|
||||
Ok(result) => {
|
||||
if result {
|
||||
println!("Filter matches the object");
|
||||
} else {
|
||||
println!("Filter does not match the object");
|
||||
}
|
||||
}
|
||||
// FilterError::KeyNotFound mostly means the object did not pass the filter, but it's still an error though
|
||||
Err(err) => match err {
|
||||
FilterError::InvalidFilter => println!("Invalid filter"),
|
||||
FilterError::UnknownOperator => println!("Unknown operator in filter"),
|
||||
FilterError::KeyNotFound => println!("Key not found in object"),
|
||||
},
|
||||
}
|
||||
}
|
17
examples/filter.rs
Normal file
17
examples/filter.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use jsonfilter::matches;
|
||||
use serde_json::json;
|
||||
|
||||
fn main() {
|
||||
let filter = json!({"name": "John", "age": 30});
|
||||
let obj = json!({"name": "John", "age": 30, "city": "New York"});
|
||||
|
||||
println!("Applying filter:");
|
||||
println!("{}", filter);
|
||||
println!("To object:");
|
||||
println!("{}", obj);
|
||||
if matches(&filter, &obj) {
|
||||
println!("Filter matches the object");
|
||||
} else {
|
||||
println!("Filter does not match the object");
|
||||
}
|
||||
}
|
53
examples/nested_filters.rs
Normal file
53
examples/nested_filters.rs
Normal file
|
@ -0,0 +1,53 @@
|
|||
use jsonfilter::{try_matches, FilterError};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
fn main() {
|
||||
// Filter can be as complex as you want
|
||||
let filter = json!({
|
||||
"$and": [
|
||||
{"$or": [
|
||||
{"age": {"$gte": 18}},
|
||||
{"is_student": true}
|
||||
]},
|
||||
{"$not": {
|
||||
"$or": [
|
||||
{"city": "New York"},
|
||||
{"city": "Los Angeles"}
|
||||
]
|
||||
}}
|
||||
]
|
||||
});
|
||||
|
||||
let obj1 = json!({"age": 25, "is_student": false, "city": "Chicago"});
|
||||
let obj2 = json!({"age": 16, "is_student": true, "city": "Miami"});
|
||||
let obj3 = json!({"age": 30, "is_student": false, "city": "New York"});
|
||||
|
||||
println!("Filter:");
|
||||
println!("{}", filter);
|
||||
println!("Objects:");
|
||||
println!("Object 1: {}", obj1);
|
||||
println!("Object 2: {}", obj2);
|
||||
println!("Object 3: {}", obj3);
|
||||
|
||||
// Matching objects against the filter
|
||||
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"),
|
||||
},
|
||||
}
|
||||
}
|
38
examples/range.rs
Normal file
38
examples/range.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use jsonfilter::{try_matches, FilterError};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
fn main() {
|
||||
let filter = json!({"$and": [{"age": {"$gte": 18}}, {"age": {"$lte": 30}}]});
|
||||
|
||||
let obj1 = json!({"name": "John Doe", "age": 25});
|
||||
let obj2 = json!({"name": "Alice Smith", "age": 35});
|
||||
let obj3 = json!({"name": "Bob Brown", "age": 20});
|
||||
|
||||
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"),
|
||||
},
|
||||
}
|
||||
}
|
39
examples/regex.rs
Normal file
39
examples/regex.rs
Normal file
|
@ -0,0 +1,39 @@
|
|||
use jsonfilter::{try_matches, FilterError};
|
||||
use serde_json::{json, Value};
|
||||
|
||||
fn main() {
|
||||
// You can filter text with regex
|
||||
let filter = json!({"description": {"$regex": "(Rust|Python)"}});
|
||||
|
||||
let obj1 = json!({"name": "John Doe", "description": "Enthusiastic about programming in Rust"});
|
||||
let obj2 = json!({"name": "Alice Smith", "description": "Experienced in web development"});
|
||||
let obj3 = json!({"name": "Bob Brown", "description": "Loves to code in Python"});
|
||||
|
||||
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