Expand description
Convert a T
into fastnbt::Value
which is an enum that can represent
any valid NBT data.
Example
use serde::Serialize;
use fastnbt::nbt;
use std::error::Error;
#[derive(Serialize)]
struct User {
fingerprint: String,
location: String,
}
fn compare_nbt_values() -> Result<(), Box<dyn Error>> {
let u = User {
fingerprint: "0xF9BA143B95FF6D82".to_owned(),
location: "Menlo Park, CA".to_owned(),
};
// The type of `expected` is `fastnbt::Value`
let expected = nbt!({
"fingerprint": "0xF9BA143B95FF6D82",
"location": "Menlo Park, CA",
});
let v = fastnbt::to_value(u).unwrap();
assert_eq!(v, expected);
Ok(())
}
Errors
This conversion can fail if T
’s implementation of Serialize
decides to
fail, or if T
contains a map with non-string keys.
use std::collections::BTreeMap;
// The keys in this map are vectors, not strings.
let mut map = BTreeMap::new();
map.insert(vec![32, 64], "x86");
println!("{}", fastnbt::to_value(map).unwrap_err());