diff --git a/shen-nbt5/src/lib.rs b/shen-nbt5/src/lib.rs index fda9dd8..81b95cd 100644 --- a/shen-nbt5/src/lib.rs +++ b/shen-nbt5/src/lib.rs @@ -120,11 +120,11 @@ pub enum NbtError { /// 未知错误 UnknownErr(String), /// 根节点类型错误 - WrongRootType(u8), + WrongRootType(NbtTypeId), /// 根节点无名称 RootWithoutName, /// 未知类型 - UnknownType(u8), + UnknownType(NbtTypeId), /// 名称读取错误 NameRead(String), /// 指针超出范围 @@ -141,6 +141,8 @@ pub enum NbtError { VarlongTooBig(usize), /// NbtList 中类型不同 ListTypeNotSame(Vec), + /// 错误类型 + IncorrectType(NbtTypeId, NbtTypeId), } pub type NbtResult = std::result::Result; @@ -186,6 +188,9 @@ impl std::fmt::Display for NbtError { NbtError::ListTypeNotSame(types) => { write!(f, "NbtList 中类型不同: {:?} 应相同", types) } + NbtError::IncorrectType(expect, got) => { + write!(f, "错误类型: 期望: {}, 实际: {}", expect, got) + } } } } @@ -274,4 +279,114 @@ impl NbtValue { { W::to_bytes(self) } + + #[inline] + pub fn as_i18(&self) -> NbtResult { + match self { + NbtValue::Byte(v) => Ok(*v), + _ => Err(NbtError::IncorrectType(1_u8, self.tag())), + } + } + #[inline] + pub fn as_i16(&self) -> NbtResult { + match self { + NbtValue::Short(v) => Ok(*v), + _ => Err(NbtError::IncorrectType(2_u8, self.tag())), + } + } + #[inline] + pub fn as_i32(&self) -> NbtResult { + match self { + NbtValue::Int(v) => Ok(*v), + _ => Err(NbtError::IncorrectType(3_u8, self.tag())), + } + } + #[inline] + pub fn as_i64(&self) -> NbtResult { + match self { + NbtValue::Long(v) => Ok(*v), + _ => Err(NbtError::IncorrectType(4_u8, self.tag())), + } + } + #[inline] + pub fn as_f32(&self) -> NbtResult { + match self { + NbtValue::Float(v) => Ok(*v), + _ => Err(NbtError::IncorrectType(5_u8, self.tag())), + } + } + #[inline] + pub fn as_f64(&self) -> NbtResult { + match self { + NbtValue::Double(v) => Ok(*v), + _ => Err(NbtError::IncorrectType(6_u8, self.tag())), + } + } + #[inline] + pub fn as_i8_array(&self) -> NbtResult> { + match self { + NbtValue::ByteArray(v) => Ok(v.clone()), + _ => Err(NbtError::IncorrectType(7_u8, self.tag())), + } + } + #[inline] + pub fn as_i32_array(&self) -> NbtResult> { + match self { + NbtValue::IntArray(v) => Ok(v.clone()), + _ => Err(NbtError::IncorrectType(11_u8, self.tag())), + } + } + #[inline] + pub fn as_i64_array(&self) -> NbtResult> { + match self { + NbtValue::LongArray(v) => Ok(v.clone()), + _ => Err(NbtError::IncorrectType(12_u8, self.tag())), + } + } + #[inline] + pub fn as_string(&self) -> NbtResult { + match self { + NbtValue::String(v) => Ok(v.clone()), + _ => Err(NbtError::IncorrectType(8_u8, self.tag())), + } + } + #[inline] + pub fn as_list(&self) -> NbtResult> { + match self { + NbtValue::List(v) => Ok(v.clone()), + _ => Err(NbtError::IncorrectType(9_u8, self.tag())), + } + } + #[inline] + pub fn as_compound(&self) -> NbtResult<(Option<&String>, Vec<(String, NbtValue)>)> { + match self { + NbtValue::Compound(name, v) => Ok((name.as_ref(), v.clone())), + _ => Err(NbtError::IncorrectType(10_u8, self.tag())), + } + } + + #[inline] + pub fn is_i8(&self) -> bool { matches!(self, NbtValue::Byte(_)) } + #[inline] + pub fn is_i16(&self) -> bool { matches!(self, NbtValue::Short(_)) } + #[inline] + pub fn is_i32(&self) -> bool { matches!(self, NbtValue::Int(_)) } + #[inline] + pub fn is_i64(&self) -> bool { matches!(self, NbtValue::Long(_)) } + #[inline] + pub fn is_f32(&self) -> bool { matches!(self, NbtValue::Float(_)) } + #[inline] + pub fn is_f64(&self) -> bool { matches!(self, NbtValue::Double(_)) } + #[inline] + pub fn is_i8_array(&self) -> bool { matches!(self, NbtValue::ByteArray(_)) } + #[inline] + pub fn is_i32_array(&self) -> bool { matches!(self, NbtValue::IntArray(_)) } + #[inline] + pub fn is_i64_array(&self) -> bool { matches!(self, NbtValue::LongArray(_)) } + #[inline] + pub fn is_string(&self) -> bool { matches!(self, NbtValue::String(_)) } + #[inline] + pub fn is_list(&self) -> bool { matches!(self, NbtValue::List(_)) } + #[inline] + pub fn is_compound(&self) -> bool { matches!(self, NbtValue::Compound(_, _)) } } diff --git a/shen-nbt5/src/ser.rs b/shen-nbt5/src/ser.rs index bf0cccb..72bb78d 100644 --- a/shen-nbt5/src/ser.rs +++ b/shen-nbt5/src/ser.rs @@ -2,7 +2,7 @@ use crate::{nbt_version, NbtError, NbtResult, NbtValue}; -compile_error!("Serde support is not yet implemented, awaiting for PR"); +compile_error!("Serde support is not yet implemented, awaiting for PR https://github.com/shenjackyuanjie/nbt-rust/issues/1"); // impl serde_ser::Error for NbtError { // fn custom(msg: T) -> Self { NbtError::UnknownErr(msg.to_string()) } // } @@ -51,7 +51,7 @@ compile_error!("Serde support is not yet implemented, awaiting for PR"); // None => Ok(None), // } // } - + // } // impl<'de, 'a> serde_de::Deserializer for DeserializeNbtValue<'de> { @@ -76,7 +76,7 @@ compile_error!("Serde support is not yet implemented, awaiting for PR"); // NbtValue::Compound(name, v) => visitor.visit_map(CompoundDeserializer::new(v)), // NbtValue::IntArray(v) => visitor.visit_seq(ArrayDeserializer::new(v.iter())), // NbtValue::LongArray(v) => visitor.visit_seq(ArrayDeserializer::new(v.iter())), - + // } // } // }