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
use std::io;
use std::io::Read;
use std::io::Result as Res;
use std::mem::size_of;

use crate::{BigEndian, ERead, Endianness, LittleEndian};

/**
	Implement this for your types to be able to `read` them.

	## Examples

	### Deserialize a struct:

	Note how the trait bound for `R` is `ERead<E>`, as we want to use the functionality of this crate to delegate deserialization to the struct's fields.

	Note: Rust currently can't recognize sealed traits, so even though the primitive types are implemented, you may need to write `where` clauses like below for this to work. If/When the compiler gets smarter about sealed traits this won't be necessary.
	```
	# #[derive(Debug, PartialEq)]
	struct Example {
		a: u8,
		b: bool,
		c: u32,
	}
	{
		use std::io::Result;
		use endio::{Deserialize, Endianness, ERead};

		impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for Example
			where bool: Deserialize<E, R>,
			      u8  : Deserialize<E, R>,
			      u32 : Deserialize<E, R> {
			fn deserialize(reader: &mut R) -> Result<Self> {
				let a = reader.read()?;
				let b = reader.read()?;
				let c = reader.read()?;
				Ok(Example { a, b, c })
			}
		}
	}
	// will then allow you to directly write:
	{
		use endio::LERead;
		let mut reader = &b"\x2a\x01\xcf\xfe\xf3\x2c"[..];
		let e: Example = reader.read().unwrap();

		assert_eq!(e, Example { a: 42, b: true, c: 754187983 });
	}
	# {
	# 	use endio::BERead;
	# 	let mut reader = &b"\x2a\x01\x2c\xf3\xfe\xcf"[..];
	# 	let e: Example = reader.read().unwrap();
	#
	# 	assert_eq!(e, Example { a: 42, b: true, c: 754187983 });
	# }
	```

	### Deserialize a primitive / something where you need the bare `std::io::Read` functionality:

	Note how the trait bound for `R` is `Read`.

	```
	use std::io::{Read, Result};
	use endio::{Deserialize, Endianness, ERead};

	struct new_u8(u8);

	impl<E: Endianness, R: Read> Deserialize<E, R> for new_u8 {
		fn deserialize(reader: &mut R) -> Result<Self> {
			let mut buf = [0; 1];
			reader.read_exact(&mut buf);
			Ok(new_u8(buf[0]))
		}
	}
	```

	### Deserialize with endian-specific code:

	Note how instead of using a trait bound on Endianness, we implement Deserialize twice, once for `BigEndian` and once for `LittleEndian`.
	```
	use std::io::{Read, Result};
	use std::mem::size_of;
	use endio::{BigEndian, Deserialize, LittleEndian};

	struct new_u16(u16);

	impl<R: Read> Deserialize<BigEndian, R> for new_u16 {
		fn deserialize(reader: &mut R) -> Result<Self> {
			let mut buf = [0; size_of::<u16>()];
			reader.read_exact(&mut buf)?;
			Ok(new_u16(u16::from_be_bytes(buf)))
		}
	}

	impl<R: Read> Deserialize<LittleEndian, R> for new_u16 {
		fn deserialize(reader: &mut R) -> Result<Self> {
			let mut buf = [0; size_of::<u16>()];
			reader.read_exact(&mut buf)?;
			Ok(new_u16(u16::from_le_bytes(buf)))
		}
	}
	```
*/
pub trait Deserialize<E: Endianness, R>: Sized {
	/// Deserializes the type by reading from the reader.
	fn deserialize(reader: &mut R) -> Res<Self>;
}

/// Reads a bool by reading a byte, returning false for 0, true for 1, and an `InvalidData` error for any other value.
impl<E: Endianness, R: Read> Deserialize<E, R> for bool {
	fn deserialize(reader: &mut R) -> Res<Self> {
		let mut buf = [0; size_of::<Self>()];
		reader.read_exact(&mut buf)?;
		match buf[0] {
			0 => Ok(false),
			1 => Ok(true),
			_ => Err(io::Error::new(io::ErrorKind::InvalidData, "bool had value other than 0 or 1")),
		}
	}
}

impl<E: Endianness, R: Read> Deserialize<E, R> for i8 {
	fn deserialize(reader: &mut R) -> Res<Self> {
		let mut buf = [0; size_of::<Self>()];
		reader.read_exact(&mut buf)?;
		Ok(Self::from_ne_bytes(buf))
	}
}

