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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use std::io::{Write, Read, Cursor};
use crate::error::{NBTResult, NBTError};
use crate::tags::Tag;
use crate::encode::{write_tag, write_root};
use crate::blob::Blob;
use crate::decode::{read_tag, read_ident, read_root};
use serde::Serialize;
use crate::ser::NBTSerializer;
use crate::TagIdent;
use serde::de::DeserializeOwned;
use crate::de::NBTDeserializer;

/// A trait supporting encoding of NBT Tags/Blobs into bytes.
pub trait NBTWrite {
    fn write<W: Write>(&self, writer: &mut W) -> NBTResult<()>;

    fn bytes(&self) -> NBTResult<Vec<u8>> {
        let mut buffer = Vec::new();
        self.write(&mut buffer)?;
        Ok(buffer)
    }
}

impl NBTWrite for Tag {
    fn write<W: Write>(&self, writer: &mut W) -> NBTResult<()> {
        write_tag(writer, &self)
    }
}
impl NBTWrite for Blob {
    fn write<W: Write>(&self, writer: &mut W) -> NBTResult<()> {
        write_root(writer, &self.root, &self.elements)
    }
}

/// A trait supporting decoding of bytes into NBT/Tags.
///
/// This trait provides two functions:
/// - `read` for reading from a readable source or buffer
/// - `from_bytes` for reading from a array of bytes
///
///
pub trait NBTRead: Sized {
    /// Function for reading from a buffer.
    fn read<R: Read>(reader: &mut R) -> NBTResult<Self>;

    /// Function for reading from a byte array.
    fn from_bytes<B: AsRef<[u8]>>(data: B) -> NBTResult<Self> {
        Self::read(&mut Cursor::new(data.as_ref().to_vec()))
    }
}

impl NBTRead for Tag {
    fn read<R: Read>(reader: &mut R) -> NBTResult<Self> {
        let ident = read_ident(reader)?;
        read_tag(reader, &ident)
    }
}
impl NBTRead for Blob {
    fn read<R: Read>(reader: &mut R) -> NBTResult<Self> {
        let (name, elements) = read_root(reader)?;
        Ok(Self { root: name, elements })
    }
}

#[cfg(feature="with_serde")]
/// Encode a Serde serializable value into a NBT Tag.
///
/// ### Example
/// ```
/// use nbt::{encode_tag, Tag};
///
/// let list: Vec<i8> = vec![127, 42, 10];
/// let tag = encode_tag(&list).unwrap().unwrap();
///
/// # assert_eq!(tag, Tag::List(vec![Tag::Byte(127), Tag::Byte(42), Tag::Byte(10)]));
/// ```
pub fn encode_tag<T: Serialize>(o: &T) -> NBTResult<Option<Tag>> {
    o.serialize(NBTSerializer)
}

/// Encode a Serde serializable value into a NBT Blob with a given root name.
///
/// ### Example
/// ```
/// use nbt::{encode_tag, encode_named, Tag};
/// use std::collections::HashMap;
/// use serde::Serialize;
///
/// // Define a Serializable Struct
/// #[derive(Serialize)]
/// pub struct Example {
///     name: String,
/// }
///
/// // Create a instance
/// let example = Example {
///     name: "Bananrama".to_string(),
/// };
/// // Encode a NBT blob with name "hello_world"
/// let tag = encode_named(&example, "hello_world").unwrap();
///
/// # let mut test = HashMap::new();
/// # test.insert("name".to_string(), Tag::String("Bananrama".to_string()));
/// # assert_eq!(tag.compound(), Tag::Compound(test));
/// ```
///
#[cfg(feature="with_serde")]
pub fn encode_named<T: Serialize>(o: &T, name: &str) -> NBTResult<Blob> {
    match encode_tag(o)? {
        Some(tag) => if let Tag::Compound(map) = tag {
            Ok(Blob { elements: map, root: name.to_string() })
        } else {
            Err(NBTError::InvalidImplicit { found: tag.ident() })
        },
        // Not sure about this
        None => Err(NBTError::InvalidImplicit { found: TagIdent::TAG_End })
    }
}

