pub struct Parser<R: Read> { /* private fields */ }
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

Create new parser for the given reader.

Parse the next value from the input.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.