add some simple test to our nbt crate

This commit is contained in:
Alexander Medvedev 2024-12-27 00:27:24 +01:00
parent 25f4834236
commit 7161491005
6 changed files with 76 additions and 12 deletions

View File

@ -8,6 +8,8 @@ use serde::{Deserialize, Serialize};
pub enum ClickEvent<'a> { pub enum ClickEvent<'a> {
/// Opens a URL /// Opens a URL
OpenUrl(Cow<'a, str>), OpenUrl(Cow<'a, str>),
/// Opens a File
OpenFile(Cow<'a, str>),
/// Works in signs, but only on the root text component /// Works in signs, but only on the root text component
RunCommand(Cow<'a, str>), RunCommand(Cow<'a, str>),
/// Replaces the contents of the chat box with the text, not necessarily a /// Replaces the contents of the chat box with the text, not necessarily a

View File

@ -1,4 +1,4 @@
// TODO // TODO make this dynamic
#[derive(Clone)] #[derive(Clone)]
#[repr(i32)] #[repr(i32)]
pub enum EntityType { pub enum EntityType {

View File

@ -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> pub fn from_bytes<'a, T>(s: &'a mut impl Buf) -> Result<T>
where where
T: Deserialize<'a>, T: Deserialize<'a>,
@ -32,14 +32,6 @@ where
T::deserialize(&mut deserializer) T::deserialize(&mut deserializer)
} }
pub fn from_cursor<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = Deserializer::new(cursor, true);
T::deserialize(&mut deserializer)
}
/// Deserializes struct using Serde Deserializer from normal NBT /// 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_bytes_unnamed<'a, T>(s: &'a mut impl Buf) -> Result<T>
where where
@ -49,6 +41,14 @@ where
T::deserialize(&mut deserializer) T::deserialize(&mut deserializer)
} }
pub fn from_cursor<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = Deserializer::new(cursor, true);
T::deserialize(&mut deserializer)
}
pub fn from_cursor_unnamed<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T> pub fn from_cursor_unnamed<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
where where
T: Deserialize<'a>, T: Deserialize<'a>,

View File

@ -200,3 +200,65 @@ macro_rules! impl_array {
impl_array!(IntArray, "int"); impl_array!(IntArray, "int");
impl_array!(LongArray, "long"); impl_array!(LongArray, "long");
impl_array!(BytesArray, "byte"); 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);
}
}

View File

@ -30,4 +30,4 @@ aes = "0.8.4"
cfb8 = "0.8.1" cfb8 = "0.8.1"
# decryption # decryption
libdeflater = "1.22.0" libdeflater = "1.23.0"

View File

@ -46,7 +46,7 @@ rsa = "0.9.7"
rsa-der = "0.3.0" rsa-der = "0.3.0"
# authentication # authentication
reqwest = { version = "0.12.9", default-features = false, features = [ reqwest = { version = "0.12.10", default-features = false, features = [
"http2", "http2",
"json", "json",
"macos-system-configuration", "macos-system-configuration",