jupiter_rs/node.rs
1// use std::collections::HashMap;
2// use std::fmt::{Debug, Display, Formatter};
3//
4// use crate::infograph::strings::{BoxedString, InlineString};
5// use tokio::time::Duration;
6// use yaml_rust::Yaml;
7//
8// #[derive(Clone, PartialEq, Debug)]
9// pub enum Node {
10// Empty,
11// Float(f64),
12// Integer(i64),
13// BoxedString(BoxedString),
14// InlineString(InlineString),
15// Boolean(bool),
16// List(Vec<Node>),
17// Object(HashMap<String, Node>),
18// }
19//
20// impl Node {
21// pub fn string(value: impl AsRef<str>) -> Self {
22// let string = value.as_ref();
23// // let length = string.len();
24// // if length <= MAX_STRING_INLINE_LEN {
25// // let mut data = [0 as u8; MAX_STRING_INLINE_LEN];
26// // data[0..length].clone_from_slice(&string.as_bytes()[0..length]);
27// // Node::InlineString(InlineString {
28// // data,
29// // size: length as u8,
30// // })
31// // } else {
32// // Node::BoxedString(BoxedString {
33// // data: string.as_bytes().into(),
34// // })
35// // }
36// Node::BoxedString(BoxedString::from(string))
37// }
38// pub fn object() -> Self {
39// Node::Object(HashMap::new())
40// }
41//
42// pub fn from_yaml(yaml: &Yaml) -> Self {
43// match yaml {
44// Yaml::Real(value) => value
45// .parse::<f64>()
46// .map(|val| Node::Float(val))
47// .unwrap_or(Node::Empty),
48// Yaml::Integer(value) => Node::Integer(*value),
49// Yaml::String(value) => Node::string(value),
50// Yaml::Boolean(value) => Node::Boolean(*value),
51// Yaml::Array(value) => {
52// let mut elements = Vec::new();
53// for element in value.iter() {
54// elements.push(Node::from_yaml(element));
55// }
56//
57// Node::List(elements)
58// }
59// Yaml::Hash(value) => {
60// let mut elements = HashMap::new();
61// for element in value.iter() {
62// if let Some(key) = element.0.as_str().map(|str| str.to_owned()) {
63// let value = Node::from_yaml(&element.1);
64// elements.insert(key, value);
65// }
66// }
67//
68// Node::Object(elements)
69// }
70// _ => Node::Empty,
71// }
72// }
73//
74// pub fn is_empty(&self) -> bool {
75// if let Node::Empty = self {
76// true
77// } else {
78// false
79// }
80// }
81//
82// pub fn is_filled(&self) -> bool {
83// self != &Node::Empty
84// }
85//
86// pub fn is_float(&self) -> bool {
87// if let Node::Float(_) = self {
88// true
89// } else {
90// false
91// }
92// }
93//
94// pub fn as_float(&self) -> Option<f64> {
95// match self {
96// Node::Float(value) => Some(*value),
97// _ => None,
98// }
99// }
100//
101// pub fn is_int(&self) -> bool {
102// if let Node::Integer(_) = self {
103// true
104// } else {
105// false
106// }
107// }
108//
109// pub fn as_int(&self) -> Option<i64> {
110// match self {
111// Node::Integer(value) => Some(*value),
112// _ => None,
113// }
114// }
115//
116// pub fn as_required_int(&self) -> Option<i64> {
117// match self {
118// Node::Integer(value) if *value != 0 => Some(*value),
119// _ => None,
120// }
121// }
122//
123// pub fn is_string(&self) -> bool {
124// match self {
125// Node::BoxedString(_) | Node::InlineString(_) => true,
126// _ => false,
127// }
128// }
129//
130// pub fn as_str(&self) -> Option<&str> {
131// match self {
132// Node::BoxedString(value) => Some(value.as_ref()),
133// Node::InlineString(value) => Some(value.as_ref()),
134// _ => None,
135// }
136// }
137//
138// pub fn as_required_str(&self) -> Option<&str> {
139// match self {
140// Node::BoxedString(value) if !value.is_empty() => Some(value.as_ref()),
141// Node::InlineString(value) if !value.is_empty() => Some(value.as_ref()),
142// _ => None,
143// }
144// }
145//
146// pub fn as_size(&self) -> anyhow::Result<usize> {
147// match self {
148// Node::Integer(value) => Ok(*value as usize),
149// Node::BoxedString(value) => crate::fmt::parse_size(value),
150// Node::InlineString(value) => crate::fmt::parse_size(value),
151// _ => Err(anyhow::anyhow!("Expected either a string or a number.")),
152// }
153// }
154//
155// pub fn as_required_size(&self) -> anyhow::Result<usize> {
156// match self.as_size() {
157// Ok(value) if value > 0 => Ok(value),
158// Ok(_) => Err(anyhow::anyhow!("Expected a positive size value.")),
159// other => other,
160// }
161// }
162//
163// pub fn as_duration(&self) -> anyhow::Result<Duration> {
164// match self {
165// Node::Integer(value) => Ok(Duration::from_millis(*value as u64)),
166// Node::BoxedString(value) => crate::fmt::parse_duration(value),
167// Node::InlineString(value) => crate::fmt::parse_duration(value),
168// _ => Err(anyhow::anyhow!("Expected either a string or a number.")),
169// }
170// }
171//
172// pub fn as_required_duration(&self) -> anyhow::Result<Duration> {
173// match self.as_duration() {
174// Ok(value) if value.as_micros() > 0 => Ok(value),
175// Ok(_) => Err(anyhow::anyhow!("Expected a positive duration.")),
176// other => other,
177// }
178// }
179//
180// pub fn is_bool(&self) -> bool {
181// if let Node::Boolean(_) = self {
182// true
183// } else {
184// false
185// }
186// }
187//
188// pub fn as_bool(&self) -> Option<bool> {
189// match self {
190// Node::Boolean(value) => Some(*value),
191// _ => None,
192// }
193// }
194//
195// pub fn is_list(&self) -> bool {
196// if let Node::List(_) = self {
197// true
198// } else {
199// false
200// }
201// }
202//
203// pub fn as_list(&self) -> Option<&Vec<Node>> {
204// match self {
205// Node::List(value) => Some(value),
206// _ => None,
207// }
208// }
209//
210// pub fn is_object(&self) -> bool {
211// if let Node::Object(_) = self {
212// true
213// } else {
214// false
215// }
216// }
217//
218// pub fn as_object(&self) -> Option<&HashMap<String, Node>> {
219// match self {
220// Node::Object(value) => Some(value),
221// _ => None,
222// }
223// }
224//
225// pub fn put(&mut self, key: &str, value: Node) -> Option<Node> {
226// if let Node::Object(map) = self {
227// map.insert(key.to_owned(), value)
228// } else {
229// None
230// }
231// }
232//
233// pub fn get(&self, key: &str) -> &Node {
234// if let Node::Object(map) = self {
235// if let Some(result) = map.get(key) {
236// return result;
237// }
238// }
239//
240// &Node::Empty
241// }
242//
243// pub fn len(&self) -> usize {
244// match self {
245// Node::List(list) => list.len(),
246// Node::Object(map) => map.len(),
247// _ => 0,
248// }
249// }
250//
251// pub fn at(&self, index: usize) -> &Node {
252// if let Node::List(list) = self {
253// if let Some(result) = list.get(index) {
254// return result;
255// }
256// }
257//
258// &Node::Empty
259// }
260//
261// pub fn query(&self, path: &str) -> &Node {
262// let mut result = self;
263// for part in path.split(".").filter(|part| part.len() > 0) {
264// if let Node::Object(map) = &result {
265// result = map.get(part).unwrap_or(&Node::Empty);
266// } else {
267// return &Node::Empty;
268// }
269// }
270//
271// result
272// }
273//
274// pub fn query_first_filled(&self, first_path: &str, second_path: &str) -> &Node {
275// let result = self.query(first_path);
276// if result.is_filled() {
277// result
278// } else {
279// self.query(second_path)
280// }
281// }
282//
283// pub fn or<'a>(&'a self, other: &'a Node) -> &'a Node {
284// if self.is_empty() {
285// other
286// } else {
287// self
288// }
289// }
290//
291// pub fn or_get<'a>(&'a self, other: impl Fn() -> &'a Node) -> &'a Node {
292// if self.is_empty() {
293// other()
294// } else {
295// self
296// }
297// }
298//
299// pub fn allocated_size(&self) -> usize {
300// match self {
301// Node::BoxedString(str) => 0, //std::mem::size_of::<Box<[u8]>>() + str.data.len(),
302// Node::InlineString(_) => 0,
303// Node::List(list) => {
304// let vector_size = list.capacity() * std::mem::size_of::<Node>();
305// let content_size: usize = list.iter().map(|node| node.allocated_size()).sum();
306//
307// vector_size + content_size
308// }
309// Node::Object(map) => {
310// let entry_size = std::mem::size_of::<String>()
311// + std::mem::size_of::<*const Node>()
312// + std::mem::size_of::<*const Node>()
313// + std::mem::size_of::<*const Node>()
314// + std::mem::size_of::<*const String>()
315// + std::mem::size_of::<Node>()
316// + std::mem::size_of::<u64>();
317// let map_size = (map.capacity() * 11 / 10).next_power_of_two() * entry_size;
318// let content_size: usize = map
319// .iter()
320// .map(|node| node.0.len() + node.1.allocated_size())
321// .sum();
322//
323// map_size + content_size
324// }
325// _ => 0,
326// }
327// }
328// }