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
//! This Module contains all neccessary traits for [`NbtValue`]'s
//!
//! [`NbtValue`]: `crate::NbtValue`
use std::collections::HashMap;

use crate::{NbtValue, error::NbtResult, reader::NbtReader};

/// Trait for every type that has to be write NBT data
pub trait NbtWrite {
    /// function to write a signed 8-bit integer array
    fn write_i8_array(writer: &mut Vec<u8>, data: &[i8]);
    /// function to write a signed 32-bit integer array
    fn write_i32_array(writer: &mut Vec<u8>, data: &[i32]);
    /// function to write a signed 64-bit integer array
    fn write_i64_array(writer: &mut Vec<u8>, data: &[i64]);
    /// function to write a string
    fn write_nbt_string(writer: &mut Vec<u8>, data: &str);
    /// function to write a list of nbt values
    fn write_list(writer: &mut Vec<u8>, data: &[NbtValue]) -> NbtResult<()>;
    /// function to write a list of named nbt values
    fn write_compound(writer: &mut Vec<u8>, name: Option<&String>, data: Vec<(&String, &NbtValue)>) -> NbtResult<()>;
    /// function to write nbt data to an vector
    fn write_to(value: &NbtValue, buff: &mut Vec<u8>) -> NbtResult<()>;
    /// function to write nbt data to an vector with an name
    fn write_to_with_name(name: &str, value: &NbtValue, buff: &mut Vec<u8>) -> NbtResult<()>;
    /// function to write a text component to an vector
    fn write_text_component(writer: &mut Vec<u8>, value: &NbtValue) -> NbtResult<()>;
    /// function to convert nbt data into bytes
    fn to_bytes(value: &NbtValue) -> NbtResult<Vec<u8>> {
        let mut buff = Vec::new();
        Self::write_to(value, &mut buff)?;
        Ok(buff)
    }
}

/// A trait, that allowes to convert the struct to the nbt representation
pub trait AsNbtValue {
    /// converts the struct to a [`NbtValue`]
    ///
    /// [`NbtValue`]: `nbt_lib::NbtValue`
    fn as_nbt_value(&self) -> Result<NbtValue, ()>;
}
/// A trait, that allowes to convert the struct to the nbt representation
pub trait FromNbtValue {
    /// converts the struct to a [`NbtValue`]
    ///
    /// [`NbtValue`]: `nbt_lib::NbtValue`
    fn from_nbt_value(value: NbtValue) -> Result<Self, ()> where Self: Sized;
}

/// A trait to determine the size of a struct
pub trait SizeOf {
    /// Get the size of a struct
    fn size_of(&self) -> usize;
}

/// trait for every type that has to be read as NBT data
pub trait NbtRead {
    /// function to read a signed 8-bit integer array
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn read_i8_array(reader: &mut NbtReader) -> NbtResult<Vec<i8>>;
    /// function to read a signed 32-bit integer array
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn read_i32_array(reader: &mut NbtReader) -> NbtResult<Vec<i32>>;
    /// function to read a signed 64-bit integer array
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn read_i64_array(reader: &mut NbtReader) -> NbtResult<Vec<i64>>;
    /// function to read a string
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn read_nbt_string(reader: &mut NbtReader) -> NbtResult<String>;
    /// function to read a list of nbt values
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn read_list(reader: &mut NbtReader) -> NbtResult<Vec<NbtValue>>;
    /// function to read a list of named nbt values
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn read_compound(reader: &mut NbtReader) -> NbtResult<HashMap<String, NbtValue>>;
    /// function to read nbt data from a `NbtReader`
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn from_reader(reader: NbtReader) -> NbtResult<NbtValue>;
    /// function to read a text component from a `NbtReader`
    ///
    /// # Arguments
    /// `reader` - A mutable reference to `NbtReader`
    ///
    /// # Returns
    ///
    /// Returns a Result with the value of an Error, why the read failed
    fn from_reader_text_component(reader: NbtReader) -> NbtResult<NbtValue>;
}
/// A trait that allows structs to have a function that transrforms themself into a `NbtValue`
pub trait IntoNbt {
    /// Converts this struct into a `NbtValue`
    fn to_nbt(&self) -> NbtValue;
}