source-kv
A straightforward, high-performance serde implementation for parsing and serializing Valve's textual Key-Values formats (such as .vmf, .vmt, gameinfo.txt, GameConfig.txt, etc.).
Key Features
- Full Serde Integration: Seamlessly deserialize into custom Rust structs and serialize back using standard
#[derive(Deserialize, Serialize)]attributes. - Duplicate Key Support: Valve's format heavily relies on duplicate keys (especially in
.vmffiles).source-kvnatively handles this—just map repeated keys to aVec<T>. - Order Preservation: Internally uses
IndexMapto guarantee that the order of properties and blocks is preserved during serialization. - Syntax Tolerance: Safely handles unquoted keys, C-style line comments (
//), and implicitly converts integer/boolean strings into native Rust types. - Dynamic / Untyped API: Don't want to write static structs? Parse files directly into a generic
source_kv::Value(AST) and easily convert specific nodes to typed structs later usingfrom_value.
Quick Start
1. Parsing Strongly-Typed Structures (e.g., VMF Entities)
Repeated keys are easily captured using Vec<T> alongside #[serde(rename = "...")].
use Deserialize;
2. Using the Dynamic AST (source_kv::Value)
Sometimes you don't know the exact structure upfront. You can parse the document into an untyped tree and deserialize specific parts on demand.
use Deserialize;
use ;
API Overview
source_kv::from_str— Deserialize a KeyValues string directly into a typed structT.source_kv::to_string— Serialize a typed structTback into KeyValues format.source_kv::Value— An enum representing the AST (StrorObj), offering ergonomic getters like.get(key)and.get_string(key).source_kv::from_value— Convert an existing ASTValuenode into a Serde-compatible structT.
License
MIT License.