Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 5 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
2 changes: 1 addition & 1 deletion valence_nbt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
edition = "2021"

Expand Down
170 changes: 50 additions & 120 deletions valence_nbt/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 $take_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 $take_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 take_byte
Short = i16 => is_short as_short as_short_mut take_short
Int = i32 => is_int as_int as_int_mut take_int
Long = i64 => is_long as_long as_long_mut take_long
Float = f32 => is_float as_float as_float_mut take_float
Double = f64 => is_double as_double as_double_mut take_double
ByteArray = Vec<i8> => is_byte_array as_byte_array as_byte_array_mut take_byte_array
String = String => is_string as_string as_string_mut take_string
List = List => is_list as_list as_list_mut take_list
Compound = Compound => is_compound as_compound as_compound_mut take_compound
IntArray = Vec<i32> => is_int_array as_int_array as_int_array_mut take_int_array
LongArray = Vec<i64> => is_long_array as_long_array as_long_array_mut take_long_array
}
}

impl From<i8> for Value {
fn from(v: i8) -> Self {
Self::Byte(v)
Expand Down Expand Up @@ -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
}
}
}
2 changes: 1 addition & 1 deletion valence_protocol/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down