use nbtea::{NamedTag, Tag}; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename = "")] struct Codec {} #[test] fn test_empty() { let codec = Codec {}; let vec = nbtea::to_vec(&codec).unwrap(); assert_eq!(vec, b"\x0a\0\0\0"); let tag = Tag::Compound(vec![]); let vec = nbtea::root_tag_to_vec("", &tag).unwrap(); assert_eq!(vec, b"\x0a\0\0\0"); let tag = nbtea::root_tag_from_slice(&vec[..]).unwrap(); assert_eq!( tag, NamedTag { name: "".into(), tag: Tag::Compound(vec![]) } ); let codec2: Codec = nbtea::from_slice(&vec[..]).unwrap(); assert_eq!(codec, codec2); } #[derive(Debug, Serialize, Deserialize, PartialEq)] #[serde(rename = "hello world")] struct HelloWorld { pub name: String, } #[test] fn test_wiki_vg() { let hw = HelloWorld { name: "Bananrama".to_owned(), }; let vec = nbtea::to_vec(&hw).unwrap(); assert_eq!(vec, b"\x0a\x00\x0b\x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64\x08\x00\x04\x6e\x61\x6d\x65\x00\x09\x42\x61\x6e\x61\x6e\x72\x61\x6d\x61\x00"); let tag = nbtea::root_tag_from_slice(&vec[..]).unwrap(); assert_eq!( tag, NamedTag { name: "hello world".into(), tag: Tag::Compound(vec![NamedTag { name: "name".into(), tag: Tag::String("Bananrama".into()), }]) } ); let hw2: HelloWorld = nbtea::from_slice(&vec[..]).unwrap(); assert_eq!(hw, hw2); } #[derive(Debug, Deserialize, PartialEq)] struct LevelData { thundering: i8, #[serde(rename = "LastPlayed")] last_played: i64, } #[derive(Debug, Deserialize, PartialEq)] struct Level { #[serde(rename = "Data")] data: LevelData, } #[test] fn test_level_sample() { const DATA: &[u8] = include_bytes!("../samples/level.dat"); let _tag = nbtea::root_tag_from_slice_gzip(DATA).unwrap(); let level: Level = nbtea::from_slice_gzip(DATA).unwrap(); assert_eq!( level, Level { data: LevelData { thundering: 0, last_played: 1436477890870, } } ) }