Enum quartz_nbt::NbtTag

source ·
pub enum NbtTag {
    Byte(i8),
    Short(i16),
    Int(i32),
    Long(i64),
    Float(f32),
    Double(f64),
    ByteArray(Vec<i8>),
    String(String),
    List(NbtList),
    Compound(NbtCompound),
    IntArray(Vec<i32>),
    LongArray(Vec<i64>),
}
Expand description

The generic NBT tag type, containing all supported tag variants which wrap around a corresponding rust type.

This type will implement both Serialize and Deserialize when the serde feature is enabled, however this type should still be read and written with the utilities in the io module when possible if speed is the main priority. When linking into the serde ecosystem, we ensured that all tag types would have their data inlined into the resulting NBT output of our Serializer. Because of this, NBT tags are only compatible with self-describing formats, and also have slower deserialization implementations due to this restriction.

Variants§

§

Byte(i8)

A signed, one-byte integer.

§

Short(i16)

A signed, two-byte integer.

§

Int(i32)

A signed, four-byte integer.

§

Long(i64)

A signed, eight-byte integer.

§

Float(f32)

A 32-bit floating point value.

§

Double(f64)

A 64-bit floating point value.

§

ByteArray(Vec<i8>)

An array (vec) of one-byte integers. Minecraft treats this as an array of signed bytes.

§

String(String)

A UTF-8 string.

§

List(NbtList)

An NBT tag list.

§

Compound(NbtCompound)

An NBT tag compound.

§

IntArray(Vec<i32>)

An array (vec) of signed, four-byte integers.

§

LongArray(Vec<i64>)

An array (vec) of signed, eight-byte integers.

Implementations§

source§

impl NbtTag

source

pub fn type_specifier(&self) -> Option<&'static str>

Returns the single character denoting this tag’s type, or None if this tag has no type specifier.

§Examples
assert_eq!(NbtTag::Long(10).type_specifier(), Some("L"));
assert_eq!(NbtTag::IntArray(Vec::new()).type_specifier(), Some("I"));
assert_eq!(NbtTag::String(String::new()).type_specifier(), None);
source

pub fn to_snbt(&self) -> String

Converts this NBT tag into a valid, parsable SNBT string with no extraneous spacing. This method should not be used to generate user-facing text, rather to_pretty_snbt should be used instead. If finer control over the output is desired, then the tag can be formatted via the standard library’s format! macro to pass additional formatting parameters.

§Examples

Simple primitive conversion:

assert_eq!(NbtTag::Byte(5).to_snbt(), "5B");
assert_eq!(NbtTag::String("\"Quoted text\"".to_owned()).to_snbt(), "'\"Quoted text\"'");

More complex tag conversion:

let mut compound = NbtCompound::new();
compound.insert("foo", vec![-1_i64, -3_i64, -5_i64]);
assert_eq!(NbtTag::Compound(compound).to_snbt(), "{foo:[L;-1,-3,-5]}");
source

pub fn to_pretty_snbt(&self) -> String

Converts this NBT tag into a valid, parsable SNBT string with extra spacing for readability. If a more compact SNBT representation is desired, then use to_snbt. If finer control over the output is desired, then the tag can be formatted via the standard library’s format! macro to pass additional formatting parameters.

§Examples

Simple primitive conversion:

assert_eq!(NbtTag::Byte(5).to_pretty_snbt(), "5B");
assert_eq!(
    NbtTag::String("\"Quoted text\"".to_owned()).to_pretty_snbt(),
    "'\"Quoted text\"'"
);

More complex tag conversion:

let mut compound = NbtCompound::new();
compound.insert("foo", vec![-1_i64, -3_i64, -5_i64]);
let repr =
r#"{
    foo: [
        L;
        -1,
        -3,
        -5
    ]
}"#;
assert_eq!(NbtTag::Compound(compound).to_pretty_snbt(), repr);
source

pub fn should_quote(string: &str) -> bool

Returns whether or not the given string needs to be quoted due to non-alphanumeric or otherwise non-standard characters.

source

pub fn string_to_snbt(string: &str) -> Cow<'_, str>

Wraps the given string in quotes and escapes any quotes contained in the original string.

§Examples
use std::borrow::Cow;

