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
use crate::get_selection::get_selection;
use crate::types::{ExtendedSelections, MaybeArray, Selections, Selector};
use rayon::prelude::*;
use serde_json::json;
use serde_json::Value;
/// Apply the filter selectors to a JSON value and returns a selection.
pub fn apply_filter(filter_selectors: &[Selector], json: &Value) -> ExtendedSelections {
// Apply the filter iff the provided JSON value is an array.
match json.as_array() {
Some(array) => {
let selections: Vec<Selections> = array
.par_iter()
.cloned()
.map(|partial_json| -> Selections {
if filter_selectors.is_empty() {
Ok(vec![partial_json])
} else {
get_selection(&filter_selectors, &partial_json)
}
})
.collect();
// Try to find the first error.
match selections
.iter()
.find_map(|selection| selection.clone().err())
{
// Throw it back.
Some(error) => Err(error),
// No error in this case, we can safely unwrap.
None => Ok(MaybeArray::Array(selections.iter().fold(
Vec::new(),
|mut acc: Vec<Value>, selection| {
acc.push(json!(selection.clone().unwrap().last().unwrap().clone()));
acc
},
))),
}
}
// Not an array, return the raw JSON content if there's no filter or
// throw an error.
None => {
if filter_selectors.is_empty() {
Ok(MaybeArray::NonArray(vec![json.clone()]))
} else {
Err(String::from("A filter can only be applied to an array"))
}
}
}
}