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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::array_walker::array_walker;
use crate::range_selector::range_selector;
use crate::types::{Display, Selection, Selections, Selector, Selectors};
use serde_json::{json, Value};
fn apply_selector(
inner_json: &Value,
map_index: usize,
raw_selector: &str,
selectors: &Selectors,
) -> Selection {
// No JSON value has been found.
if inner_json.get(raw_selector).is_none() {
if map_index == 0 {
return Err([
r#"Node ""#,
raw_selector,
r#"" not found on the parent element"#,
]
.join(""));
} else {
return Err([
r#"Node ""#,
raw_selector,
r#"" not found on parent "#,
&selectors[map_index - 1].as_str(false),
]
.join(""));
}
}
// Default case.
Ok(inner_json[raw_selector].clone())
}
/// Returns a selection based on selectors and a JSON content as a Result of
/// values or an Err early on, stopping the iteration as soon as the latter is
/// encountered.
pub fn get_selection(selectors: &Selectors, json: &Value) -> Selections {
// Local copy of the original JSON that will be reused in the loop.
let mut inner_json = json.clone();
selectors
.iter()
.enumerate()
.map(|(map_index, current_selector)| -> Selection {
match current_selector {
// Object selector.
Selector::Object(properties) => {
properties
.iter()
.fold(Ok(json!({})), |acc: Selection, property| {
let value = apply_selector(&inner_json, map_index, property, selectors);
match value {
Ok(value) => match acc {
Ok(mut current) => {
// Get the associated mutable Map and insert
// the property.
current
.as_object_mut()
.unwrap()
.insert(property.clone(), value);
Ok(current)
}
Err(error) => Err(error),
},
Err(error) => Err(error),
}
})
}
// Default selector.
Selector::Default(raw_selector) => {
match apply_selector(&inner_json, map_index, raw_selector, selectors) {
Ok(ref json) => {
inner_json = json.clone();
Ok(json.clone())
}
Err(error) => Err(error),
}
}
// Range selector.
Selector::Range((start, end)) => match range_selector(
*end,
&inner_json.clone(),
map_index,
if map_index == 0 {
None
} else {
Some(&selectors[map_index - 1])
},
&selectors,
*start,
) {
Ok(json) => {
inner_json = json.clone();
Ok(json)
}
Err(error) => Err(error),
},
// Array selector.
Selector::Array => match range_selector(
None,
&inner_json.clone(),
map_index,
if map_index == 0 {
None
} else {
Some(&selectors[map_index - 1])
},
&selectors,
Some(0),
) {
Ok(json) => {
inner_json = json.clone();
Ok(json)
}
Err(error) => Err(error),
},
// Index selector.
Selector::Index(array_indexes) => {
match array_walker(&array_indexes, &inner_json, map_index, &selectors) {
Ok(json) => {
inner_json = json.clone();
Ok(json)
}
Err(error) => Err(error),
}
}
}
})
.collect()
}