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
use core::fmt;

macro_rules! impl_type {
    (
        $ty: ty, $for_ty: ty
    ) => {
        impl From<$ty> for $for_ty {
            fn from(val: $ty) -> Self {
                val.0
            }
        }

        impl From<$for_ty> for $ty {
            fn from(val: $for_ty) -> Self {
                Self::new(val)
            }
        }

        impl std::ops::Deref for $ty {
            type Target = $for_ty;

            fn deref(&self) -> &Self::Target {
                &self.0
            }
        }

        impl std::ops::DerefMut for $ty {
            fn deref_mut(&mut self) -> &mut Self::Target {
                &mut self.0
            }
        }
    };
}

/// Little Endian (LE) wrapper type
/// This type is used to indicate that the value is in little endian format
/// It's primary use is in deriving from `BinaryIo` trait
///
/// # Example
/// ```rust ignore
/// use binary_util::types::LE;
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo)]
/// struct MyStruct {
///    test: LE<u32>,
/// }
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LE<T>(pub T);

impl<T> LE<T> {
    pub fn new(val: T) -> Self {
        Self(val)
    }
}
impl_type!(LE<u16>, u16);
impl_type!(LE<u24>, u24);
impl_type!(LE<u32>, u32);
impl_type!(LE<u64>, u64);
impl_type!(LE<u128>, u128);
impl_type!(LE<i16>, i16);
impl_type!(LE<i24>, i24);
impl_type!(LE<i32>, i32);
impl_type!(LE<i64>, i64);
impl_type!(LE<i128>, i128);
impl_type!(LE<f32>, f32);
impl_type!(LE<f64>, f64);

/// Big Endian (BE) wrapper type
/// This type is used to indicate that the value is in big endian format
/// It's primary use is in deriving from `BinaryIo` trait
///
/// # Example
/// ```rust ignore
/// use binary_util::types::BE;
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo)]
/// struct MyStruct {
///   test: BE<u32>,
/// }
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BE<T>(pub T);

impl<T> BE<T> {
    pub fn new(val: T) -> Self {
        Self(val)
    }
}

impl_type!(BE<u16>, u16);
impl_type!(BE<u24>, u24);
impl_type!(BE<u32>, u32);
impl_type!(BE<u64>, u64);
impl_type!(BE<u128>, u128);
impl_type!(BE<i16>, i16);
impl_type!(BE<i24>, i24);
impl_type!(BE<i32>, i32);
impl_type!(BE<i64>, i64);
impl_type!(BE<i128>, i128);
impl_type!(BE<f32>, f32);
impl_type!(BE<f64>, f64);

/// Unsigned 24 bit integer explicit type.
/// You should really only use this when you need to derive the `BinaryIo` trait
/// as it is a helper type.
///
/// # Example
/// ```rust ignore
/// use binary_util::types::u24;
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo)]
/// struct MyStruct {
///    test: u24,
/// }
/// ```
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct u24(pub u32);

impl u24 {
    pub fn new(val: u32) -> Self {
        if val <= 0xFFFFFF {
            Self(val)
        } else {
            panic!("u24: value out of range")
        }
    }
}

impl fmt::Display for u24 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl_type!(u24, u32);

/// Signed 24 bit integer explicit type.
/// You should really only use this when you need to derive the `BinaryIo` trait
/// as it is a helper type.
///
/// # Example
/// ```rust ignore
/// use binary_util::types::i24;
/// use binary_util::BinaryIo;
///
/// #[derive(BinaryIo)]
/// struct MyStruct {
///   test: i24,
/// }
/// ```
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct i24(pub i32);

impl i24 {
    pub fn new(val: i32) -> Self {
        if val >= -0x800000 && val <= 0x7FFFFF {
            Self(val)
        } else {
            panic!("i24: value out of range")
        }
    }
}

impl fmt::Display for i24 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl_type!(i24, i32);

/// A variable length integer type that can be up to 32 bits.
/// This is a helper type for deriving the `BinaryIo` trait.
///
/// You should not use this type directly, if you are reading or writing
/// a variable length integer, use the `ByteWriter` or `ByteReader` and use
/// the corresponding `read_var_u32` or `write_var_u32` methods.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct varu32(pub u32);

impl varu32 {
    pub fn new(val: u32) -> Self {
        Self(val)
    }
}
impl_type!(varu32, u32);

/// A variable length integer type that can be up to 32 bits.
/// This is a helper type for deriving the `BinaryIo` trait.
///
/// You should not use this type directly, if you are reading or writing
/// a variable length integer, use the `ByteWriter` or `ByteReader` and use
/// the corresponding `read_var_i32` or `write_var_i32` methods.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct vari32(pub i32);

impl vari32 {
    pub fn new(val: i32) -> Self {
        Self(val)
    }
}
impl_type!(vari32, i32);

/// A variable length integer type that can be up to 64 bits.
/// This is a helper type for deriving the `BinaryIo` trait.
///
/// > You should not use this type directly, if you are reading or writing
/// > a variable length integer, use the `ByteWriter` or `ByteReader` and use
/// > the corresponding `read_var_u64` or `write_var_u64` methods.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct varu64(pub u64);

impl varu64 {
    pub fn new(val: u64) -> Self {
        Self(val)
    }
}

impl_type!(varu64, u64);

/// A variable length integer type that can be up to 64 bits.
/// This is a helper type for deriving the `BinaryIo` trait.
///
/// > You should not use this type directly, if you are reading or writing
/// > a variable length integer, use the `ByteWriter` or `ByteReader` and use
/// > the corresponding `read_var_i64` or `write_var_i64` methods.
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct vari64(pub i64);

impl vari64 {
    pub fn new(val: i64) -> Self {
        Self(val)
    }
}

impl_type!(vari64, i64);