Re-visit From impl for native data types found in NBT data format. (#169)

This commit is contained in:
Terminator 2022-12-16 03:38:52 +01:00 committed by GitHub
parent 740778ec41
commit c73abacc98
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 123 deletions

View file

@ -36,7 +36,7 @@ thiserror = "1.0.35"
tracing = "0.1.37"
url = { version = "2.2.2", features = ["serde"] }
uuid = { version = "1.1.2", features = ["serde"] }
valence_nbt = { version = "0.4.0", path = "valence_nbt" }
valence_nbt = { version = "0.5.0", path = "valence_nbt" }
valence_protocol = { version = "0.1.0", path = "valence_protocol", features = ["encryption", "compression"] }
vek = "0.15.8"

View file

@ -6,7 +6,7 @@ repository = "https://github.com/valence-rs/valence/tree/main/valence_nbt"
readme = "README.md"
license = "MIT"
keywords = ["nbt", "minecraft", "serialization"]
version = "0.4.0"
version = "0.5.0"
authors = ["Ryan Johnson <ryanj00a@gmail.com>"]
edition = "2021"

View file

@ -72,6 +72,56 @@ impl List {
}
}
/// We can not create new identities in stable Rust using macros, so we provide
/// them in the macro invocation itself.
macro_rules! nbt_conversion {
( $($nbt_type:ident = $value_type:ty => $is_function:ident $as_function:ident $as_mut_function:ident $into_function:ident)+ ) => {
$(
pub fn $is_function(&self) -> bool {
self.$as_function().is_some()
}
pub fn $as_function(&self) -> Option<&$value_type> {
match self {
Self::$nbt_type(value) => Some(value),
_ => None
}
}
pub fn $as_mut_function(&mut self) -> Option<&mut $value_type> {
match self {
Self::$nbt_type(value) => Some(value),
_ => None
}
}
pub fn $into_function(self) -> Option<$value_type> {
match self {
Self::$nbt_type(value) => Some(value),
_ => None
}
}
)*
};
}
impl Value {
nbt_conversion! {
Byte = i8 => is_byte as_byte as_byte_mut into_byte
Short = i16 => is_short as_short as_short_mut into_short
Int = i32 => is_int as_int as_int_mut into_int
Long = i64 => is_long as_long as_long_mut into_long
Float = f32 => is_float as_float as_float_mut into_float
Double = f64 => is_double as_double as_double_mut into_double
ByteArray = Vec<i8> => is_byte_array as_byte_array as_byte_array_mut into_byte_array
String = String => is_string as_string as_string_mut into_string
List = List => is_list as_list as_list_mut into_list
Compound = Compound => is_compound as_compound as_compound_mut into_compound
IntArray = Vec<i32> => is_int_array as_int_array as_int_array_mut into_int_array
LongArray = Vec<i64> => is_long_array as_long_array as_long_array_mut into_long_array
}
}
impl From<i8> for Value {
fn from(v: i8) -> Self {
Self::Byte(v)
@ -234,123 +284,3 @@ impl From<Vec<Vec<i64>>> for List {
List::LongArray(v)
}
}
impl From<Value> for Option<i8> {
fn from(value: Value) -> Self {
if let Value::Byte(b) = value {
Some(b)
} else {
None
}
}
}
impl From<Value> for Option<i16> {
fn from(value: Value) -> Self {
if let Value::Short(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<i32> {
fn from(value: Value) -> Self {
if let Value::Int(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<i64> {
fn from(value: Value) -> Self {
if let Value::Long(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<f32> {
fn from(value: Value) -> Self {
if let Value::Float(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<f64> {
fn from(value: Value) -> Self {
if let Value::Double(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Vec<i8>> {
fn from(value: Value) -> Self {
if let Value::ByteArray(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<String> {
fn from(value: Value) -> Self {
if let Value::String(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<List> {
fn from(value: Value) -> Self {
if let Value::List(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Compound> {
fn from(value: Value) -> Self {
if let Value::Compound(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Vec<i32>> {
fn from(value: Value) -> Self {
if let Value::IntArray(val) = value {
Some(val)
} else {
None
}
}
}
impl From<Value> for Option<Vec<i64>> {
fn from(value: Value) -> Self {
if let Value::LongArray(val) = value {
Some(val)
} else {
None
}
}
}

View file

@ -18,7 +18,7 @@ serde_json = "1.0.87"
thiserror = "1.0.37"
uuid = "1.2.1"
valence_derive = { version = "0.1.0", path = "../valence_derive" }
valence_nbt = { version = "0.4.0", path = "../valence_nbt" }
valence_nbt = { version = "0.5.0", path = "../valence_nbt" }
[dev-dependencies]
rand = "0.8.5"