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

Add remaining is_method_name and as_method_name in NBT Value #595

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all 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
132 changes: 132 additions & 0 deletions crates/valence_nbt/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,93 @@ 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)*);
as_number!(as_i64, i64, $($deref)*);
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<S>> {
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<S>> {
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<bool> {
self.as_i8().map(|v| v != 0)
Expand Down Expand Up @@ -209,6 +289,32 @@ impl_value!(ValueRef, 'a, (**), &'a);
impl_value!(ValueMut, 'a, (**), &'a mut);

impl<S> Value<S> {
/// Returns the mutable `Vec<i8>` representation of this value if it exists.
pub fn as_i8_array_mut(&mut self) -> Option<&mut Vec<i8>> {
match self {
Self::ByteArray(v) => Some(v),
_ => None,
}
}

/// Returns the mutable `Vec<i32>` representation of this value if it
/// exists.
pub fn as_i32_array_mut(&mut self) -> Option<&mut Vec<i32>> {
match self {
Self::IntArray(v) => Some(v),
_ => None,
}
}

/// Returns the mutable `Vec<i64>` representation of this value if it
/// exists.
pub fn as_i64_array_mut(&mut self) -> Option<&mut Vec<i64>> {
match self {
Self::LongArray(v) => Some(v),
_ => None,
}
}

/// Converts a reference to a value to a [`ValueRef`].
pub fn as_value_ref(&self) -> ValueRef<S> {
match self {
Expand Down Expand Up @@ -273,6 +379,32 @@ impl<S> ValueMut<'_, S>
where
S: Clone,
{
/// Returns the mutable `Vec<i8>` representation of this value if it exists.
pub fn as_i8_array_mut(&mut self) -> Option<&mut Vec<i8>> {
match self {
Self::ByteArray(v) => Some(v),
_ => None,
}
}

/// Returns the mutable `Vec<i32>` representation of this value if it
/// exists.
pub fn as_i32_array_mut(&mut self) -> Option<&mut Vec<i32>> {
match self {
Self::IntArray(v) => Some(v),
_ => None,
}
}

/// Returns the mutable `Vec<i64>` representation of this value if it
/// exists.
pub fn as_i64_array_mut(&mut self) -> Option<&mut Vec<i64>> {
match self {
Self::LongArray(v) => Some(v),
_ => None,
}
}

/// Clones this mutable value reference to a new owned [`Value`].
pub fn to_value(&self) -> Value<S> {
match self {
Expand Down
Loading