new-vdf-parser 0.3.0

A parser for Steam's VDF file formats including appinfo.vdf and shortcuts.vdf
Documentation
use serde_json::{Map, Value};

use crate::reader::Reader;

/// Reads a vdf entry string to JSON.
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);
    }
}

/// Reads a vdf entry map to JSON.
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;
}

/// Reads a vdf entry field to JSON.
pub fn read_entry_field(
    reader: &mut Reader,
    field_type: u8,
    magic: Option<u32>,
    strings: &Option<&mut Vec<String>>,
) -> Value {
    match field_type {
        0x00 => {
            //? map
            return Value::Object(read_entry_map(reader, magic, strings));
        }
        0x01 => {
            //? string
            let value = reader.read_string(None);
            return Value::String(value);
        }
        0x02 => {
            //? number
            let value = reader.read_uint32(true);
            return Value::Number(value.into());
        }
        0x0A => {
            //? int64 (signed)
            let value = reader.read_int64(true);
            return Value::from(value);
        }
        0x03 => {
            // float32
            let bits = reader.read_uint32(true);
            let value = f32::from_bits(bits);

            return serde_json::Number::from_f64(value as f64)
                .map(Value::Number)
                .unwrap_or(Value::Null);
        }
        0x04 | 0x06 => {
            //? u32
            let value = reader.read_uint32(true);
            return Value::Number(value.into());
        }
        0x05 => {
            // null terminated null-terminated
            let mut chars: Vec<u16> = Vec::new();

            loop {
                let char = reader.read_uint16(true);
                if char == 0 {
                    break;
                }

                chars.push(char);
            }

            return Value::String(String::from_utf16_lossy(&chars));
        }
        0x07 => {
            // uint64
            let value = reader.read_uint64(true);
            return Value::from(value);
        }
        _ => {
            panic!("Unexpected field type {}!", field_type);
        }
    }
}