1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![doc = include_str!("../README.md")]
#![deny(
    rustdoc::broken_intra_doc_links,
    rustdoc::private_intra_doc_links,
    rustdoc::missing_crate_level_docs,
    rustdoc::invalid_codeblock_attributes,
    rustdoc::invalid_rust_codeblocks,
    rustdoc::bare_urls,
    rustdoc::invalid_html_tags
)]
#![warn(
    trivial_casts,
    trivial_numeric_casts,
    unused_lifetimes,
    unused_import_braces,
    unreachable_pub,
    clippy::dbg_macro
)]

#[cfg(feature = "binary")]
pub use binary::{from_binary, to_binary};
pub use compound::Compound;
pub use list::List;
pub use tag::Tag;
pub use value::Value;

#[cfg(feature = "binary")]
pub mod binary;
pub mod compound;
pub mod conv;
pub mod list;
#[cfg(feature = "serde")]
pub mod serde;
#[cfg(feature = "snbt")]
pub mod snbt;
mod tag;
pub mod value;

/// A convenience macro for constructing [`Compound`]s.
///
/// Key expressions must implement `Into<String>` while value expressions must
/// implement `Into<Value>`.
///
/// # Examples
///
/// ```
/// use valence_nbt::{compound, List};
///
/// let c = compound! {
///     "byte" => 123_i8,
///     "list_of_int" => List::Int(vec![3, -7, 5]),
///     "list_of_string" => List::String(vec![
///         "foo".to_owned(),
///         "bar".to_owned(),
///         "baz".to_owned()
///     ]),
///     "string" => "aé日",
///     "compound" => compound! {
///         "foo" => 1,
///         "bar" => 2,
///         "baz" => 3,
///     },
///     "int_array" => vec![5, -9, i32::MIN, 0, i32::MAX],
///     "byte_array" => vec![0_i8, 2, 3],
///     "long_array" => vec![123_i64, 456, 789],
/// };
///
/// println!("{c:?}");
/// ```
///
/// It is also possible to specify a custom string type like this:
/// ```
/// # use std::borrow::Cow;
///
/// use valence_nbt::compound;
///
/// let c = compound! { <Cow<str>>
///     "foo" => 123_i8,
/// };
///
/// println!("{c:?}");
/// ```
#[macro_export]
macro_rules! compound {
    (<$string_type:ty> $($key:expr => $value:expr),* $(,)?) => {
        <$crate::Compound<$string_type> as ::std::iter::FromIterator<($string_type, $crate::Value<$string_type>)>>::from_iter([
            $(
                (
                    ::std::convert::Into::<$string_type>::into($key),
                    ::std::convert::Into::<$crate::Value<$string_type>>::into($value)
                ),
            )*
        ])
    };

    ($($key:expr => $value:expr),* $(,)?) => {
        compound!(<::std::string::String> $($key => $value),*)
    };
}

/// A convenience macro for constructing [`Compound`]`<`[`JavaString`]`>`s
///
/// [`JavaString`]: java_string::JavaString
#[cfg(feature = "java_string")]
#[macro_export]
macro_rules! jcompound {
    ($($key:expr => $value:expr),*, $(,)?) => {
        compound!(<::java_string::JavaString> $($key => $value),*)
    }
}