Struct fastnbt::stream::Parser

source ·
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§

source§

impl<R: Read> Parser<R>

source

pub fn new(reader: R) -> Self

Create new parser for the given reader.

source

pub fn next(&mut self) -> Result<Value>

Parse the next value from the input.

Auto Trait Implementations§

§

impl<R> RefUnwindSafe for Parser<R>where R: RefUnwindSafe,

§

impl<R> Send for Parser<R>where R: Send,

§

impl<R> Sync for Parser<R>where R: Sync,

§

impl<R> Unpin for Parser<R>where R: Unpin,

§

impl<R> UnwindSafe for Parser<R>where R: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.