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
#[macro_use]
extern crate lazy_static;

mod decoder;
mod encoder;
#[macro_use]
mod tag;

pub use tag::*;

use std::str;
use std::ops;

#[derive(Debug, PartialEq, Clone)]
pub struct Nbt {
    pub name: String,
    pub tag: Tag,
}

impl Nbt {
    pub fn new(name: String, tag: Tag) -> Self {
        Self { name, tag }
    }

    pub fn get<I: Index>(&self, index: I) -> Option<&Tag> {
        self.tag.get(index)
    }

    pub fn get_mut<I: Index>(&mut self, index: I) -> Option<&mut Tag> {
        self.tag.get_mut(index)
    }

    pub fn insert<I: Index>(&mut self, index: I, value: Tag) {
        self.tag.insert(index, value);
    }
}

impl<I: Index> ops::Index<I> for Tag {
    type Output = Self;

    fn index(&self, index: I) -> &Self::Output {
        match self.get(index) {
            Some(tag) => tag,
            None => &Tag::End,
        }
    }
}

impl<I: Index> ops::IndexMut<I> for Tag {
    fn index_mut(&mut self, index: I) -> &mut Self::Output {
        index.index_or_insert(self)
    }
}

impl ops::Index<&str> for Nbt {
    type Output = Tag;

    fn index<'a>(&self, index: &str) -> &Self::Output {
        &self.tag[index]
    }
}

impl ops::IndexMut<&str> for Nbt {
    fn index_mut(&mut self, index: &str) -> &mut Self::Output {
        &mut self.tag[index]
    }
}

#[cfg(test)]
mod tests {
    use crate::{Kind, Nbt, Tag, tag};
    use std::io::Cursor;

    fn nbt(data: &[u8]) -> Nbt {
        let data = data.to_vec();
        let mut data = Cursor::new(data);

        // decode nbt from data
        let nbt = Nbt::decode(&mut data);

        assert!(nbt.is_ok(), "failed to decode nbt");

        // unwrap nbt data
        let nbt = nbt.unwrap();

        assert!(match nbt.tag.kind() {
            Kind::Compound => true,
            _ => false,
        }, "nbt does not match expected type");

        nbt
    }

    #[test]
    fn uncompressed() {
        nbt(include_bytes!("../examples/uncompressed.nbt"));
    }

    #[test]
    fn compressed() {
        nbt(include_bytes!("../examples/compressed.nbt"));
    }

    #[cfg(feature = "preserve-order")]
    #[test]
    fn insertion_order() {
        let bytes = include_bytes!("../examples/uncompressed.nbt");
        let nbt = nbt(bytes);
        let mut new_bytes = vec![];

        assert!(nbt.encode(&mut new_bytes, false).is_ok(), "failed to encode");
        assert!(bytes.len() == new_bytes.len(), "size doesn't match");
        assert!({
            let len = bytes.len();
            let mut matches = true;

            for i in 0..len {
                if bytes[i] != new_bytes[i] {
                    matches = false;
                    break;
                }
            }

            matches
        }, "data doesn't match")
    }

    #[test]
    fn string() {
        let nbt: Tag = String::from("\"\"\\'this is a test!").into();
        assert_eq!(format!("{}", nbt), "'\"\"\\\\\\'this is a test!'");
    }

    #[test]
    fn assignment() {
        let mut nbt = nbt(include_bytes!("../examples/uncompressed.nbt"));
        nbt["testing"] = tag!("[B;8b,2b]");

        assert_eq!(nbt["testing"], tag!("[B;8b,2b]"));
    }

    #[test]
    fn display() {
        let byte_array = tag!("[B;1B,2B,6b,10b]").to_string();
        let list = tag!("[5l,10L,20l]").to_string();
        let compound = tag!("{name:'Jaden'}").to_string();

        assert_eq!(byte_array, "[B;1b,2b,6b,10b]");
        assert_eq!(list, "[5L,10L,20L]");
        assert_eq!(compound, "{name:\"Jaden\"}");
    }

    #[test]
    fn verify_value() {
        let nbt = nbt(include_bytes!("../examples/uncompressed.nbt"));
        let seed: i64 = (&nbt["Data"]["RandomSeed"]).into();
        let version_name: String = (&nbt["Data"]["Version"]["Name"]).into();

        assert_eq!(seed, 4443890602994873962);
        assert_eq!(version_name, "1.14.1 Pre-Release 2");
    }
}