use serde_json::{Value, Map};
use crate::reader::Reader;
pub fn read_vdf_string(reader: &mut Reader, magic: Option<u32>, strings: &Option<&mut Vec<String>>) -> String {
if magic.is_some() && magic.unwrap() == 0x07564429 {
let index: usize = reader.read_uint32(true).try_into().unwrap();
let string_pool = strings.as_ref().unwrap();
let string = &string_pool[index];
return string.to_owned();
} else {
return reader.read_string(None);
}
}
pub fn read_entry_map(reader: &mut Reader, magic: Option<u32>, strings: &Option<&mut Vec<String>>) -> Map<String, Value> {
let mut props = Map::new();
let mut field_type = reader.read_uint8(true);
while field_type != 0x08 {
let key = read_vdf_string(reader, magic, strings);
let value = read_entry_field(reader, field_type, magic, strings);
props.insert(key, value);
field_type = reader.read_uint8(true);
}
return props;
}
pub fn read_entry_field(reader: &mut Reader, field_type: u8, magic: Option<u32>, strings: &Option<&mut Vec<String>>) -> Value {
match field_type {
0x00 => { return Value::Object(read_entry_map(reader, magic, strings));
},
0x01 => { let value = reader.read_string(None);
return Value::String(value);
},
0x02 => { let value = reader.read_uint32(true);
return Value::Number(value.into());
},
_ => {
panic!("Unexpected field type {}!", field_type);
}
}
}