[][src]Struct fastnbt::Parser

pub struct Parser<R: Read> { /* fields omitted */ }

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::{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.

This example is not tested
use fastnbt::{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]

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

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.