好活,直接啥激进的东西都堆进来
This commit is contained in:
parent
397dd71e03
commit
6ec8c678d3
@ -20,18 +20,109 @@ impl<'data> RawData<'data> {
|
|||||||
pub fn new(raw_data: &'data [u8], length: usize) -> Self { Self { raw_data, length } }
|
pub fn new(raw_data: &'data [u8], length: usize) -> Self { Self { raw_data, length } }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub enum Value<'value> {
|
pub enum Value<'value> {
|
||||||
Byte(RawData<'value>),
|
Byte(i8),
|
||||||
Short(RawData<'value>),
|
Short(i16),
|
||||||
Int(RawData<'value>),
|
Int(i32),
|
||||||
Long(RawData<'value>),
|
Long(i64),
|
||||||
Float(RawData<'value>),
|
Float(f32),
|
||||||
Double(RawData<'value>),
|
Double(f64),
|
||||||
String(RawData<'value>),
|
String(&'value str),
|
||||||
ByteArray(RawData<'value>),
|
ByteArray(RawData<'value>),
|
||||||
IntArray(RawData<'value>),
|
IntArray(RawData<'value>),
|
||||||
LongArray(RawData<'value>),
|
LongArray(RawData<'value>),
|
||||||
List(Vec<Value<'value>>),
|
List(Vec<Value<'value>>),
|
||||||
Compound(HashMap<&'value str, Value<'value>>),
|
Compound(HashMap<&'value str, Value<'value>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
pub enum ListContent<'value> {
|
||||||
|
ByteList(Vec<i8>),
|
||||||
|
ShortList(Vec<i16>),
|
||||||
|
IntList(Vec<i32>),
|
||||||
|
LongList(Vec<i64>),
|
||||||
|
FloatList(Vec<f32>),
|
||||||
|
DoubleList(Vec<f64>),
|
||||||
|
StringList(Vec<&'value str>),
|
||||||
|
ByteArrayList(Vec<Vec<i8>>),
|
||||||
|
IntArrayList(Vec<Vec<i32>>),
|
||||||
|
LongArrayList(Vec<Vec<i64>>),
|
||||||
|
CompoundList(Vec<HashMap<String,Value<'value>>>),
|
||||||
|
ListList(Vec<ListContent<'value>>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user