impl<E: Endianness, R: Read> Deserialize<E, R> for u8 {
	fn deserialize(reader: &mut R) -> Res<Self> {
		let mut buf = [0; size_of::<Self>()];
		reader.read_exact(&mut buf)?;
		Ok(Self::from_ne_bytes(buf))
	}
}

macro_rules! impl_int {
	($t:ident) => {
		impl<R: Read> Deserialize<BigEndian, R> for $t {
			fn deserialize(reader: &mut R) -> Res<Self> {
				let mut buf = [0; size_of::<Self>()];
				reader.read_exact(&mut buf)?;
				Ok(Self::from_be_bytes(buf))
			}
		}

		impl<R: Read> Deserialize<LittleEndian, R> for $t {
			fn deserialize(reader: &mut R) -> Res<Self> {
				let mut buf = [0; size_of::<Self>()];
				reader.read_exact(&mut buf)?;
				Ok(Self::from_le_bytes(buf))
			}
		}

		#[cfg(test)]
		mod $t {
			#[test]
			fn test() {
				let integer: u128 = 0xbaadf00dbaadf00dbaadf00dbaadf00d;
				let bytes = b"\x0d\xf0\xad\xba\x0d\xf0\xad\xba\x0d\xf0\xad\xba\x0d\xf0\xad\xba";
				let mut val: $t;
				{
					use crate::BERead;
					let mut reader = &bytes[..];
					val = reader.read().unwrap();
					assert_eq!(val, (integer as $t).to_be());
				}
				{
					use crate::LERead;
					let mut reader = &bytes[..];
					val = reader.read().unwrap();
					assert_eq!(val, (integer as $t).to_le());
				}
			}
		}
	}
}

impl_int!(u16);
impl_int!(u32);
impl_int!(u64);
impl_int!(u128);
impl_int!(i16);
impl_int!(i32);
impl_int!(i64);
impl_int!(i128);

impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for f32 where u32: Deserialize<E, R> {
	fn deserialize(reader: &mut R) -> Res<Self> {
		let ival: u32 = reader.read()?;
		Ok(Self::from_bits(ival))
	}
}

impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for f64 where u64: Deserialize<E, R> {
	fn deserialize(reader: &mut R) -> Res<Self> {
		let ival: u64 = reader.read()?;
		Ok(Self::from_bits(ival))
	}
}

#[cfg(test)]
mod tests {
	use std::io;
	use std::io::Result as Res;

	#[test]
	fn read_bool_false() {
		let data = b"\x00";
		let mut val: bool;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, false);
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, false);
		}
	}

	#[test]
	fn read_bool_true() {
		let data = b"\x01";
		let mut val: bool;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, true);
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, true);
		}
	}

	#[test]
	fn read_bool_invalid() {
		let data = b"\x2a";
		let mut val;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read::<bool>().unwrap_err();
			assert_eq!(val.kind(), io::ErrorKind::InvalidData);
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read::<bool>().unwrap_err();
			assert_eq!(val.kind(), io::ErrorKind::InvalidData);
		}
	}

	#[test]
	fn read_i8() {
		let data = b"\x80";
		let mut val: i8;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, i8::min_value());
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, i8::min_value());
		}
	}

	#[test]
	fn read_u8() {
		let data = b"\xff";
		let mut val: u8;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, u8::max_value());
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, u8::max_value());
		}
	}

	#[test]
	fn read_f32() {
		let data = b"\x44\x20\xa7\x44";
		let mut val: f32;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, 642.613525390625);
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, 1337.0083007812);
		}
	}

	#[test]
	fn read_f64() {
		let data = b"\x40\x94\x7a\x14\xae\xe5\x94\x40";
		let mut val: f64;
		{
			use crate::BERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, 1310.5201984283194);
		}
		{
			use crate::LERead;
			let mut reader = &data[..];
			val = reader.read().unwrap();
			assert_eq!(val, 1337.4199999955163);
		}
	}

	#[test]
	fn read_struct_forced() {
		struct Test {
			a: u16,
		}
		{
			use crate::{Deserialize, Endianness, ERead};

			impl<E: Endianness, R: ERead<E>> Deserialize<E, R> for Test where u16: Deserialize<E, R> {
				fn deserialize(reader: &mut R) -> Res<Self> {
					let a = reader.read()?;
					Ok(Test { a })
				}
			}
		}

		use crate::LERead;
		let data = b"\xba\xad";
		let mut reader = &data[..];
		let val: Test = reader.read_be().unwrap();
		assert_eq!(val.a, 0xbaad);
	}
}