#![allow(dead_code)] use fastnbt::error::Result; use fastnbt::{from_bytes, Value}; use flate2::read::GzDecoder; use serde::Deserialize; use std::io::Read; // This example show retrieving a players inventory from the palyer.dat file // found in the world/playerdata directory. // // In particular we are avoiding allocating a new string for every inventory // slot by instead having a &str with a lifetime tied to the input data. #[derive(Deserialize, Debug)] #[serde(rename_all = "PascalCase")] struct PlayerDat<'a> { data_version: i32, #[serde(borrow)] inventory: Vec>, ender_items: Vec>, } #[derive(Deserialize, Debug)] struct InventorySlot<'a> { id: &'a str, // We avoid allocating a string here. tag: Option, // Also get the less structured properties of the object. // We need to rename fields a lot. #[serde(rename = "Count")] count: i8, } fn main() { let args: Vec<_> = std::env::args().skip(1).collect(); let file = std::fs::File::open(args[0].clone()).unwrap(); // Player dat files are compressed with GZip. let mut decoder = GzDecoder::new(file); let mut data = vec![]; decoder.read_to_end(&mut data).unwrap(); let player: Result = from_bytes(data.as_slice()); println!("{:#?}", player); }