2024-01-19 00:07:59 +00:00
|
|
|
use super::{Token, Class};
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
|
|
pub fn format(tree: Vec<Token>) -> HashMap<String, String> {
|
|
|
|
let mut output = HashMap::new();
|
|
|
|
let mut current_key = "".to_string();
|
2024-01-19 00:26:05 +00:00
|
|
|
let mut current_index;
|
2024-01-19 00:07:59 +00:00
|
|
|
let mut i = 0;
|
|
|
|
while i < tree.len() {
|
|
|
|
if tree[i].class == Class::IDENTIFIER {
|
|
|
|
current_key = tree[i].value.clone().unwrap_or("".to_string());
|
|
|
|
i = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-19 00:32:58 +00:00
|
|
|
if (tree[i].class == Class::LITERAL || tree[i].class == Class::BOOLEAN) && current_key != "".to_string() {
|
2024-01-19 00:07:59 +00:00
|
|
|
output.insert(current_key, tree[i].value.clone().unwrap_or("".to_string()));
|
|
|
|
current_key = "".to_string();
|
|
|
|
i = i + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if tree[i].class == Class::SEPARATOR && current_key != "".to_string() && tree[i].value.clone().unwrap_or("".to_string()) == "[".to_string() {
|
|
|
|
current_index = 0;
|
|
|
|
loop {
|
|
|
|
i = i + 1;
|
|
|
|
output.insert(format!("{}[{}]", current_key, current_index), tree[i].value.clone().unwrap_or("".to_string()));
|
|
|
|
current_index = current_index + 1;
|
|
|
|
i = i + 1;
|
|
|
|
if tree[i].class == Class::SEPARATOR && tree[i].value.clone().unwrap_or("".to_string()) == ",".to_string() {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2024-01-19 00:26:05 +00:00
|
|
|
output.insert(current_key.clone(), (current_index).to_string());
|
2024-01-19 00:07:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
i = i + 1;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|