diff --git a/crates/valence_nbt/src/value.rs b/crates/valence_nbt/src/value.rs index 4cb30661e..d97a04195 100644 --- a/crates/valence_nbt/src/value.rs +++ b/crates/valence_nbt/src/value.rs @@ -122,6 +122,38 @@ macro_rules! impl_value { } } + /// Returns whether this value is a array, i.e. a byte array, int array or long array. + pub fn is_array(&self) -> bool { + match self { + Self::ByteArray(_) | Self::IntArray(_) | Self::LongArray(_) => true, + _ => false, + } + } + + /// Returns whether this value is a string. + pub fn is_string(&self) -> bool { + match self { + Self::String(_) => true, + _ => false, + } + } + + /// Returns whether this value is a list of values, i.e. a compound list, a string list and so on. + pub fn is_list(&self) -> bool { + match self { + Self::List(_) => true, + _ => false, + } + } + + /// Returns whether this value is a compound. + pub fn is_compound(&self) -> bool { + match self { + Self::Compound(_) => true, + _ => false, + } + } + as_number!(as_i8, i8, $($deref)*); as_number!(as_i16, i16, $($deref)*); as_number!(as_i32, i32, $($deref)*); @@ -129,6 +161,54 @@ macro_rules! impl_value { as_number_float!(as_f32, f32, $($deref)*); as_number_float!(as_f64, f64, $($deref)*); + /// Returns the `[i8]` representation of this value if it exists. + pub fn as_i8_array(&self) -> Option<&[i8]> { + match self { + Self::ByteArray(v) => Some(v), + _ => None, + } + } + + /// Returns the `[i32]` representation of this value if it exists. + pub fn as_i32_array(&self) -> Option<&[i32]> { + match self { + Self::IntArray(v) => Some(v), + _ => None, + } + } + + /// Returns the `[i64]` representation of this value if it exists. + pub fn as_i64_array(&self) -> Option<&[i64]> { + match self { + Self::LongArray(v) => Some(v), + _ => None, + } + } + + /// Returns the `String` representation of this value if it exists. + pub fn as_string(&$($lifetime)* self) -> Option<&$($lifetime)* S> { + match self { + Self::String(v) => Some(v), + _ => None, + } + } + + /// Returns the `List` representation of this value if it exists. + pub fn as_list(&$($lifetime)* self) -> Option<&$($lifetime)* List> { + match self { + Self::List(v) => Some(v), + _ => None, + } + } + + /// Returns the `Compound` representation of this value if it exists. + pub fn as_compound(&$($lifetime)* self) -> Option<&$($lifetime)* Compound> { + match self { + Self::Compound(v) => Some(v), + _ => None, + } + } + /// If this value is a number, returns the `bool` representation of this value. pub fn as_bool(&self) -> Option { self.as_i8().map(|v| v != 0) @@ -209,6 +289,32 @@ impl_value!(ValueRef, 'a, (**), &'a); impl_value!(ValueMut, 'a, (**), &'a mut); impl Value { + /// Returns the mutable `Vec` representation of this value if it exists. + pub fn as_i8_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::ByteArray(v) => Some(v), + _ => None, + } + } + + /// Returns the mutable `Vec` representation of this value if it + /// exists. + pub fn as_i32_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::IntArray(v) => Some(v), + _ => None, + } + } + + /// Returns the mutable `Vec` representation of this value if it + /// exists. + pub fn as_i64_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::LongArray(v) => Some(v), + _ => None, + } + } + /// Converts a reference to a value to a [`ValueRef`]. pub fn as_value_ref(&self) -> ValueRef { match self { @@ -273,6 +379,32 @@ impl ValueMut<'_, S> where S: Clone, { + /// Returns the mutable `Vec` representation of this value if it exists. + pub fn as_i8_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::ByteArray(v) => Some(v), + _ => None, + } + } + + /// Returns the mutable `Vec` representation of this value if it + /// exists. + pub fn as_i32_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::IntArray(v) => Some(v), + _ => None, + } + } + + /// Returns the mutable `Vec` representation of this value if it + /// exists. + pub fn as_i64_array_mut(&mut self) -> Option<&mut Vec> { + match self { + Self::LongArray(v) => Some(v), + _ => None, + } + } + /// Clones this mutable value reference to a new owned [`Value`]. pub fn to_value(&self) -> Value { match self {