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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
mod array_serializer;
mod de;
mod ser;

use std::collections::HashMap;

use serde::{serde_if_integer128, Deserialize, Serialize};

use crate::{error::Error, ByteArray, IntArray, LongArray};

pub use self::ser::Serializer;

/// Value is a complete NBT value. It owns its data. Compounds and Lists are
/// resursively deserialized. This type takes care to preserve all the
/// information from the original NBT, with the exception of the name of the
/// root compound (which is usually the empty string).
///
/// ```no_run
/// # use fastnbt::Value;
/// # use fastnbt::error::Result;
/// # use std::collections::HashMap;
/// #
/// # fn main() -> Result<()> {
/// #   let mut buf = vec![];
///     let compound: HashMap<String, Value> = fastnbt::from_bytes(buf.as_slice())?;
///     match compound["DataVersion"] {
///         Value::Int(ver) => println!("Version: {}", ver),
///         _ => {},
///     }
///     println!("{:#?}", compound);
/// #   Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Byte(i8),
    Short(i16),
    Int(i32),
    Long(i64),
    Float(f32),
    Double(f64),
    String(String),
    ByteArray(ByteArray),
    IntArray(IntArray),
    LongArray(LongArray),
    List(Vec<Value>),
    Compound(HashMap<String, Value>),
}

#[cfg(feature = "arbitrary1")]
fn het_list<'a, T, F>(u: &mut arbitrary::Unstructured<'a>, f: F) -> arbitrary::Result<Vec<Value>>
where
    F: FnMut(T) -> Value,
    T: arbitrary::Arbitrary<'a>,
{
    Ok(u.arbitrary_iter::<T>()?
        .collect::<arbitrary::Result<Vec<_>>>()?
        .into_iter()
        .map(f)
        .collect())
}

#[cfg(feature = "arbitrary1")]
fn arb_list(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Vec<Value>> {
    use crate::Tag;
    use Value::*;

    Ok(match u.arbitrary::<Tag>()? {
        Tag::End => return Err(arbitrary::Error::IncorrectFormat),
        Tag::Byte => het_list(u, Byte)?,
        Tag::Short => het_list(u, Short)?,
        Tag::Int => het_list(u, Int)?,
        Tag::Long => het_list(u, Long)?,
        Tag::Float => het_list(u, Float)?,
        Tag::Double => het_list(u, Double)?,
        Tag::ByteArray => het_list(u, ByteArray)?,
        Tag::String => het_list(u, String)?,
        Tag::List => {
            // make a list of lists
            let len = u.arbitrary_len::<Value>()?;
            let mut v = vec![];
            for _ in 0..len {
                v.push(Value::List(arb_list(u)?));
            }
            v
        }
        Tag::Compound => het_list(u, Compound)?,
        Tag::IntArray => het_list(u, IntArray)?,
        Tag::LongArray => het_list(u, LongArray)?,
    })
}

#[cfg(feature = "arbitrary1")]
impl<'a> arbitrary::Arbitrary<'a> for Value {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        use crate::Tag;
        use Value::*;

        Ok(match u.arbitrary::<Tag>()? {
            Tag::End => return Err(arbitrary::Error::IncorrectFormat),
            Tag::Byte => Byte(u.arbitrary()?),
            Tag::Short => Short(u.arbitrary()?),
            Tag::Int => Int(u.arbitrary()?),
            Tag::Long => Long(u.arbitrary()?),
            Tag::Float => Float(u.arbitrary()?),
            Tag::Double => Double(u.arbitrary()?),
            Tag::ByteArray => ByteArray(u.arbitrary()?),
            Tag::String => String(u.arbitrary()?),
            Tag::Compound => Compound(u.arbitrary()?),
            Tag::IntArray => IntArray(u.arbitrary()?),
            Tag::LongArray => LongArray(u.arbitrary()?),

            // Lists need to all be the same type.
            Tag::List => List(arb_list(u)?),
        })
    }
}

impl Value {
    pub fn as_i64(&self) -> Option<i64> {
        match *self {
            Value::Byte(v) => Some(v as i64),
            Value::Short(v) => Some(v as i64),
            Value::Int(v) => Some(v as i64),
            Value::Long(v) => Some(v),
            Value::Float(v) => Some(v as i64),
            Value::Double(v) => Some(v as i64),
            _ => None,
        }
    }

    pub fn as_u64(&self) -> Option<u64> {
        match *self {
            Value::Byte(v) => Some(v as u64),
            Value::Short(v) => Some(v as u64),
            Value::Int(v) => Some(v as u64),
            Value::Long(v) => Some(v as u64),
            Value::Float(v) => Some(v as u64),
            Value::Double(v) => Some(v as u64),
            _ => None,
        }
    }

    pub fn as_f64(&self) -> Option<f64> {
        match *self {
            Value::Byte(v) => Some(v as f64),
            Value::Short(v) => Some(v as f64),
            Value::Int(v) => Some(v as f64),
            Value::Long(v) => Some(v as f64),
            Value::Float(v) => Some(v as f64),
            Value::Double(v) => Some(v),
            _ => None,
        }
    }

    pub fn as_str(&self) -> Option<&str> {
        match self {
            Value::String(v) => Some(v),
            _ => None,
        }
    }
}

// ------------- From<T> impls -------------

