valence_nbt/
binary.rs

1//! Support for serializing and deserializing compounds in Java edition's binary
2//! format.
3//!
4//! # Examples
5//!
6//! ```
7//! use valence_nbt::{compound, to_binary, Compound, List};
8//!
9//! let c = compound! {
10//!     "byte" => 5_i8,
11//!     "string" => "hello",
12//!     "list_of_float" => List::Float(vec![
13//!         std::f32::consts::PI,
14//!         std::f32::consts::E,
15//!         1.4142
16//!     ]),
17//! };
18//!
19//! let mut buf = vec![];
20//!
21//! to_binary(&c, &mut buf, "").unwrap();
22//! ```
23//!
24//! Decode NBT data from its binary form.
25//!
26//! ```
27//! use valence_nbt::{compound, from_binary, Compound, Value};
28//!
29//! let some_bytes = [10, 0, 0, 3, 0, 3, 105, 110, 116, 0, 0, 222, 173, 0];
30//!
31//! let expected_value = compound! {
32//!     "int" => 0xdead
33//! };
34//!
35//! let (nbt, root_name) = from_binary(&mut some_bytes.as_slice()).unwrap();
36//!
37//! assert_eq!(nbt, expected_value);
38//! assert_eq!(root_name, "");
39//! ```
40
41mod decode;
42mod encode;
43mod modified_utf8;
44#[cfg(test)]
45mod tests;
46
47pub use decode::*;
48pub use encode::*;
49
50use crate::Tag;
51
52impl Tag {
53    /// Returns the name of this tag for error reporting purposes.
54    const fn name(self) -> &'static str {
55        match self {
56            Tag::End => "end",
57            Tag::Byte => "byte",
58            Tag::Short => "short",
59            Tag::Int => "int",
60            Tag::Long => "long",
61            Tag::Float => "float",
62            Tag::Double => "double",
63            Tag::ByteArray => "byte array",
64            Tag::String => "string",
65            Tag::List => "list",
66            Tag::Compound => "compound",
67            Tag::IntArray => "int array",
68            Tag::LongArray => "long array",
69        }
70    }
71}