crab_nbt

Crates.iocrab_nbt
lib.rscrab_nbt
version
sourcesrc
created_at2024-03-20 20:51:34.34671
updated_at2024-11-30 19:54:47.959051
descriptionUp-to-date Rust crate for easy and intuitive working with NBT data.
homepage
repositoryhttps://github.com/CrabCraftDev/CrabNBT
max_upload_size
id1180960
Cargo.toml error:TOML parse error at line 26, column 1 | 26 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Norbiros (Norbiros)

documentation

README

🦀 CrabNBT

Up-to-date Rust crate for easy and intuitive working with NBT data.

Why not other libraries?

CrabNBT combines best features of existing NBT crates, to create perfect, easy to use solution.
Big thanks to simdnbt and fastnbt for ideas!

Features

✅ Support for serializing to/from Struct (serde)
✅ Java string support
✅ nbt! macro for easy creation
✅ Easy to use system of retrieving values from NBT
✅ Serialization support for individual tags
✅ Support for Network NBT

Installing

cargo add crab_nbt

Serializing

use crab_nbt::{nbt, Nbt, NbtCompound};

// Using NBT macro
let nbt = nbt!("root nbt_inner name", {
    "float": 1.0,
    "key": "value",
    "long_array": [L; 1, 2],
    "int_array": [Int; 1, 10, 25],
    "byte_array": [B; 0, 1, 0, 0, 1],
    "list": ["a", "b", "c"],
    "nbt_inner": {
        "key": "sub value"
    }
});

let nbt = Nbt::new(
    "root".to_owned(),
    NbtCompound::from_iter([
        ("float".to_owned(), 1.0.into()),
        ("key".to_owned(), "value".into()),
        ("nbt_inner".to_owned(), NbtCompound::from_iter([
            ("key".to_owned(), "sub value".into()),
        ]).into())
    ])
);

let network_bytes = nbt.write_unnamed();
let normal_bytes = nbt.write();

Deserializing

use bytes::Bytes;
use crab_nbt::{nbt, Nbt, NbtCompound};

fn example(bytes: &mut Bytes) {
    let nbt = Nbt::read(bytes).unwrap();
    let egg_name = nbt
        .get_compound("nbt_inner")
        .and_then(|compound| compound.get_compound("egg"))
        .and_then(|compound| compound.get_string("name"))
        .unwrap();
}

Serde

Requires serde feature.

use crab_nbt::serde::{arrays::IntArray, ser::to_bytes_unnamed, de::from_bytes_unnamed};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test {
    number: i32,
    #[serde(with = "IntArray")]
    int_array: Vec<i32>,
}

fn cycle() {
    let test = Test {
        number: 5,
        int_array: vec![7, 8]
    };
    let mut bytes = to_bytes_unnamed(&test).unwrap();
    let recreated_struct: Test = from_bytes_unnamed(&mut bytes).unwrap();
    
    assert_eq!(test, recreated_struct);
}
Commit count: 45

cargo fmt