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
#[derive(Debug, thiserror::Error)]
pub enum Error {
	#[error("out-of-bounds seek to {pos:#X} (size {size:#X})")]
	Seek { pos: usize, size: usize },
	#[error("out-of-bounds read of {pos:#X}+{len} (size {size:#X})")]
	Read { pos: usize, len: usize, size: usize },
	#[error("error at {pos:#X}: {source}")]
	Other { pos: usize, #[source] source: Box<dyn std::error::Error + Send + Sync> },
}

pub type Result<T, E=Error> = std::result::Result<T, E>;

impl Error {
	pub fn pos(&self) -> usize {
		match self {
			Error::Seek { pos, .. } => *pos,
			Error::Read { pos, .. } => *pos,
			Error::Other { pos, .. } => *pos,
		}
	}

	pub fn pos_mut(&mut self) -> &mut usize {
		match self {
			Error::Seek { pos, .. } => pos,
			Error::Read { pos, .. } => pos,
			Error::Other { pos, .. } => pos,
		}
	}
}

#[derive(Clone, Debug, thiserror::Error)]
#[error("mismatched {type_}. expected: {expected}, got: {got}", type_ = std::any::type_name::<T>())]
pub struct CheckError<T: std::fmt::Display> {
	pub expected: T,
	pub got: T,
}

#[derive(Clone, Debug, thiserror::Error)]
pub struct CheckBytesError {
	pub expected: Vec<u8>,
	pub got: Vec<u8>,
}

impl std::fmt::Display for CheckBytesError {
	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
		let mut got = Vec::new();
		let mut exp = Vec::new();
		for (&g, &e) in std::iter::zip(&self.got, &self.expected) {
			got.extend(std::ascii::escape_default(g).map(char::from));
			exp.extend(std::ascii::escape_default(e).map(char::from));
			while got.len() < exp.len() { got.push('░') }
			while exp.len() < got.len() { exp.push('░') }
		}
		writeln!(f, "mismatched bytes.")?;
		writeln!(f, "expected: b\"{}\"", String::from_iter(exp))?;
		write  !(f, "got:      b\"{}\"", String::from_iter(got))
	}
}

/// An incremental reader from a byte slice.
///
/// Cloning this type is cheap, but it does not implement [`Copy`] for similar reasons as
/// [`Range`](`std::ops::Range`).
#[derive(Clone)]
pub struct Reader<'a> {
	pos: usize,
	data: &'a [u8],
}

impl<'a> std::fmt::Debug for Reader<'a> {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		f.debug_struct("Reader")
			.field("pos", &self.pos)
			.field("data", &format_args!("[_; {}]", self.data.len()))
			.finish()
	}
}

impl<'a> Reader<'a> {
	/// Constructs a new `Reader`.
	#[inline(always)]
	pub fn new(data: &'a [u8]) -> Reader<'a> {
		Self {
			pos: 0,
			data,
		}
	}

	/// Reads a slice of data from the input. No copying is done.
	///
	/// Returns an error if there is not enough data left, in which case the read position is
	/// unchanged.
	#[inline(always)]
	pub fn slice(&mut self, len: usize) -> Result<&'a [u8]> {
		if len > self.remaining().len() {
			return Err(Error::Read { pos: self.pos(), len, size: self.len() });
		}
		let pos = self.pos;
		self.pos += len;
		Ok(&self.data[pos..pos+len])
	}

	/// Reads a fixed-size slice of data from the input.
	///
	/// Handles errors identically to [`slice`](`Self::slice`).
	#[inline(always)]
	pub fn array<const N: usize>(&mut self) -> Result<[u8; N]> {
		let mut x = [0; N];
		self.read_into(&mut x)?;
		Ok(x)
	}

	/// Reads a slice of data into a preexisting buffer.
	///
	/// Handles errors identically to [`slice`](`Self::slice`).
	#[inline(always)]
	pub fn read_into(&mut self, buf: &mut [u8]) -> Result<()> {
		buf.copy_from_slice(self.slice(buf.len())?);
		Ok(())
	}

	/// Returns the read position of the reader.
	///
	/// To change the position, use [`seek`](`Self::seek`) or [`at`](`Self::at`).
	#[must_use]
	#[inline(always)]
	pub fn pos(&self) -> usize {
		self.pos
	}

	/// Returns the total length of the input.
	///
	/// Note that this is the total length, which does not change. See
	/// [`remaining`](`Self::remaining`) for the number of bytes left that can be read.
	#[must_use]
	#[inline(always)]
	pub fn len(&self) -> usize {
		self.data.len()
	}

	/// Returns true if there are no more bytes left to read.
	///
	/// Note that unlike slices, this is based on [`remaining`](`Self::remaining`), not
	/// [`len`](`Self::len`).
	#[must_use]
	#[inline(always)]
	pub fn is_empty(&self) -> bool {
		self.remaining().is_empty()
	}

