Skip to content

Commit

Permalink
fix: Maybe final review fixes...
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Apr 7, 2024
1 parent 9c638f8 commit 04c1954
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Macro that simplifies the creation of Nbt using JSON/SNBT-like syntax.
/// Macro that simplifies the creation of NBT using JSON/SNBT-like syntax.
/// It takes a name and a content block, and returns an `Nbt` object.
///
/// # Examples
Expand Down
11 changes: 6 additions & 5 deletions src/nbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ pub mod compound;
pub mod tag;
mod utils;

/// The root nbt tag.
/// Represents the main NBT structure.
/// It contains the root compound tag of the NBT structure and its associated name
#[derive(Clone, PartialEq, Debug, Default)]
pub struct Nbt {
pub name: String,
Expand All @@ -24,7 +25,7 @@ impl Nbt {
}
}

pub fn read(bytes: &mut Bytes) -> Result<Nbt, Error> {
pub fn read(bytes: &mut impl Buf) -> Result<Nbt, Error> {
let tag_type_id = bytes.get_u8();

if tag_type_id != COMPOUND_ID {
Expand All @@ -37,9 +38,9 @@ impl Nbt {
})
}

/// Reads Nbt tag, that doesn't contain the name of root compound.
/// Reads NBT tag, that doesn't contain the name of root compound.
/// Used in [Network NBT](https://wiki.vg/NBT#Network_NBT_(Java_Edition)).
pub fn read_unnamed(bytes: &mut Bytes) -> Result<Nbt, Error> {
pub fn read_unnamed(bytes: &mut impl Buf) -> Result<Nbt, Error> {
let tag_type_id = bytes.get_u8();

if tag_type_id != COMPOUND_ID {
Expand All @@ -60,7 +61,7 @@ impl Nbt {
bytes.freeze()
}

/// Writes Nbt tag, without name of root compound.
/// Writes NBT tag, without name of root compound.
/// Used in [Network NBT](https://wiki.vg/NBT#Network_NBT_(Java_Edition)).
pub fn write_unnamed(&self) -> Bytes {
let mut bytes = BytesMut::new();
Expand Down
5 changes: 2 additions & 3 deletions src/nbt/compound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl NbtCompound {
}
}

pub fn deserialize_content(bytes: &mut Bytes) -> Result<NbtCompound, Error> {
pub fn deserialize_content(bytes: &mut impl Buf) -> Result<NbtCompound, Error> {
let mut child_tags = HashMap::new();

while !bytes.is_empty() {
Expand All @@ -42,7 +42,7 @@ impl NbtCompound {
pub fn serialize_content(&self) -> Bytes {
let mut bytes = BytesMut::new();
for (name, tag) in &self.child_tags {
bytes.put_u8(tag.id());
bytes.put_u8(tag.get_type_id());
bytes.put(NbtTag::String(name.clone()).serialize_data());
bytes.put(tag.serialize_data());
}
Expand Down Expand Up @@ -125,7 +125,6 @@ impl FromIterator<(String, NbtTag)> for NbtCompound {

impl IntoIterator for NbtCompound {
type Item = (String, NbtTag);

type IntoIter = IntoIter<String, NbtTag>;

fn into_iter(self) -> Self::IntoIter {
Expand Down
14 changes: 6 additions & 8 deletions src/nbt/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ pub enum NbtTag {

impl NbtTag {
/// Returns the numeric id associated with the data type.
pub const fn id(&self) -> u8 {
pub const fn get_type_id(&self) -> u8 {
// See https://doc.rust-lang.org/reference/items/enumerations.html#pointer-casting
unsafe { *(self as *const Self as *const u8) }
}

pub fn serialize(&self) -> Bytes {
let mut bytes = BytesMut::new();
bytes.put_u8(self.id());
bytes.put_u8(self.get_type_id());
bytes.put(self.serialize_data());
bytes.freeze()
}

/// Serializes tag data into bytes.
pub fn serialize_data(&self) -> Bytes {
let mut bytes = BytesMut::new();
match self {
Expand All @@ -59,7 +58,7 @@ impl NbtTag {
bytes.put_slice(&java_string);
}
NbtTag::List(list) => {
bytes.put_u8(list.first().unwrap_or(&NbtTag::End).id());
bytes.put_u8(list.first().unwrap_or(&NbtTag::End).get_type_id());
bytes.put_i32(list.len() as i32);
for nbt_tag in list {
bytes.put(nbt_tag.serialize_data())
Expand All @@ -84,13 +83,12 @@ impl NbtTag {
bytes.freeze()
}

pub fn deserialize(bytes: &mut Bytes) -> Result<NbtTag, Error> {
pub fn deserialize(bytes: &mut impl Buf) -> Result<NbtTag, Error> {
let tag_id = bytes.get_u8();
Self::deserialize_data(bytes, tag_id)
}

/// Deserializes tag data from bytes.
pub fn deserialize_data(bytes: &mut Bytes, tag_id: u8) -> Result<NbtTag, Error> {
pub fn deserialize_data(bytes: &mut impl Buf, tag_id: u8) -> Result<NbtTag, Error> {
match tag_id {
END_ID => Ok(NbtTag::End),
BYTE_ID => {
Expand Down Expand Up @@ -130,7 +128,7 @@ impl NbtTag {
let mut list = Vec::with_capacity(len as usize);
for _ in 0..len {
let tag = NbtTag::deserialize_data(bytes, tag_type_id)?;
assert_eq!(tag.id(), tag_type_id);
assert_eq!(tag.get_type_id(), tag_type_id);
list.push(tag);
}
Ok(NbtTag::List(list))
Expand Down
3 changes: 2 additions & 1 deletion tests/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use crab_nbt::{nbt, Nbt, NbtTag};

#[test]
fn serialize_data_string() {
let test_string = b"\0\x0cHow are you?"; // Length (12) + String
let serialized = NbtTag::String("How are you?".to_string()).serialize_data();
assert_eq!(serialized.to_vec(), b"\0\x0cHow are you?")
assert_eq!(serialized.to_vec(), test_string)
}

#[test]
Expand Down

0 comments on commit 04c1954

Please sign in to comment.