diff --git a/Cargo.toml b/Cargo.toml index 8412a9d..4f8aa5d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/valence_nbt/Cargo.toml b/valence_nbt/Cargo.toml index 2c94557..ada39ad 100644 --- a/valence_nbt/Cargo.toml +++ b/valence_nbt/Cargo.toml @@ -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 "] edition = "2021" diff --git a/valence_nbt/src/value.rs b/valence_nbt/src/value.rs index e6be1b6..663ba9e 100644 --- a/valence_nbt/src/value.rs +++ b/valence_nbt/src/value.rs @@ -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 => 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 => is_int_array as_int_array as_int_array_mut into_int_array + LongArray = Vec => is_long_array as_long_array as_long_array_mut into_long_array + } +} + impl From for Value { fn from(v: i8) -> Self { Self::Byte(v) @@ -234,123 +284,3 @@ impl From>> for List { List::LongArray(v) } } - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Byte(b) = value { - Some(b) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Short(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Int(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Long(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Float(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Double(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option> { - fn from(value: Value) -> Self { - if let Value::ByteArray(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::String(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::List(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option { - fn from(value: Value) -> Self { - if let Value::Compound(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option> { - fn from(value: Value) -> Self { - if let Value::IntArray(val) = value { - Some(val) - } else { - None - } - } -} - -impl From for Option> { - fn from(value: Value) -> Self { - if let Value::LongArray(val) = value { - Some(val) - } else { - None - } - } -} diff --git a/valence_protocol/Cargo.toml b/valence_protocol/Cargo.toml index ccf8396..c24177c 100644 --- a/valence_protocol/Cargo.toml +++ b/valence_protocol/Cargo.toml @@ -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"