Struct fastnbt::stream::Parser [−][src]
Parser can take any reader and parse it as NBT data. Does not do decompression.
Examples
Dump NBT
The following takes a stream of GZip compressed data from stdin and dumps it out in Rust's Debug
format, with
some indentation to help see the structure.
use fastnbt::stream::{Parser, Value}; use flate2::read::GzDecoder; let stdin = std::io::stdin(); let decoder = GzDecoder::new(stdin); let mut parser = Parser::new(decoder); let mut indent = 0; loop { match parser.next() { Err(e) => { println!("{:?}", e); break; } Ok(value) => { match value { Value::CompoundEnd => indent -= 4, Value::ListEnd => indent -= 4, _ => {} } println!("{:indent$}{:?}", "", value, indent = indent); match value { Value::Compound(_) => indent += 4, Value::List(_, _, _) => indent += 4, _ => {} } } } }
Finding a heightmap
Here we assume we've parsed up until we have entered the Heightmaps
compound of the
Minecraft Anvil chunk format. We keep parsing until we find the
WORLD_SURFACE
long array. We avoid entering nested compounds by skipping them if we enter one. We know we have
finished with the current compound when we see the CompoundEnd
value.
ⓘ
use fastnbt::stream::{Parser, Value, skip_compound}; use fastanvil::expand_heightmap; let mut parser = /* ... */ loop { match parser.next()? { Value::LongArray(Some(ref name), data) if name == "WORLD_SURFACE" => { skip_compound(&mut parser)?; return Ok(Some(expand_heightmap(data.as_slice()))); } Value::Compound(_) => { // don't enter another compound. skip_compound(&mut parser)?; } Value::CompoundEnd => { // No heightmap found, it happens. return Ok(None); } // also need to not enter lists _ => {} } }
Implementations
impl<R: Read> Parser<R>
[src]
pub fn new(reader: R) -> Self
[src]
Create new parser for the given reader.
pub fn next(&mut self) -> Result<Value>
[src]
Parse the next value from the input.
Auto Trait Implementations
impl<R> RefUnwindSafe for Parser<R> where
R: RefUnwindSafe,
[src]
R: RefUnwindSafe,
impl<R> Send for Parser<R> where
R: Send,
[src]
R: Send,
impl<R> Sync for Parser<R> where
R: Sync,
[src]
R: Sync,
impl<R> Unpin for Parser<R> where
R: Unpin,
[src]
R: Unpin,
impl<R> UnwindSafe for Parser<R> where
R: UnwindSafe,
[src]
R: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,