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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
use FromStr;
use crate;
use debug;
use ;
use ExpandableExt;
use SelfAddressingIdentifier;
// Extract attributes key pairs for ADD and MODIFY command
// pub fn extract_attribute_key_pairs(attr_pair: Pair) -> Option<(String, NestedValue)> {
// let mut key = String::new();
// let mut value = NestedValue::Value(String::new());
//
// debug!("Extracting the attribute from: {:?}", attr_pair);
// for item in attr_pair.into_inner() {
// match item.as_rule() {
// Rule::attr_key => {
// key = item.as_str().to_string();
// debug!("Extracting attribute key {:?}", key);
// }
// Rule::key_value => {
// if let Some(nested_item) = item.clone().into_inner().next() {
// match nested_item.as_rule() {
// Rule::string => {
// value = NestedValue::Value(
// nested_item
// .clone()
// .into_inner()
// .next_back()
// .unwrap()
// .as_span()
// .as_str()
// .to_string(),
// );
// }
// _ => {
// value = NestedValue::Value(item.as_str().to_string());
// }
// }
// }
// }
// Rule::json_object => {
// value = extract_json_object(item);
// }
// _ => {
// panic!("Invalid attribute in {:?}", item.as_rule());
// }
// }
// }
// Some((key, value))
// }
// pub fn extract_json_object(object: Pair) -> NestedValue {
// let mut json_object = IndexMap::new();
// for item in object.into_inner() {
// match item.as_rule() {
// Rule::json_pair => {
// let mut key = String::new();
// let mut value = NestedValue::Value(String::new());
// for el in item.clone().into_inner() {
// match el.as_rule() {
// Rule::json_key => {
// key = el
// .clone()
// .into_inner()
// .next_back()
// .unwrap()
// .into_inner()
// .next_back()
// .unwrap()
// .as_span()
// .as_str()
// .to_lowercase();
// }
// Rule::json_value => {
// if let Some(nested_item) = el.clone().into_inner().next() {
// match nested_item.as_rule() {
// Rule::string => {
// value = NestedValue::Value(
// nested_item
// .clone()
// .into_inner()
// .next_back()
// .unwrap()
// .as_span()
// .as_str()
// .to_string(),
// );
// }
// Rule::json_object => {
// value = extract_json_object(nested_item);
// }
// _ => {
// panic!("Invalid json value in {:?}", nested_item.as_rule());
// }
// }
// }
// }
// _ => {
// panic!("Invalid json pair in {:?}", el.as_rule());
// }
// }
// }
// json_object.insert(key, value);
// }
// _ => {
// panic!("Invalid json object in {:?}", item.as_rule());
// }
// }
// }
// NestedValue::Object(json_object)
// }
//
// TODO remove it
// pub fn extract_attributes_key_paris(object: Pair) -> Option<IndexMap<String, NestedValue>> {
// let mut attributes: IndexMap<String, NestedValue> = IndexMap::new();
//
// debug!("Extracting content of the attributes: {:?}", object);
// for attr in object.into_inner() {
// debug!("Inside the object: {:?}", attr);
// match attr.as_rule() {
// // Rule::attr_key_pairs => {
// // for attr in attr.into_inner() {
// // debug!("Parsing attribute {:?}", attr);
// // if let Some((key, value)) = extract_attribute_key_pairs(attr) {
// // debug!("Parsed attribute: {:?} = {:?}", key, value);
// // // TODO find out how to parse nested objects
// // attributes.insert(key, value);
// // } else {
// // debug!("Skipping attribute");
// // }
// // }
// // }
// Rule::attr_key => {
// debug!("Parsing attribute key {:?}", attr);
// let attr_key = attr.as_str().to_string();
// attributes.insert(attr_key, NestedValue::Value("".to_string()));
// }
//
// _ => {
// debug!(
// "Unexpected token: Skipping invalid attribute in instruction {:?}",
// attr.as_rule()
// );
// }
// }
// }
//
// Some(attributes)
// }
// TODO remove it
// Extract properties key pairs for any command
// pub fn extract_properites_key_pairs(object: Pair) -> Option<IndexMap<String, NestedValue>> {
// let mut properties: IndexMap<String, NestedValue> = IndexMap::new();
//
// debug!("Extracting properties from the object: {:?}", object);
// for attr in object.into_inner() {
// debug!("Inside the object: {:?}", attr);
// match attr.as_rule() {
// // Rule::prop_key_pairs => {
// // for prop in attr.into_inner() {
// // debug!("Parsing property {:?}", prop);
// // if let Some((key, value)) = extract_attribute_key_pairs(prop) {
// // debug!("Parsed property: {:?} = {:?}", key, value);
// // properties.insert(key, value);
// // } else {
// // debug!("Skipping property");
// // }
// // }
// // }
// Rule::lang => {
// debug!("Parsing language: {:?}", attr.as_str());
// properties.insert(
// "lang".to_string(),
// NestedValue::Value(attr.as_str().to_string()),
// );
// }
// Rule::alias => {
// debug!("Parsing target alias: {:?}", attr.as_str());
// properties.insert(
// "target".to_string(),
// NestedValue::Reference(RefValue::Name(attr.as_str().to_string())),
// );
// }
// Rule::said => {
// debug!("Parsing target said: {:?}", attr.as_str());
// if let Ok(said) = SelfAddressingIdentifier::from_str(attr.as_str()) {
// properties.insert(
// "target".to_string(),
// NestedValue::Reference(RefValue::Said(said)),
// );
// }
// }
// _ => {
// debug!(
// "Unexpected token: Invalid attribute in instruction {:?}",
// attr.as_rule()
// );
// }
// }
// }
// Some(properties)
// }
//
// TODO Remove it
// Extract content from any instruction related to any overlay
// pub fn extract_content(object: Pair) -> Content {
// let properties: Option<IndexMap<String, NestedValue>> =
// extract_properites_key_pairs(object.clone());
//
// Content {
// properties,
// version: None,
// }
// }