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;
pub fn apply_filter(filter_selectors: &[Selector], json: &Value) -> ExtendedSelections {
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();
match selections
.iter()
.find_map(|selection| selection.clone().err())
{
Some(error) => Err(error),
None => Ok(MaybeArray::Array(selections.iter().fold(
Vec::new(),
|mut acc: Vec<Value>, selection| {
acc.push(json!(selection.clone().unwrap().last().unwrap().clone()));
acc
},
))),
}
}
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"))
}
}
}
}