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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// use std::collections::HashMap;
// use std::fmt::{Debug, Display, Formatter};
//
// use crate::infograph::strings::{BoxedString, InlineString};
// use tokio::time::Duration;
// use yaml_rust::Yaml;
//
// #[derive(Clone, PartialEq, Debug)]
// pub enum Node {
//     Empty,
//     Float(f64),
//     Integer(i64),
//     BoxedString(BoxedString),
//     InlineString(InlineString),
//     Boolean(bool),
//     List(Vec<Node>),
//     Object(HashMap<String, Node>),
// }
//
// impl Node {
//     pub fn string(value: impl AsRef<str>) -> Self {
//         let string = value.as_ref();
//         // let length = string.len();
//         // if length <= MAX_STRING_INLINE_LEN {
//         //     let mut data = [0 as u8; MAX_STRING_INLINE_LEN];
//         //     data[0..length].clone_from_slice(&string.as_bytes()[0..length]);
//         //     Node::InlineString(InlineString {
//         //         data,
//         //         size: length as u8,
//         //     })
//         // } else {
//         //     Node::BoxedString(BoxedString {
//         //         data: string.as_bytes().into(),
//         //     })
//         // }
//         Node::BoxedString(BoxedString::from(string))
//     }
//     pub fn object() -> Self {
//         Node::Object(HashMap::new())
//     }
//
//     pub fn from_yaml(yaml: &Yaml) -> Self {
//         match yaml {
//             Yaml::Real(value) => value
//                 .parse::<f64>()
//                 .map(|val| Node::Float(val))
//                 .unwrap_or(Node::Empty),
//             Yaml::Integer(value) => Node::Integer(*value),
//             Yaml::String(value) => Node::string(value),
//             Yaml::Boolean(value) => Node::Boolean(*value),
//             Yaml::Array(value) => {
//                 let mut elements = Vec::new();
//                 for element in value.iter() {
//                     elements.push(Node::from_yaml(element));
//                 }
//
//                 Node::List(elements)
//             }
//             Yaml::Hash(value) => {
//                 let mut elements = HashMap::new();
//                 for element in value.iter() {
//                     if let Some(key) = element.0.as_str().map(|str| str.to_owned()) {
//                         let value = Node::from_yaml(&element.1);
//                         elements.insert(key, value);
//                     }
//                 }
//
//                 Node::Object(elements)
//             }
//             _ => Node::Empty,
//         }
//     }
//
//     pub fn is_empty(&self) -> bool {
//         if let Node::Empty = self {
//             true
//         } else {
//             false
//         }
//     }
//
//     pub fn is_filled(&self) -> bool {
//         self != &Node::Empty
//     }
//
//     pub fn is_float(&self) -> bool {
//         if let Node::Float(_) = self {
//             true
//         } else {
//             false
//         }
//     }
//
//     pub fn as_float(&self) -> Option<f64> {
//         match self {
//             Node::Float(value) => Some(*value),
//             _ => None,
//         }
//     }
//
//     pub fn is_int(&self) -> bool {
//         if let Node::Integer(_) = self {
//             true
//         } else {
//             false
//         }
//     }
//
//     pub fn as_int(&self) -> Option<i64> {
//         match self {
//             Node::Integer(value) => Some(*value),
//             _ => None,
//         }
//     }
//
//     pub fn as_required_int(&self) -> Option<i64> {
//         match self {
//             Node::Integer(value) if *value != 0 => Some(*value),
//             _ => None,
//         }
//     }
//
//     pub fn is_string(&self) -> bool {
//         match self {
//             Node::BoxedString(_) | Node::InlineString(_) => true,
//             _ => false,
//         }
//     }
//
//     pub fn as_str(&self) -> Option<&str> {
//         match self {
//             Node::BoxedString(value) => Some(value.as_ref()),
//             Node::InlineString(value) => Some(value.as_ref()),
//             _ => None,
//         }
//     }
//
//     pub fn as_required_str(&self) -> Option<&str> {
//         match self {
//             Node::BoxedString(value) if !value.is_empty() => Some(value.as_ref()),
//             Node::InlineString(value) if !value.is_empty() => Some(value.as_ref()),
//             _ => None,
//         }
//     }
//
//     pub fn as_size(&self) -> anyhow::Result<usize> {
//         match self {
//             Node::Integer(value) => Ok(*value as usize),
//             Node::BoxedString(value) => crate::fmt::parse_size(value),
//             Node::InlineString(value) => crate::fmt::parse_size(value),
//             _ => Err(anyhow::anyhow!("Expected either a string or a number.")),
//         }
//     }
//
//     pub fn as_required_size(&self) -> anyhow::Result<usize> {
//         match self.as_size() {
//             Ok(value) if value > 0 => Ok(value),
//             Ok(_) => Err(anyhow::anyhow!("Expected a positive size value.")),
//             other => other,
//         }
//     }
//
//     pub fn as_duration(&self) -> anyhow::Result<Duration> {
//         match self {
//             Node::Integer(value) => Ok(Duration::from_millis(*value as u64)),
//             Node::BoxedString(value) => crate::fmt::parse_duration(value),
//             Node::InlineString(value) => crate::fmt::parse_duration(value),
//             _ => Err(anyhow::anyhow!("Expected either a string or a number.")),
//         }
//     }
//
//     pub fn as_required_duration(&self) -> anyhow::Result<Duration> {
//         match self.as_duration() {
//             Ok(value) if value.as_micros() > 0 => Ok(value),
//             Ok(_) => Err(anyhow::anyhow!("Expected a positive duration.")),
//             other => other,
//         }
//     }
//
//     pub fn is_bool(&self) -> bool {
//         if let Node::Boolean(_) = self {
//             true
//         } else {
//             false
//         }
//     }
//
//     pub fn as_bool(&self) -> Option<bool> {
//         match self {
//             Node::Boolean(value) => Some(*value),
//             _ => None,
//         }
//     }
//
//     pub fn is_list(&self) -> bool {
//         if let Node::List(_) = self {
//             true
//         } else {
//             false
//         }
//     }
//
//     pub fn as_list(&self) -> Option<&Vec<Node>> {
//         match self {
//             Node::List(value) => Some(value),
//             _ => None,
//         }
//     }
//
//     pub fn is_object(&self) -> bool {
//         if let Node::Object(_) = self {
//             true
//         } else {
//             false
//         }
//     }
//
//     pub fn as_object(&self) -> Option<&HashMap<String, Node>> {
//         match self {
//             Node::Object(value) => Some(value),
//             _ => None,
//         }
//     }
//
//     pub fn put(&mut self, key: &str, value: Node) -> Option<Node> {
//         if let Node::Object(map) = self {
//             map.insert(key.to_owned(), value)
//         } else {
//             None
//         }
//     }
//
//     pub fn get(&self, key: &str) -> &Node {
//         if let Node::Object(map) = self {
//             if let Some(result) = map.get(key) {
//                 return result;
//             }
//         }
//
//         &Node::Empty
//     }
//
//     pub fn len(&self) -> usize {
//         match self {
//             Node::List(list) => list.len(),
//             Node::Object(map) => map.len(),
//             _ => 0,
//         }
//     }
//
//     pub fn at(&self, index: usize) -> &Node {
//         if let Node::List(list) = self {
//             if let Some(result) = list.get(index) {
//                 return result;
//             }
//         }
//
//         &Node::Empty
//     }
//
//     pub fn query(&self, path: &str) -> &Node {
//         let mut result = self;
//         for part in path.split(".").filter(|part| part.len() > 0) {
//             if let Node::Object(map) = &result {
//                 result = map.get(part).unwrap_or(&Node::Empty);
//             } else {
//                 return &Node::Empty;
//             }
//         }
//
//         result
//     }
//
//     pub fn query_first_filled(&self, first_path: &str, second_path: &str) -> &Node {
//         let result = self.query(first_path);
//         if result.is_filled() {
//             result
//         } else {
//             self.query(second_path)
//         }
//     }
//
//     pub fn or<'a>(&'a self, other: &'a Node) -> &'a Node {
//         if self.is_empty() {
//             other
//         } else {
//             self
//         }
//     }
//
//     pub fn or_get<'a>(&'a self, other: impl Fn() -> &'a Node) -> &'a Node {
//         if self.is_empty() {
//             other()
//         } else {
//             self
//         }
//     }
//
//     pub fn allocated_size(&self) -> usize {
//         match self {
//             Node::BoxedString(str) => 0, //std::mem::size_of::<Box<[u8]>>() + str.data.len(),
//             Node::InlineString(_) => 0,
//             Node::List(list) => {
//                 let vector_size = list.capacity() * std::mem::size_of::<Node>();
//                 let content_size: usize = list.iter().map(|node| node.allocated_size()).sum();
//
//                 vector_size + content_size
//             }
//             Node::Object(map) => {
//                 let entry_size = std::mem::size_of::<String>()
//                     + std::mem::size_of::<*const Node>()
//                     + std::mem::size_of::<*const Node>()
//                     + std::mem::size_of::<*const Node>()
//                     + std::mem::size_of::<*const String>()
//                     + std::mem::size_of::<Node>()
//                     + std::mem::size_of::<u64>();
//                 let map_size = (map.capacity() * 11 / 10).next_power_of_two() * entry_size;
//                 let content_size: usize = map
//                     .iter()
//                     .map(|node| node.0.len() + node.1.allocated_size())
//                     .sum();
//
//                 map_size + content_size
//             }
//             _ => 0,
//         }
//     }
// }