From 6ec8c678d3ae519eb59a08b4f8db1b9f751d7405 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 12 Jan 2024 18:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A5=BD=E6=B4=BB=EF=BC=8C=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E5=95=A5=E6=BF=80=E8=BF=9B=E7=9A=84=E4=B8=9C=E8=A5=BF=E9=83=BD?= =?UTF-8?q?=E5=A0=86=E8=BF=9B=E6=9D=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- v2/src/data_struct.rs | 105 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 7 deletions(-) diff --git a/v2/src/data_struct.rs b/v2/src/data_struct.rs index 2497125..44c83f9 100644 --- a/v2/src/data_struct.rs +++ b/v2/src/data_struct.rs @@ -20,18 +20,109 @@ impl<'data> RawData<'data> { pub fn new(raw_data: &'data [u8], length: usize) -> Self { Self { raw_data, length } } } + #[allow(unused)] pub enum Value<'value> { - Byte(RawData<'value>), - Short(RawData<'value>), - Int(RawData<'value>), - Long(RawData<'value>), - Float(RawData<'value>), - Double(RawData<'value>), - String(RawData<'value>), + Byte(i8), + Short(i16), + Int(i32), + Long(i64), + Float(f32), + Double(f64), + String(&'value str), ByteArray(RawData<'value>), IntArray(RawData<'value>), LongArray(RawData<'value>), List(Vec>), Compound(HashMap<&'value str, Value<'value>>), } + + +#[allow(unused)] +pub enum ListContent<'value> { + ByteList(Vec), + ShortList(Vec), + IntList(Vec), + LongList(Vec), + FloatList(Vec), + DoubleList(Vec), + StringList(Vec<&'value str>), + ByteArrayList(Vec>), + IntArrayList(Vec>), + LongArrayList(Vec>), + CompoundList(Vec>>), + ListList(Vec>), +} + +#[allow(unused)] +impl<'value> Value<'value> { + #[inline(always)] + pub fn read_byte(data: &mut [u8]) -> Self { + let (value, data) = data.split_at(1); + Self::Byte(value[0] as i8) + } + #[inline(always)] + pub fn read_short(data: &mut [u8]) -> Self { + let (value, data) = data.split_at(2); + #[cfg(target_endian = "little")] + return Self::Short(i16::from_be_bytes([value[0], value[1]])); + #[cfg(target_endian = "big")] + return Self::Short(i16::from_le_bytes([value[0], value[1]])); + } + #[inline(always)] + pub fn read_int(data: &mut [u8]) -> Self { + let (value, data) = data.split_at(4); + #[cfg(target_endian = "little")] + return Self::Int(i32::from_be_bytes([value[0], value[1], value[2], value[3]])); + #[cfg(target_endian = "big")] + return Self::Int(i32::from_le_bytes([value[0], value[1], value[2], value[3]])); + } + #[inline(always)] + pub fn read_long(data: &mut [u8]) -> Self { + let (value, data) = data.split_at(8); + #[cfg(target_endian = "little")] + return Self::Long(i64::from_be_bytes([ + value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], + ])); + #[cfg(target_endian = "big")] + return Self::Long(i64::from_le_bytes([ + value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], + ])); + } + #[inline(always)] + pub fn read_float(data: &mut [u8]) -> Self { + let (value, data) = data.split_at(4); + #[cfg(target_endian = "little")] + return Self::Float(f32::from_be_bytes([value[0], value[1], value[2], value[3]])); + #[cfg(target_endian = "big")] + return Self::Float(f32::from_le_bytes([value[0], value[1], value[2], value[3]])); + } + #[inline(always)] + pub fn read_double(data: &mut [u8]) -> Self { + let (value, data) = data.split_at(8); + #[cfg(target_endian = "little")] + return Self::Double(f64::from_be_bytes([ + value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], + ])); + #[cfg(target_endian = "big")] + return Self::Double(f64::from_le_bytes([ + value[0], value[1], value[2], value[3], value[4], value[5], value[6], value[7], + ])); + } + #[inline(always)] + pub fn read_string(data: &'value mut [u8]) -> Self { + let (length, data) = data.split_at(2); + let length = u16::from_le_bytes([length[0], length[1]]); + let (value, data) = data.split_at(length as usize); + Self::String(std::str::from_utf8(value).unwrap()) + } + pub fn read_list(data: &mut [u8]) -> Self { + // 先读 name, 在读 内容类型 + let (name, data) = data.split_at(2); + let name = u16::from_le_bytes([name[0], name[1]]); + let (name, data) = data.split_at(name as usize); + let name = std::str::from_utf8(name).unwrap(); + let (length, data) = data.split_at(4); + + } +}