valence_nbt/
lib.rs

1#![doc = include_str!("../../README.md")]
2// Run locally with `RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --open`
3#![cfg_attr(docsrs, feature(doc_cfg))]
4
5#[cfg(feature = "binary")]
6#[cfg_attr(docsrs, doc(cfg(feature = "binary")))]
7pub use binary::{from_binary, to_binary};
8pub use compound::Compound;
9pub use error::*;
10pub use list::List;
11pub use tag::*;
12pub use value::Value;
13
14#[cfg(feature = "binary")]
15#[cfg_attr(docsrs, doc(cfg(feature = "binary")))]
16pub mod binary;
17pub mod compound;
18pub mod conv;
19mod error;
20pub mod list;
21#[cfg(feature = "serde")]
22#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
23pub mod serde;
24#[cfg(feature = "snbt")]
25#[cfg_attr(docsrs, doc(cfg(feature = "snbt")))]
26pub mod snbt;
27mod tag;
28pub mod value;
29
30/// A convenience macro for constructing [`Compound`]s.
31///
32/// Key expressions must implement `Into<String>` while value expressions must
33/// implement `Into<Value>`.
34///
35/// # Examples
36///
37/// ```
38/// use valence_nbt::{compound, List};
39///
40/// let c = compound! {
41///     "byte" => 123_i8,
42///     "list_of_int" => List::Int(vec![3, -7, 5]),
43///     "list_of_string" => List::String(vec![
44///         "foo".to_owned(),
45///         "bar".to_owned(),
46///         "baz".to_owned()
47///     ]),
48///     "string" => "aé日",
49///     "compound" => compound! {
50///         "foo" => 1,
51///         "bar" => 2,
52///         "baz" => 3,
53///     },
54///     "int_array" => vec![5, -9, i32::MIN, 0, i32::MAX],
55///     "byte_array" => vec![0_i8, 2, 3],
56///     "long_array" => vec![123_i64, 456, 789],
57/// };
58///
59/// println!("{c:?}");
60/// ```
61///
62/// It is also possible to specify a custom string type like this:
63/// ```
64/// # use std::borrow::Cow;
65///
66/// use valence_nbt::compound;
67///
68/// let c = compound! { <Cow<str>>
69///     "foo" => 123_i8,
70/// };
71///
72/// println!("{c:?}");
73/// ```
74#[macro_export]
75macro_rules! compound {
76    (<$string_type:ty> $($key:expr => $value:expr),* $(,)?) => {
77        <$crate::Compound<$string_type> as ::std::iter::FromIterator<($string_type, $crate::Value<$string_type>)>>::from_iter([
78            $(
79                (
80                    ::std::convert::Into::<$string_type>::into($key),
81                    ::std::convert::Into::<$crate::Value<$string_type>>::into($value)
82                ),
83            )*
84        ])
85    };
86
87    ($($key:expr => $value:expr),* $(,)?) => {
88        compound!(<::std::string::String> $($key => $value),*)
89    };
90}
91
92/// A convenience macro for constructing [`Compound`]`<`[`JavaString`]`>`s
93///
94/// [`JavaString`]: java_string::JavaString
95#[cfg(feature = "java_string")]
96#[cfg_attr(docsrs, doc(cfg(feature = "java_string")))]
97#[macro_export]
98macro_rules! jcompound {
99    ($($key:expr => $value:expr),* $(,)?) => {
100        compound!(<::java_string::JavaString> $($key => $value),*)
101    }
102}