Named Binary Tag (NBT)
The The Named Binary Tag, is a structured binary format used by the game Minecraft for a variety of purposes such as, Player Data and World Saves as well as being used within the Minecraft Protocol.
This Crate
This crate is yet another implementation of the NBT format.
Key features
- Support for Serialisation and Deserialization with the Serde framework.
- Ability to create partial or complete documents through the
TagandBlobobjects. - Ability to read/write from a socket or buffer.
Cargo Features
with_serde(default) includes Serde serialisation and deserialization support.serde_booleanconverts booleans to bytes during serialisation and deserialization.serde_unsignedconverts unsigned to their signed counterparts during serialisation and deserialization.arraysutils for writing byte, int and long arrays. (dev branch)compressiongzip and DEFLATE support. (dev branch)
Operation
This crate has two seperate operations that allow data to be mutated.
- Read/Writing -
Tags/Blobs<--NBT-->Bytes/Buffer - Encoding/Decoding -
Tags/Blobs<--Serde-->Structs
Quick Start
Tags
One way of creating partial NBT objects is with tags.
use Tag;
// An example of a TAG_Byte with value 42.
let byte = Byte;
// An example of a TAG_String with value "Hello!"
let string = String;
// An example of TAG_List containing bytes, with values of [1,2,3]
let list = List;
// An example of a compound
use HashMap;
let mut map = new;
map.insert;
map.insert;
let compound = Compound;
Blobs
Blobs allow for you to create full NBT objects/documents.
use ;
// Creating a blob
let mut blob = new;
// Inserting a tag
blob.insert;
// Using the ToTag trait to insert a tag.
blob.insert;
Blobs reprsent the root compound, and hence can be named.
When a name is not specifed it defaults to ""
use ;
// Creating a blob with a name
let mut blob = create;
blob.insert;
Encoding / Writing
You can encode a partial or full NBT object using the NBTWrite trait.
use ;
let tag = Float;
// Writing to a buffer
let mut buffer = Vecnew;
tag.write.unwrap;
// Outputting to a Vec.
let bytes = tag.bytes.unwrap;
println!;
Decoding / Reading
use ;
let data = /* vec![...] */
# vec!;
// Reading from a buffer
use Cursor;
let mut cursor = new;
let one = read.unwrap;
// Also works for: Blob::read(...)
// Reading from vec
let two = from_bytes;
// Also works for: Blob::from_bytes(...)
Serde
This library has full serde serialisation and deserialization support for all types in the serde data model except for u8 (byte)arrays.
Serde support requires the with_serde cargo feature, which is enabled by default.
Encoding
Here is an basic example for encoding between a struct and bytes
use Serialize;
use ;
// Define a Serializable struct
// Instantiate
let hello = HelloWorld ;
// Encode/Serialise the struct into a blob.
let blob = encode.unwrap;
// get the bytes from the blob.
let bytes = blob.bytes.unwrap;
assert_eq!
Decoding
Here is the reverse operation for the above example
use Deserialize;
use ;
// Define a Deserialisable struct
// Bytes
let bytes = vec!;
// Create a blob from bytes
let blob = from_bytes.unwrap;
// Deserialize the blob into the struct
let hello = .unwrap;
assert_eq!;
Serde Functions
BLOB --> SERDEdecode(...)BLOB <-- SERDEencode(...)TAG --> SERDEdecode_tag(...)TAG <-- SERDEencode_tag(...)BLOB --> SERDE + NAMEdecode_named(...)BLOB <-- SERDE + NAMEencode_named(...)