assert_eq!(NbtTag::string_to_snbt("string"), Cow::Borrowed("string"));
assert_eq!(
    NbtTag::string_to_snbt("\\\n\r\t'\""),
    Cow::<str>::Owned(String::from(r#"'\\\n\r\t\'"'"#))
);

Trait Implementations§

source§

impl Clone for NbtTag

source§

fn clone(&self) -> NbtTag

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for NbtTag

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for NbtTag

source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for NbtTag

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Extend<NbtTag> for NbtList

source§

fn extend<T: IntoIterator<Item = NbtTag>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl From<&String> for NbtTag

source§

fn from(value: &String) -> NbtTag

Converts to this type from the input type.
source§

impl From<&str> for NbtTag

source§

fn from(value: &str) -> NbtTag

Converts to this type from the input type.
source§

impl From<NbtCompound> for NbtTag

source§

fn from(value: NbtCompound) -> NbtTag

Converts to this type from the input type.
source§

impl From<NbtList> for NbtTag

source§

fn from(value: NbtList) -> NbtTag

Converts to this type from the input type.
source§

impl From<String> for NbtTag

source§

fn from(value: String) -> NbtTag

Converts to this type from the input type.
source§

impl<T: NbtRepr> From<T> for NbtTag

source§

fn from(x: T) -> Self

Converts to this type from the input type.
source§

impl From<Vec<i32>> for NbtTag

source§

fn from(value: Vec<i32>) -> NbtTag

Converts to this type from the input type.
source§

impl From<Vec<i64>> for NbtTag

source§

fn from(value: Vec<i64>) -> NbtTag

Converts to this type from the input type.
source§

impl From<Vec<i8>> for NbtTag

source§

fn from(value: Vec<i8>) -> NbtTag

Converts to this type from the input type.
source§

impl From<Vec<u8>> for NbtTag

source§

fn from(value: Vec<u8>) -> Self

Converts to this type from the input type.
source§

impl From<bool> for NbtTag

source§

fn from(value: bool) -> NbtTag

Converts to this type from the input type.
source§

impl From<f32> for NbtTag

source§

fn from(value: f32) -> NbtTag

Converts to this type from the input type.
source§

impl From<f64> for NbtTag

source§

fn from(value: f64) -> NbtTag

Converts to this type from the input type.
source§

impl From<i16> for NbtTag

source§

fn from(value: i16) -> NbtTag

Converts to this type from the input type.
source§

impl From<i32> for NbtTag

source§

fn from(value: i32) -> NbtTag

Converts to this type from the input type.
source§

impl From<i64> for NbtTag

source§

fn from(value: i64) -> NbtTag

Converts to this type from the input type.
source§

impl From<i8> for NbtTag

source§

fn from(value: i8) -> NbtTag

Converts to this type from the input type.
source§

impl From<u8> for NbtTag

source§

fn from(value: u8) -> Self

Converts to this type from the input type.
source§

impl FromIterator<NbtTag> for NbtList

source§

fn from_iter<T: IntoIterator<Item = NbtTag>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl PartialEq for NbtTag

source§

fn eq(&self, other: &NbtTag) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for NbtTag

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<'a> TryFrom<&'a NbtTag> for &'a [i32]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a [i64]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a [i8]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a [u8]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a NbtCompound

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a NbtList

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a String

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a Vec<i32>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a Vec<i64>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a Vec<i8>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a f32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a f64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a i16

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a i32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a i64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a i8

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a str

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a NbtTag> for &'a u8

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for bool

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for f32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for f64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for i16

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for i32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for i64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for i8

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<&NbtTag> for u8

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut [i32]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut [i64]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut [i8]

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut NbtCompound

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut NbtList

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut String

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut Vec<i32>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut Vec<i64>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut Vec<i8>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut f32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut f64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut i16

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut i32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut i64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut i8

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl<'a> TryFrom<&'a mut NbtTag> for &'a mut str

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: &'a mut NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for NbtCompound

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for NbtList

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for String

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for Vec<i32>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for Vec<i64>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for Vec<i8>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for Vec<u8>

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for f32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for f64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for i16

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for i32

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for i64

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl TryFrom<NbtTag> for i8

§

type Error = NbtStructureError

The type returned in the event of a conversion error.
source§

fn try_from(tag: NbtTag) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl StructuralPartialEq for NbtTag

Auto Trait Implementations§

§

impl Freeze for NbtTag

§

impl RefUnwindSafe for NbtTag

§

impl Send for NbtTag

§

impl Sync for NbtTag

§

impl Unpin for NbtTag

§

impl UnwindSafe for NbtTag

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,