#[cfg(feature="with_serde")]
/// Encode a Serde serializable value into a NBT Blob with a empty root name.
///
/// Encode a Serde serializable value into a NBT Blob with a given root name.
/// Encode a Serde serializable value into a NBT Blob with a given root name.
///
/// ### Example
/// ```
/// use nbt::{encode_tag, encode, Tag};
/// use std::collections::HashMap;
/// use serde::Serialize;
///
/// // Define a Serializable Struct
/// #[derive(Serialize)]
/// pub struct Example {
///     foo: String,
///     bar: i8,
///     baz: i16
/// }
///
/// // Create a instance
/// let example = Example {
///     foo: "Hello World!".to_string(),
///     bar: 42,
///     baz: 25565
/// };
/// // Encode a NBT blob with name "example"
/// let tag = encode(&example).unwrap();
///
/// # let mut test = HashMap::new();
/// # test.insert("foo".to_string(), Tag::String("Hello World!".to_string()));
/// # test.insert("bar".to_string(), Tag::Byte(42));
/// # test.insert("baz".to_string(), Tag::Short(25565));
/// # assert_eq!(tag.compound(), Tag::Compound(test));
/// ```
///
pub fn encode<T: Serialize>(o: &T) -> NBTResult<Blob> {
    encode_named(o, "")
}

#[cfg(feature="with_serde")]
/// Decode a NBT Tag into a Serde deserializable value.
///
/// ### Example
/// ```
/// use nbt::{Tag, decode_tag};
///
/// // Create a Byte List.
/// let tag = Tag::List(vec![Tag::Byte(127), Tag::Byte(42)]);
///
/// // Decode the tag into a vec.
/// let list: Vec<i8> = decode_tag(tag).unwrap();
///
/// assert_eq!(list, vec![127, 42]);
/// ```
pub fn decode_tag<T: DeserializeOwned>(tag: Tag) -> NBTResult<T> {
    T::deserialize(NBTDeserializer::some(tag))
}


#[cfg(feature="with_serde")]
/// Decode a NBT Blob into a Serde deserializable value.
///
/// ### Example
/// ```
/// use nbt::{Tag, decode, Blob};
/// use serde::Deserialize;
///
/// // Create Deserializable struct
/// #[derive(Deserialize, PartialEq, Debug)]
/// pub struct Example {
///     foo: String
/// }
///
/// // Create a Blob
/// let mut blob = Blob::new();
/// blob.insert("foo", "bar");
///
/// // Decode the data from blob
/// let data: Example = decode(blob).unwrap();
///
/// assert_eq!(data, Example { foo: "bar".to_string() });
/// ```
pub fn decode<T: DeserializeOwned>(tag: Blob) -> NBTResult<T> {
    T::deserialize(NBTDeserializer::some(Tag::Compound(tag.elements)))
}

#[cfg(feature="with_serde")]
/// Decode a NBT Blob into a Serde deserializable value and the given root name.
///
/// ### Example
/// ```
/// use nbt::{Tag, Blob, decode_named};
/// use serde::Deserialize;
///
/// // Create Deserializable struct
/// #[derive(Deserialize, PartialEq, Debug)]
/// pub struct Example {
///     foo: String
/// }
///
/// // Create a Blob
/// let mut blob = Blob::create("baz");
/// blob.insert("foo", "bar");
///
/// // Decode the root name and data from blob
/// let (root, data): (String, Example) = decode_named(blob).unwrap();
///
/// assert_eq!(data, Example { foo: "bar".to_string() });
/// assert_eq!(root, "baz".to_string());
/// ```
pub fn decode_named<T: DeserializeOwned>(tag: Blob) -> NBTResult<(String, T)> {
    Ok((tag.root.clone(), T::deserialize(NBTDeserializer::some(Tag::Compound(tag.elements)))?))
}