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
impl NbtTag
sourcepub fn type_specifier(&self) -> Option<&'static str>
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);
sourcepub fn to_snbt(&self) -> String
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]}");
sourcepub fn to_pretty_snbt(&self) -> String
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);
sourcepub fn should_quote(string: &str) -> bool
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.
sourcepub fn string_to_snbt(string: &str) -> Cow<'_, str>
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<'de> Deserialize<'de> for NbtTag
impl<'de> Deserialize<'de> for NbtTag
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
source§impl Extend<NbtTag> for NbtList
impl Extend<NbtTag> for NbtList
source§fn extend<T: IntoIterator<Item = NbtTag>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = NbtTag>>(&mut self, iter: T)
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)