#parser #valve #serialization #serde #array #object

kv2

kv2 (keyvalues 2) format parser with serde support

3 releases

0.1.2 Nov 18, 2024
0.1.1 Nov 18, 2024
0.1.0 Nov 17, 2024

#1142 in Parser implementations

Download history 323/week @ 2024-11-14 62/week @ 2024-11-21

385 downloads per month

GPL-3.0-only

37KB
950 lines

CI Crates.io Version docs.rs

kv2

A Rust crate for parsing Valve's KeyValues2 (KV2) format.

Overview

kv2 is a Rust library for parsing and serializing the KeyValues2 (KV2) format used by Valve in their games and tools. It allows you to read KV2 files and access their data in a structured way.

Features

  • Parsing: Parsing KV2 Format.
  • Deserialization: Deserialization Serde Support for the KV2 parsing.
  • Serialization: TODO.
  • Handles Various Data Types: Supports booleans, integers, floats, strings, arrays, hex arrays(binary blobs), objects, and null values.
  • Customizable Parsing: Built using the nom parser combinator library for flexibility.

Installation

Add kv2 to your Cargo.toml dependencies:

[dependencies]
kv2 = { version = "0.1.2", features = ["serde"] }

Example

use kv2::parse_kv2;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct DmElement {
    id: String,
    name: String,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct DmeModel {
    id: String,
    visible: bool,
}

fn main() {
    let input = r#"
"DmElement"
{
"id" "elementid" "df939bf4-8dd6-435c-9eef-a6e25434ecca"
"name" "string" "root"
}

"DmeModel"
{
"id" "elementid" "90e0ae34-0671-478d-95f5-12fa5c905c7a"
"visible" "bool" "1"
}
        "#;

    match parse_kv2(input) {
        Ok(data) => {
            let element = DmElement::deserialize(data.1[0].clone());
            let model = DmeModel::deserialize(data.1[1].clone());
        }
        Err(e) => {
            error!("{:?}", e);
        }
    }
}

Dependencies

~1.2–2MB
~42K SLoC