| Crates.io | keyvalues-serde |
| lib.rs | keyvalues-serde |
| version | 0.2.2 |
| created_at | 2021-08-08 23:06:01.25244+00 |
| updated_at | 2025-02-03 04:07:06.111116+00 |
| description | (De)serialize VDF text with serde |
| homepage | https://github.com/CosmicHorrorDev/vdf-rs/tree/main/keyvalues-serde |
| repository | https://github.com/CosmicHorrorDev/vdf-rs |
| max_upload_size | |
| id | 433384 |
| size | 134,242 |
keyvalues-serde is a (de)serialization library for
VDF text v1
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.
Just add the library to your Cargo.toml
$ cargo add keyvalues-serde
$ cargo add keyvalues-serde
$ cargo add serde --features derive
use serde::Deserialize;
// Contents take from my ~/.data/Steam/steam/games/PlatformMenu.vdf
const VDF_TEXT: &str = r##"
// this file defines the contents of the platform menu
"Platform"
{
"Menu"
{
"Games"
{
"dll" "steamui"
"interface" "SteamUIGames001"
"MenuName" "#Steam_Games"
"SteamApp" "1"
}
"Friends"
{
"dll" "bin/friendsui"
"interface" "VGuiModuleTracker001"
"MenuName" "#App_Friends"
}
"Servers"
{
"dll" "bin/serverbrowser"
"interface" "VGuiModuleServerBrowser001"
"MenuName" "#App_Servers"
}
"Settings"
{
"dll" "steamui"
"interface" "VGuiModuleSettings001"
"MenuName" "#App_Settings"
"SteamApp" "1"
}
}
}
"##;
#[derive(Deserialize, Debug)]
struct Platform {
#[serde(rename = "Menu")]
menu: Menu,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "PascalCase")]
struct Menu {
games: MenuModule,
friends: MenuModule,
servers: MenuModule,
settings: MenuModule,
}
#[derive(Deserialize, Debug)]
struct MenuModule {
dll: String,
interface: String,
#[serde(rename = "MenuName")]
menu_name: String,
#[serde(rename = "SteamApp")]
steam_app: Option<bool>,
}
fn main() -> keyvalues_serde::Result<()> {
let platform: Platform = keyvalues_serde::from_str(VDF_TEXT)?;
println!("{:#?}", platform);
Ok(())
}
booli8, i16, i32, i64, i128u8, u16, u32, u64, u128f32, f64charStringOption
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 |
Options may lead to unexpected ordering issues since a None is just omitted
Option in the middle will be very problematicVecs and Options with None are both omitted 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.