Struct fastnbt::stream::Parser [−][src]
pub struct Parser<R: Read> { /* fields omitted */ }
Expand description
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
Auto Trait Implementations
impl<R> RefUnwindSafe for Parser<R> where
R: RefUnwindSafe,
impl<R> UnwindSafe for Parser<R> where
R: UnwindSafe,