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
use crate::enums::ValueType;
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum KeyNode {
    Nil,
    Value(Value, Value),
    Node(HashMap<String, KeyNode>),
}

fn truncate(s: &str, max_chars: usize) -> String {
    match s.char_indices().nth(max_chars) {
        None => String::from(s),
        Some((idx, _)) => {
            let shorter = &s[..idx];
            let snip = "//SNIP//";
            let new_s = format!("{}{}", shorter, snip);
            new_s
        }
    }
}

impl KeyNode {
    pub fn absolute_keys_to_vec(&self, max_display_length: Option<usize>) -> Vec<ValueType> {
        let mut vec = Vec::new();
        self.absolute_keys(&mut vec, None, max_display_length);
        vec
    }

    pub fn absolute_keys(
        &self,
        keys: &mut Vec<ValueType>,
        key_from_root: Option<String>,
        max_display_length: Option<usize>,
    ) {
        let max_display_length = max_display_length.unwrap_or(4000);
        let val_key = |key: Option<String>| {
            key.map(|mut s| {
                s.push_str("->");
                s
            })
            .unwrap_or_default()
        };
        match self {
            KeyNode::Nil => {
                if let Some(key) = key_from_root {
                    keys.push(ValueType::new_key(key))
                }
            }
            KeyNode::Value(a, b) => keys.push(ValueType::new_value(
                val_key(key_from_root),
                truncate(a.to_string().as_str(), max_display_length),
                truncate(b.to_string().as_str(), max_display_length),
            )),
            KeyNode::Node(map) => {
                for (key, value) in map {
                    value.absolute_keys(
                        keys,
                        Some(format!("{}{}", val_key(key_from_root.clone()), key)),
                        Some(max_display_length),
                    )
                }
            }
        }
    }
}