	/// Returns the remaining data in the buffer.
	#[must_use]
	#[inline(always)]
	pub fn remaining(&self) -> &'a [u8] {
		&self.data[self.pos()..]
	}

	/// Returns the data being read from.
	///
	/// This is the full slice; for only the remainder, see [`remaining`](`Self::remaining`).
	#[must_use]
	#[inline(always)]
	pub fn data(&self) -> &'a [u8] {
		self.data
	}

	/// Sets the read position.
	///
	/// Returns an error if the position is out of bounds.
	///
	/// See [`at`](`Self::at`) for a version that returns a copy.
	#[inline(always)]
	pub fn seek(&mut self, pos: usize) -> Result<()> {
		if pos > self.len() {
			return Err(Error::Seek { pos, size: self.len() })
		}
		self.pos = pos;
		Ok(())
	}

	/// Returns a copy of the reader at the specified position.
	///
	/// See also [`ptrN`](`Self::ptrN`) for a shorthand for the common pattern of
	/// `f.clone().at(f.u32()? as usize)?`.
	#[inline(always)]
	pub fn at(&self, pos: usize) -> Result<Self> {
		let mut a = self.clone();
		a.seek(pos)?;
		Ok(a)
	}

	/// Rounds the read position up to the next multiple of `size`.
	#[inline(always)]
	pub fn align(&mut self, size: usize) -> Result<&'a [u8]> {
		self.slice((size-(self.pos()%size))%size)
	}

	/// Reads a number of bytes and returns an error if they are not as expected.
	///
	/// If it does not match, the read position is not affected.
	#[inline(always)]
	pub fn check(&mut self, v: &[u8]) -> Result<()> {
		let pos = self.pos();
		let u = self.slice(v.len())?;
		if u != v {
			self.pos = pos;
			return Err(Error::Other { pos, source: CheckBytesError {
				got:      u.to_owned(),
				expected: v.to_owned(),
			}.into() })
		}
		Ok(())
	}
}

#[cfg(doc)]
#[doc(hidden)]
pub type T = ();

/// Functions for reading primitives from the stream. The underlying functions are hidden from
/// the docs for brevity.
///
/// Supported primitives are `u8..=u128`, `i8..=i128`, `f32`, `f64`.
///
/// The functions are suffixed with either `_le` or `_be`, for endianness. To use unsuffixed
/// versions, import either the [`Le`] or [`Be`] trait.
#[cfg(doc)]
impl Reader<'_> {
	// Could add a shitload of #[doc(alias = _)], but don't wanna.

	/// Read a primitive from the input.
	pub fn T(&mut self) -> Result<T> {}

	/// Read a primitive from the input, giving an error if it is not as expected.
	///
	/// If it not match, the read position is not affected.
	pub fn check_T(&mut self, v: T) -> Result<()> {}

	/// Read a `uN` primitive from the input, and return a new `Reader` at that position.
	///
	/// This is a shorthand for the common pattern of `f.clone().at(f.uN()? as usize)?`.
	///
	/// Note that no checking is made that the value actually fits inside a `usize`; the higher bits are simply discarded.
	pub fn ptrN(&mut self) -> Result<Self> {}
}

mod seal { pub trait Sealed: Sized {} }
impl seal::Sealed for Reader<'_> {}

macro_rules! primitives {
	(
		$(#[$trait_attrs:meta])* trait $trait:ident;
		$suf:ident, $conv:ident;
		{ $($type:ident),* }
		{ $($ptr:tt),* }
	) => { paste::paste! {
		#[doc(hidden)]
		impl<'a> Reader<'a> {
			$(#[inline(always)] pub fn [<$type $suf>](&mut self) -> Result<$type> {
				Ok($type::$conv(self.array()?))
			})*
			$(#[inline(always)] pub fn [<check_ $type $suf>](&mut self, v: $type) -> Result<()> {
				let pos = self.pos();
				let u = self.[< $type $suf >]()?;
				if u != v {
					self.pos = pos;
					return Err(Error::Other { pos, source: CheckError {
						got: u,
						expected: v,
					}.into() })
				}
				Ok(())
			})*
			$(#[inline(always)] pub fn [<ptr$ptr $suf>](&mut self) -> Result<Self> {
				self.clone().at(self.[<u$ptr $suf>]()? as usize)
			})*
		}

		$(#[$trait_attrs])*
		pub trait $trait: seal::Sealed {
			$(#[doc(hidden)] fn $type(&mut self) -> Result<$type>;)*
			$(#[doc(hidden)] fn [<check_ $type>](&mut self, v: $type) -> Result<()>;)*
			$(#[doc(hidden)] fn [<ptr $ptr>](&mut self) -> Result<Self>;)*
		}

		impl<'a> $trait for Reader<'a> {
			$(#[doc(hidden)] #[inline(always)] fn $type(&mut self) -> Result<$type> {
				self.[<$type $suf>]()
			})*
			$(#[doc(hidden)] #[inline(always)] fn [<check_ $type>](&mut self, v: $type) -> Result<()> {
				self.[<check_ $type $suf>](v)
			})*
			$(#[doc(hidden)] #[inline(always)] fn [<ptr$ptr>](&mut self) -> Result<Self> {
				self.[<ptr$ptr $suf>]()
			})*
		}
	} }
}

primitives!(
	/// Allows reading little-endian primitives without `_le` suffix.
	///
	/// It is recommended to import this as `use gospel::read::Le as _;`.
	trait Le;
	_le, from_le_bytes;
	{
		u8, u16, u32, u64, u128,
		i8, i16, i32, i64, i128,
		f32, f64
	}
	{ 8, 16, 32, 64, 128 }
);
primitives!(
	/// Allows reading little-endian primitives without `_be` suffix.
	///
	/// It is recommended to import this as `use gospel::read::Be as _;`.
	trait Be;
	_be, from_be_bytes;
	{
		u8, u16, u32, u64, u128,
		i8, i16, i32, i64, i128,
		f32, f64
	}
	{ 8, 16, 32, 64, 128 }
);