Function quartz_nbt::snbt::parse

source ·
pub fn parse<T: AsRef<str> + ?Sized>(
    string_nbt: &T
) -> Result<NbtCompound, SnbtError>
Expand description

Parses the given string into an NBT tag compound.

§Examples

use quartz_nbt::snbt;

let mut compound = NbtCompound::new();
compound.insert("short", -10i16);
compound.insert("string", "fizzbuzz");
compound.insert("array", vec![1i64, 1, 2, 3, 5]);

const SNBT: &str = "{short: -10s, string: fizzbuzz, array: [L; 1, 1, 2, 3, 5]}";

assert_eq!(compound, snbt::parse(SNBT).unwrap());

The parser will immediately quit when it encounters a syntax error. Displaying these errors will provide useful information about where the error occurred, what went wrong, and what was expected.

use quartz_nbt::snbt;

const ERRONEOUS_SNBT: &str = "{garbage:; -'bleh ]";
let result = snbt::parse(ERRONEOUS_SNBT);
assert!(result.is_err());
assert_eq!(
    result.unwrap_err().to_string(),
    "Unexpected token at column 9 near '{garbage:;', expected value"
);