keyvalues-serde
is a (de)serialization library for
VDF text v1 and v2
built on the serde
framework. This library
leverages keyvalues-parser
for parsing and rendering the keyvalues text. This
makes it easy to deal with VDF text files using strongly typed Rust structures.
Note: this requires at least Rust 1.42.0
Just add the following to your Cargo.toml
toml
[dependencies]
keyvalues-serde = "0.1.0"
There is documentation available here and there are examples available in the examples directory
toml
[dependencies]
keyvalues-serde = "0.1.0"
serde = { version = "1.0.0", features = ["derive"] }
```rust use keyvaluesserde::{fromstr, Result as VdfResult}; use serde::Deserialize;
// Contents take from my ~/.data/Steam/steam/games/PlatformMenu.vdf const VDFTEXT: &str = r##" // this file defines the contents of the platform menu "Platform" { "Menu" { "Games" { "dll" "steamui" "interface" "SteamUIGames001" "MenuName" "#SteamGames" "SteamApp" "1" } "Friends" { "dll" "bin/friendsui" "interface" "VGuiModuleTracker001" "MenuName" "#AppFriends" } "Servers" { "dll" "bin/serverbrowser" "interface" "VGuiModuleServerBrowser001" "MenuName" "#AppServers" } "Settings" { "dll" "steamui" "interface" "VGuiModuleSettings001" "MenuName" "#App_Settings" "SteamApp" "1" } } } "##;
struct Platform { #[serde(rename = "Menu")] menu: Menu, }
struct Menu { games: MenuModule, friends: MenuModule, servers: MenuModule, settings: MenuModule, }
struct MenuModule {
dll: String,
interface: String,
#[serde(rename = "MenuName")]
menuname: String,
#[serde(rename = "SteamApp")]
steamapp: Option
fn main() -> VdfResult<()> { let platform: Platform = fromstr(VDFTEXT)?; println!("{:#?}", platform);
Ok(())
} ```
bool
i8
, i16
, i32
, i64
, i128
u8
, u16
, u32
, u64
, u128
f32
, f64
char
String
Option
null
type, so an optional value is considered Some
if present and None
if missingVec
-like types)
tuple
-like types)
HashMap
-like types)
{}
| Type | Reasoning | | :---: | :--- | | Byte Array | No clear VDF representation | | Unit | No clear VDF representation | | Unit Struct | No clear VDF representation | | Enum-containers (newtype, tuple, and struct variants) | The only clear usage would be the untagged representation in which case the ambiguity of types (everything is essentially just strings or objects) allows for too many footguns for me to be comfortable supporting |
Option
s may lead to unexpected ordering issues since a None
is just ommitted
Option
in the middle will be very problematicVec
s and Option
s with None
are both ommitted when serializing.Licensed under either of
at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.