Skip to content

Commit

Permalink
add some simple test to our nbt crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowiiii committed Dec 26, 2024
1 parent 25f4834 commit 7161491
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
2 changes: 2 additions & 0 deletions pumpkin-core/src/text/click.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
pub enum ClickEvent<'a> {
/// Opens a URL
OpenUrl(Cow<'a, str>),
/// Opens a File
OpenFile(Cow<'a, str>),
/// Works in signs, but only on the root text component
RunCommand(Cow<'a, str>),
/// Replaces the contents of the chat box with the text, not necessarily a
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-entity/src/entity_type.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// TODO
// TODO make this dynamic
#[derive(Clone)]
#[repr(i32)]
pub enum EntityType {
Expand Down
12 changes: 6 additions & 6 deletions pumpkin-nbt/src/deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'de, T: Buf> Deserializer<'de, T> {
}
}

/// Deserializes struct using Serde Deserializer from unnamed (network) NBT
/// Deserializes struct using Serde Deserializer from normal NBT
pub fn from_bytes<'a, T>(s: &'a mut impl Buf) -> Result<T>
where
T: Deserialize<'a>,
Expand All @@ -32,20 +32,20 @@ where
T::deserialize(&mut deserializer)
}

pub fn from_cursor<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
/// Deserializes struct using Serde Deserializer from normal NBT
pub fn from_bytes_unnamed<'a, T>(s: &'a mut impl Buf) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = Deserializer::new(cursor, true);
let mut deserializer = Deserializer::new(s, false);
T::deserialize(&mut deserializer)
}

/// Deserializes struct using Serde Deserializer from normal NBT
pub fn from_bytes_unnamed<'a, T>(s: &'a mut impl Buf) -> Result<T>
pub fn from_cursor<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = Deserializer::new(s, false);
let mut deserializer = Deserializer::new(cursor, true);
T::deserialize(&mut deserializer)
}

Expand Down
62 changes: 62 additions & 0 deletions pumpkin-nbt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,65 @@ macro_rules! impl_array {
impl_array!(IntArray, "int");
impl_array!(LongArray, "long");
impl_array!(BytesArray, "byte");

#[cfg(test)]
mod test {
use serde::{Deserialize, Serialize};

use crate::BytesArray;
use crate::IntArray;
use crate::LongArray;
use crate::{
deserializer::from_bytes_unnamed,
serializer::to_bytes_unnamed,
};

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct Test {
byte: i8,
short: i16,
int: i32,
long: i64,
float: f32,
string: String,
}

#[test]
fn test_simple_ser_de_unamed() {
let test = Test {
byte: 123,
short: 1342,
int: 4313,
long: 34,
float: 1.00,
string: "Hello test".to_string(),
};
let mut bytes = to_bytes_unnamed(&test).unwrap();
let recreated_struct: Test = from_bytes_unnamed(&mut bytes).unwrap();

assert_eq!(test, recreated_struct);
}

#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct TestArray {
#[serde(with = "BytesArray")]
byte_array: Vec<u8>,
#[serde(with = "IntArray")]
int_array: Vec<i32>,
#[serde(with = "LongArray")]
long_array: Vec<i64>,
}

#[test]
fn test_simple_ser_de_array() {
let test = TestArray {
byte_array: vec![0, 3, 2],
int_array: vec![13, 1321, 2],
long_array: vec![1, 0, 200301, 1],
};
let mut bytes = to_bytes_unnamed(&test).unwrap();
let recreated_struct: TestArray = from_bytes_unnamed(&mut bytes).unwrap();

assert_eq!(test, recreated_struct);
}
}
2 changes: 1 addition & 1 deletion pumpkin-protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ aes = "0.8.4"
cfb8 = "0.8.1"

# decryption
libdeflater = "1.22.0"
libdeflater = "1.23.0"
2 changes: 1 addition & 1 deletion pumpkin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ rsa = "0.9.7"
rsa-der = "0.3.0"

# authentication
reqwest = { version = "0.12.9", default-features = false, features = [
reqwest = { version = "0.12.10", default-features = false, features = [
"http2",
"json",
"macos-system-configuration",
Expand Down

0 comments on commit 7161491

Please sign in to comment.