macro_rules! from {
    ($type:ty, $variant:ident $(, $($part:tt)+)?) => {
        impl From<$type> for Value {
            fn from(val: $type) -> Self {
                Self::$variant(val$($($part)+)?)
            }
        }
        impl From<&$type> for Value {
            fn from(val: &$type) -> Self {
                Self::$variant(val.to_owned()$($($part)+)?)
            }
        }
    };
}
from!(i8, Byte);
from!(u8, Byte, as i8);
from!(i16, Short);
from!(u16, Short, as i16);
from!(i32, Int);
from!(u32, Int, as i32);
from!(i64, Long);
from!(u64, Long, as i64);
from!(f32, Float);
from!(f64, Double);
from!(String, String);
from!(&str, String, .to_owned());
from!(ByteArray, ByteArray);
from!(IntArray, IntArray);
from!(LongArray, LongArray);

impl From<bool> for Value {
    fn from(val: bool) -> Self {
        Self::Byte(i8::from(val))
    }
}
impl From<&bool> for Value {
    fn from(val: &bool) -> Self {
        Self::Byte(i8::from(*val))
    }
}

//
// Everything below is copied from serde_json,
// Partial Eq impls: https://github.com/serde-rs/json/blob/5d2cbcdd4b146e98b5aa2200de7a8ae6231bf0ba/src/value/partial_eq.rs
// to/from_value(): https://github.com/serde-rs/json/blob/52a9c050f5dcc0dc3de4825b131b8ff05219cc82/src/value/mod.rs#L886-L989
//
// For which the license is MIT:
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:

// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

fn eq_i64(value: &Value, other: i64) -> bool {
    value.as_i64().map_or(false, |i| i == other)
}

fn eq_u64(value: &Value, other: u64) -> bool {
    value.as_u64().map_or(false, |i| i == other)
}

fn eq_f64(value: &Value, other: f64) -> bool {
    value.as_f64().map_or(false, |i| i == other)
}

fn eq_str(value: &Value, other: &str) -> bool {
    value.as_str().map_or(false, |i| i == other)
}

impl PartialEq<str> for Value {
    fn eq(&self, other: &str) -> bool {
        eq_str(self, other)
    }
}

impl<'a> PartialEq<&'a str> for Value {
    fn eq(&self, other: &&str) -> bool {
        eq_str(self, other)
    }
}

impl PartialEq<Value> for str {
    fn eq(&self, other: &Value) -> bool {
        eq_str(other, self)
    }
}

impl<'a> PartialEq<Value> for &'a str {
    fn eq(&self, other: &Value) -> bool {
        eq_str(other, self)
    }
}

impl PartialEq<String> for Value {
    fn eq(&self, other: &String) -> bool {
        eq_str(self, other.as_str())
    }
}

impl PartialEq<Value> for String {
    fn eq(&self, other: &Value) -> bool {
        eq_str(other, self.as_str())
    }
}

macro_rules! partialeq_numeric {
    ($($eq:ident [$($ty:ty)*])*) => {
        $($(
            impl PartialEq<$ty> for Value {
                fn eq(&self, other: &$ty) -> bool {
                    $eq(self, *other as _)
                }
            }

            impl PartialEq<Value> for $ty {
                fn eq(&self, other: &Value) -> bool {
                    $eq(other, *self as _)
                }
            }

            impl<'a> PartialEq<$ty> for &'a Value {
                fn eq(&self, other: &$ty) -> bool {
                    $eq(*self, *other as _)
                }
            }

            impl<'a> PartialEq<$ty> for &'a mut Value {
                fn eq(&self, other: &$ty) -> bool {
                    $eq(*self, *other as _)
                }
            }
        )*)*
    }
}

partialeq_numeric! {
    eq_i64[i8 i16 i32 i64 isize]
    eq_u64[u8 u16 u32 u64 usize]
    eq_f64[f32 f64]
}

macro_rules! from_128bit {
    ($($type:ty),+) => {
        $(
            impl From<$type> for Value {
                fn from(val: $type) -> Self {
                    Self::IntArray(IntArray::new(vec![
                        (val >> 96) as i32,
                        (val >> 64) as i32,
                        (val >> 32) as i32,
                        val as i32,
                    ]))
                }
            }

            impl From<&$type> for Value {
                fn from(val: &$type) -> Self {
                    Value::from(*val)
                }
            }
        )+
    };
}
serde_if_integer128! {
    from_128bit!(i128, u128);
}

/// 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(())
/// }
/// #
/// # compare_nbt_values().unwrap();
/// ```
///
/// # 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());
/// ```
pub fn to_value<T>(value: T) -> Result<Value, Error>
where
    T: Serialize,
{
    value.serialize(&mut Serializer)
}

/// Interpret a `fastnbt::Value` as an instance of type `T`.
///
/// # Example
///
/// ```
/// use serde::Deserialize;
/// use fastnbt::nbt;
///
/// #[derive(Deserialize, Debug)]
/// struct User {
///     fingerprint: String,
///     location: String,
/// }
///
/// // The type of `j` is `fastnbt::Value`
/// let j = nbt!({
///     "fingerprint": "0xF9BA143B95FF6D82",
///     "location": "Menlo Park, CA"
/// });
///
/// let u: User = fastnbt::from_value(&j).unwrap();
/// println!("{:#?}", u);
/// ```
///
/// # Errors
///
/// This conversion can fail if the structure of the Value does not match the
/// structure expected by `T`, for example if `T` is a struct type but the Value
/// contains something other than an NBT compound. It can also fail if the structure
/// is correct but `T`'s implementation of `Deserialize` decides that something
/// is wrong with the data, for example required struct fields are missing from
/// the NBT compound or some number is too big to fit in the expected primitive
/// type.
pub fn from_value<'de, T>(value: &'de Value) -> Result<T, Error>
where
    T: Deserialize<'de>,
{
    T::deserialize(value)
}