Skip to content

Commit

Permalink
feat: Add alternative methods, so Bytes is not required
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Sep 19, 2024
1 parent 8b0dbf7 commit e3a8d5e
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
17 changes: 17 additions & 0 deletions src/nbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bytes::{Buf, BufMut, Bytes, BytesMut};
use crab_nbt::nbt::compound::NbtCompound;
use crab_nbt::nbt::tag::NbtTag;
use crab_nbt::nbt::utils::*;
use std::io::Cursor;
use std::ops::Deref;

pub mod compound;
Expand Down Expand Up @@ -38,6 +39,10 @@ impl Nbt {
})
}

pub fn read_from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Nbt, Error> {
Self::read(cursor)
}

/// 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 impl Buf) -> Result<Nbt, Error> {
Expand All @@ -53,6 +58,10 @@ impl Nbt {
})
}

pub fn read_unnamed_from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<Nbt, Error> {
Self::read_unnamed(cursor)
}

pub fn write(&self) -> Bytes {
let mut bytes = BytesMut::new();
bytes.put_u8(COMPOUND_ID);
Expand All @@ -61,6 +70,10 @@ impl Nbt {
bytes.freeze()
}

pub fn write_to_vec(&self, vec: &mut Vec<u8>) {
vec.put(self.write());
}

/// 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 {
Expand All @@ -69,6 +82,10 @@ impl Nbt {
bytes.put(self.root_tag.serialize_content());
bytes.freeze()
}

pub fn write_unnamed_to_vec(&self, vec: &mut Vec<u8>) {
vec.put(self.write_unnamed());
}
}

impl Deref for Nbt {
Expand Down
14 changes: 12 additions & 2 deletions src/nbt/compound.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{error::Error, Nbt};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use crab_nbt::nbt::tag::NbtTag;
use crab_nbt::nbt::utils::{get_nbt_string, END_ID};
use derive_more::Into;
use std::collections::{hash_map::IntoIter, HashMap};

use crate::{error::Error, Nbt};
use std::io::Cursor;

#[derive(Clone, PartialEq, Debug, Default, Into)]
pub struct NbtCompound {
Expand Down Expand Up @@ -39,6 +39,12 @@ impl NbtCompound {
Ok(NbtCompound { child_tags })
}

pub fn deserialize_content_from_cursor(
cursor: &mut Cursor<&[u8]>,
) -> Result<NbtCompound, Error> {
Self::deserialize_content(cursor)
}

pub fn serialize_content(&self) -> Bytes {
let mut bytes = BytesMut::new();
for (name, tag) in &self.child_tags {
Expand All @@ -50,6 +56,10 @@ impl NbtCompound {
bytes.freeze()
}

pub fn serialize_content_to_vec(&self, vec: &mut Vec<u8>) {
vec.put(self.serialize_content());
}

pub fn put(&mut self, name: String, value: impl Into<NbtTag>) {
self.child_tags.insert(name, value.into());
}
Expand Down
12 changes: 12 additions & 0 deletions src/nbt/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crab_nbt::error::Error;
use crab_nbt::nbt::compound::NbtCompound;
use crab_nbt::nbt::utils::*;
use derive_more::From;
use std::io::Cursor;

/// Enum representing the different types of NBT tags.
/// Each variant corresponds to a different type of data that can be stored in an NBT tag.
Expand Down Expand Up @@ -88,6 +89,10 @@ impl NbtTag {
Self::deserialize_data(bytes, tag_id)
}

pub fn deserialize_from_cursor(cursor: &mut Cursor<&[u8]>) -> Result<NbtTag, Error> {
Self::deserialize(cursor)
}

pub fn deserialize_data(bytes: &mut impl Buf, tag_id: u8) -> Result<NbtTag, Error> {
match tag_id {
END_ID => Ok(NbtTag::End),
Expand Down Expand Up @@ -155,6 +160,13 @@ impl NbtTag {
}
}

pub fn deserialize_data_from_cursor(
cursor: &mut Cursor<&[u8]>,
tag_id: u8,
) -> Result<NbtTag, Error> {
Self::deserialize_data(cursor, tag_id)
}

pub fn extract_byte(&self) -> Option<i8> {
match self {
NbtTag::Byte(byte) => Some(*byte),
Expand Down
6 changes: 4 additions & 2 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ impl Serializer {
}
State::MapKey => {
if tag != STRING_ID {
return Err(Error::SerdeError(format!("Map key can only be string, not {tag}")));
return Err(Error::SerdeError(format!(
"Map key can only be string, not {tag}"
)));
}
}
State::ListElement => {}
Expand Down Expand Up @@ -156,7 +158,7 @@ impl<'a> ser::Serializer for &'a mut Serializer {
self.state = State::Named(v.to_string());
return Ok(());
}

self.output
.put(NbtTag::String(v.to_string()).serialize_data());
Ok(())
Expand Down
7 changes: 3 additions & 4 deletions tests/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,16 @@ enum Message {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Text {
#[serde(flatten)]
pub message: Message
pub message: Message,
}


#[test]
fn test_map_cycle() {
let test = Text {
message: Message::Request {
id: "a".to_string(),
method: 0
}
method: 0,
},
};
let mut bytes = to_bytes_unnamed(&test).unwrap();
let result: Text = from_bytes_unnamed(&mut bytes).unwrap();
Expand Down

0 comments on commit e3a8d5e

Please sign in to comment.