1
0
Fork 0
mirror of https://github.com/azalea-rs/simdnbt.git synced 2025-08-02 15:36:03 +00:00

pub use CompoundTag and ListTag

This commit is contained in:
mat 2023-09-21 18:20:57 -05:00
parent a161b2d0af
commit 872ac20e5d
3 changed files with 23 additions and 17 deletions

View file

@ -1,7 +1,7 @@
//! The borrowed variant of NBT. This is useful if you're only reading data and you can keep a reference to the original buffer.
pub mod compound;
pub mod list;
mod compound;
mod list;
use std::{io::Cursor, ops::Deref};
@ -17,7 +17,7 @@ use crate::{
Error, Mutf8Str,
};
use self::{compound::CompoundTag, list::ListTag};
pub use self::{compound::CompoundTag, list::ListTag};
/// A complete NBT container. This contains a name and a compound tag.
#[derive(Debug, PartialEq)]

View file

@ -13,7 +13,7 @@ pub struct Mutf8Str {
pub(crate) slice: [u8],
}
/// An owned M-UTF8 string.
#[derive(Debug, Eq, PartialEq, Clone)]
#[derive(Debug, Eq, PartialEq, Clone, Default)]
pub struct Mutf8String {
vec: Vec<u8>,
}

View file

@ -1,7 +1,7 @@
//! The owned variant of NBT. This is useful if you're writing data from scratch or if you can't keep a reference to the original data.
pub mod compound;
pub mod list;
mod compound;
mod list;
use std::{io::Cursor, ops::Deref};
@ -17,10 +17,10 @@ use crate::{
Error, Mutf8Str,
};
use self::{compound::CompoundTag, list::ListTag};
pub use self::{compound::CompoundTag, list::ListTag};
/// A complete NBT container. This contains a name and a compound tag.
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Nbt {
name: Mutf8String,
tag: CompoundTag,
@ -33,6 +33,10 @@ pub enum OptionalNbt {
}
impl OptionalNbt {
pub fn new(name: Mutf8String, tag: CompoundTag) -> Self {
Self::Some(Nbt { name, tag })
}
/// Reads NBT from the given data. Returns `Ok(None)` if there is no data.
pub fn read(data: &mut Cursor<&[u8]>) -> Result<OptionalNbt, Error> {
let root_type = data.read_u8().map_err(|_| Error::UnexpectedEof)?;
@ -77,10 +81,21 @@ impl OptionalNbt {
}
impl Nbt {
pub fn new(name: Mutf8String, tag: CompoundTag) -> Self {
Self { name, tag }
}
/// Get the name of the NBT compound. This is often an empty string.
pub fn name(&self) -> &Mutf8Str {
&self.name
}
/// Writes the NBT to the given buffer.
pub fn write(&self, data: &mut Vec<u8>) {
data.push(COMPOUND_ID);
write_string(data, &self.name);
self.tag.write(data);
}
}
impl Deref for Nbt {
type Target = CompoundTag;
@ -90,15 +105,6 @@ impl Deref for Nbt {
}
}
impl Nbt {
/// Writes the NBT to the given buffer.
pub fn write(&self, data: &mut Vec<u8>) {
data.push(COMPOUND_ID);
write_string(data, &self.name);
self.tag.write(data);
}
}
/// A single NBT tag.
#[repr(u8)]
#[derive(Debug, Clone, PartialEq)]