From 00c012194006ec6376f6d43098b3b1b6ded90db4 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 5 Aug 2023 12:49:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8Aclippy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/data.rs | 54 ++++++++++++++++++++++++++--------------------------- src/read.rs | 24 +++++++++++++----------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/src/data.rs b/src/data.rs index 3ad6c0b..398a901 100644 --- a/src/data.rs +++ b/src/data.rs @@ -34,22 +34,22 @@ pub enum NbtList { pub enum NbtValue { /// 0x00 /// 标志着一个 NBT Compound/List 的结束 - NbtEnd, + End, /// 0x01 - NbtByte(bool), + Byte(bool), /// 0x02 - NbtShort(i16), + Short(i16), /// 0x03 - NbtInt(i32), + Int(i32), /// 0x04 - NbtLong(i64), + Long(i64), /// 0x05 - NbtFloat(f32), + Float(f32), /// 0x06 - NbtDouble(f64), + Double(f64), /// 0x08 /// 一个 UTF-8 编码的定长字符串 - NbtString(Arc), + String(Arc), } impl NbtItem { @@ -166,41 +166,41 @@ macro_rules! read_data { impl NbtValue { pub fn as_end(&self) -> Option<()> { match self { - Self::NbtEnd => Some(()), + Self::End => Some(()), _ => None, } } - export_data!(as_bool, NbtByte, bool); - export_data!(as_i16, NbtShort, i16); - export_data!(as_i32, NbtInt, i32); - export_data!(as_i64, NbtLong, i64); - export_data!(as_f32, NbtFloat, f32); - export_data!(as_f64, NbtDouble, f64); - export_data!(as_string, NbtString, Arc); + export_data!(as_bool, Byte, bool); + export_data!(as_i16, Short, i16); + export_data!(as_i32, Int, i32); + export_data!(as_i64, Long, i64); + export_data!(as_f32, Float, f32); + export_data!(as_f64, Double, f64); + export_data!(as_string, String, Arc); pub fn from_end(value: &mut Reader) -> Self { let mut buff = [0_u8]; _ = value.read(&mut buff).unwrap(); - Self::NbtEnd + Self::End } - read_data!(from_bool, NbtByte, bool, 1); - read_data!(from_i16, NbtShort, i16, 2); - read_data!(from_i32, NbtInt, i32, 4); - read_data!(from_i64, NbtLong, i64, 8); - read_data!(from_f32, NbtFloat, f32, 4); - read_data!(from_f64, NbtDouble, f64, 8); + read_data!(from_bool, Byte, bool, 1); + read_data!(from_i16, Short, i16, 2); + read_data!(from_i32, Int, i32, 4); + read_data!(from_i64, Long, i64, 8); + read_data!(from_f32, Float, f32, 4); + read_data!(from_f64, Double, f64, 8); /// 直接读取 pub fn from_string(value: &mut Reader) -> Self { let len: StringLength = Self::from_i32(value).as_i32().unwrap() as StringLength; if len == 0 { - return Self::NbtString(Arc::from("")); + return Self::String(Arc::from("")); } let mut buff = vec![0_u8; len as usize]; _ = value.read(&mut buff).unwrap(); - Self::NbtString(Arc::from(String::from_utf8(buff).unwrap())) + Self::String(Arc::from(String::from_utf8(buff).unwrap())) } /// 读取一个有类型无名称的值 (List) @@ -208,7 +208,7 @@ impl NbtValue { let mut value_type = [0_u8]; _ = value.read(&mut value_type).unwrap(); match value_type { - [0x00] => Some(Self::NbtEnd), + [0x00] => Some(Self::End), [0x01] => Some(Self::from_bool(value)), [0x02] => Some(Self::from_i16(value)), [0x03] => Some(Self::from_i32(value)), @@ -228,7 +228,7 @@ impl NbtValue { let mut value_type = [0_u8]; _ = value.read(&mut value_type).unwrap(); match value_type { - [0x00] => Some((Self::NbtEnd, Arc::from(""))), + [0x00] => Some((Self::End, Arc::from(""))), [0x01] => { let name = Self::from_string(value).as_string().unwrap(); Some((Self::from_bool(value), name)) diff --git a/src/read.rs b/src/read.rs index e5ae679..e1a5c6a 100644 --- a/src/read.rs +++ b/src/read.rs @@ -10,7 +10,7 @@ use std::io::{Cursor, Read}; /// (0x0A) Compound /// (0x0B) Vec /// (0x0C) Vec -pub mod read { +pub mod read_data { use crate::data::{NbtItem, NbtLength, NbtList, NbtValue, Reader}; use std::collections::HashMap; use std::io::Read; @@ -163,13 +163,13 @@ pub mod read { pub fn from_compound(value: &mut Reader) -> NbtList { // 进来直接是 values // loop 读取长度 name len name value value - // 直到一个 NbtEnd + // 直到一个 End let mut map: HashMap, NbtItem> = HashMap::new(); loop { let mut type_tag = [0_u8; 1]; _ = value.read(&mut type_tag).unwrap(); if type_tag == [0x00] { - // NbtEnd + // End break; } // 读取 name @@ -208,8 +208,10 @@ pub mod read { } } +use read_data::{from_bool_array, from_compound, from_i32_array, from_i64_array, from_nbt_list}; + pub enum NbtStatus { - /// 读取到了 NbtEnd + /// 读取到了 End End, /// 继续中 Going(NbtItem), @@ -239,12 +241,12 @@ impl TryFrom> for NbtItem { [0x04] => NbtStatus::Going(NbtItem::Value(NbtValue::from_i64(&mut value))), [0x05] => NbtStatus::Going(NbtItem::Value(NbtValue::from_f32(&mut value))), [0x06] => NbtStatus::Going(NbtItem::Value(NbtValue::from_f64(&mut value))), - [0x07] => NbtStatus::Going(NbtItem::from(read::from_bool_array(&mut value))), + [0x07] => NbtStatus::Going(NbtItem::from(from_bool_array(&mut value))), [0x08] => NbtStatus::Going(NbtItem::Value(NbtValue::from_string(&mut value))), - [0x09] => NbtStatus::Going(NbtItem::from(read::from_nbt_list(&mut value))), - [0x0A] => NbtStatus::Going(NbtItem::from(read::from_compound(&mut value))), - [0x0B] => NbtStatus::Going(NbtItem::from(read::from_i32_array(&mut value))), - [0x0C] => NbtStatus::Going(NbtItem::from(read::from_i64_array(&mut value))), + [0x09] => NbtStatus::Going(NbtItem::from(from_nbt_list(&mut value))), + [0x0A] => NbtStatus::Going(NbtItem::from(from_compound(&mut value))), + [0x0B] => NbtStatus::Going(NbtItem::from(from_i32_array(&mut value))), + [0x0C] => NbtStatus::Going(NbtItem::from(from_i64_array(&mut value))), _ => NbtStatus::Error(std::io::Error::new( std::io::ErrorKind::InvalidData, format!( @@ -267,10 +269,10 @@ impl TryFrom> for NbtItem { } } // 理论上 长度应该为 2 - return if items.len() >= 3 { + if items.len() >= 3 { Ok(NbtItem::Array(NbtList::from(items))) } else { Ok(items[0].clone()) - }; + } } }