byte-chisel 0.2.1

A library for decoding byte-streams into data structures
Documentation
use crate::*;

macro_rules! ty_fn_endian {
	($ty: ident, $endian: ident, $doc_endian: ident) => {
				#[doc =
concat!(r"Reads a single [`", stringify!($ty), r"`] in [", stringify!($doc_endian), "-endian][Endianness::", stringify!($endian), "] byte order.

## Errors

If an error is returned, the chisel [breaks][crate#breaking].
")]
		#[inline]
		pub fn $ty(&mut self) -> InfallibleFormatResult<$ty, S> {
			self.0.$ty(Endianness::$endian)
		}
	};
}

/**
 * A [`Chisel`], with little-endian byte order.
 *
 * See the [crate documentation](crate) for more information.
 */
#[repr(transparent)]
pub struct ChiselLittleEndian<'a, S : ChiselSource>(pub(crate) &'a mut Chisel<S>);

impl<'a, S : ChiselSource> ChiselLittleEndian<'a, S> {
	/** Returns a reference to the inner `Chisel`. */
	#[inline]
	pub fn inner(&mut self) -> &mut Chisel<S> { self.0 }

	/** Consumes the wrapper, returning the input chisel reference. */
	#[inline]
	pub fn into_inner(self) -> &'a mut Chisel<S> { self.0 }

	/**
	* Returns a format error that occurred `backstep` bytes ago.
	*
	* This is a convenience method for manually determining the appropriate byte offset and calling [ChiselError::format].
	*/
	#[inline]
	pub fn error<E>(&self, err: E, backstep: usize) -> ChiselError<E, S::Error> { self.0.error(err, backstep) }

	/** Returns the current byte offset of the Chisel. */
	#[inline]
	pub fn offset(&self) -> usize { self.0.offset() }

	/**
	* Fills a provided buffer with bytes from the source.
	*
	* This operation is endian-independent.
	*
	* ## Errors
	* This function returns an [`ChiselErrorData::EndOfInput`] if the end of the input is encountered
	* before the buffer is completely filled.
	*
	* If an error is returned, the contents of `buf` are unspecified and the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn read_buf(&mut self, buf: &mut [u8]) -> InfallibleFormatResult<(), S> { self.0.read_buf(buf) }


	/**
	* Reads a single byte [`u8`].
	*
	* This operation is endian-independent.
	*/
	#[inline]
	pub fn u8(&mut self) -> InfallibleFormatResult<u8, S> { self.0.u8() }
	ty_fn_endian!(u16, Little, little);
	ty_fn_endian!(u32, Little, little);
	ty_fn_endian!(u64, Little, little);

	/**
	* Reads a single _signed_ byte [`i8`].
	*
	* This operation is endian-independent.
	*/
	#[inline]
	pub fn i8(&mut self) -> InfallibleFormatResult<i8, S> { self.0.i8() }
	ty_fn_endian!(i16, Little, little);
	ty_fn_endian!(i32, Little, little);
	ty_fn_endian!(i64, Little, little);

	ty_fn_endian!(f32, Little, little);
	ty_fn_endian!(f64, Little, little);

	/**
	* Skips `how_many` bytes of the underlying source.
	*
	* This operation is endian-independent.
	*
	* This is provided for optimization reasons. This hint is forwarded to the source.
	*
	* Exactly `how_many` bytes are skipped, as-if [`read_buf`][Self::read_buf] were called with a buffer
	* of appropriate size, with the buffer being discarded.
	*
	* ## Errors
	*
	* If an end-of-input condition occurs before `how_many` bytes are skipped, an [`ChiselErrorData::EndOfInput`] error is returned.
	*
	* If an error is returned, the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn skip(&mut self, how_many: usize) -> InfallibleFormatResult<(), S> { self.0.skip(how_many) }

	#[cfg(feature = "alloc")]
	/**
	* Reads bytes into the provided `dest` Vec until the `delimiter` byte is encountered.
	*
	* This operation is endian-independent.
	*
	* ## Errors
	* If the end of the input is encountered before the delimiter is found, [`ChiselErrorData::EndOfInput`] is returned.
	*
	* If an error is returned, the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn read_until(&mut self, byte: u8, dest: &mut Vec<u8>) -> InfallibleFormatResult<(), S> { self.0.read_until(byte, dest) }

	#[cfg(feature = "alloc")]
	/**
	* Reads bytes into the provided `dest` Vec until the `delimiter` byte or end-of-input is encountered.
	*
	* This operation is endian-independent.
	* ## Errors
	* If an error is returned, the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn read_until_or_end(&mut self, byte: u8, dest: &mut Vec<u8>) -> InfallibleFormatResult<ReadUntilStopReason, S> { self.0.read_until_or_end(byte, dest) }
}

/**
 * A [`Chisel`], with big-endian byte order.
 *
 * See the [crate documentation](crate) for more information.
 */
#[repr(transparent)]
pub struct ChiselBigEndian<'a, S : ChiselSource>(pub(crate) &'a mut Chisel<S>);

impl<'a, S : ChiselSource> ChiselBigEndian<'a, S> {
	/** Returns a reference to the inner `Chisel`. */
	#[inline]
	pub fn inner(&mut self) -> &mut Chisel<S> { self.0 }

	/** Consumes the wrapper, returning the input chisel reference. */
	#[inline]
	pub fn into_inner(self) -> &'a mut Chisel<S> { self.0 }

	/**
	* Returns a format error that occurred `backstep` bytes ago.
	*
	* This is a convenience method for manually determining the appropriate byte offset and calling [ChiselError::format].
	*/
	#[inline]
	pub fn error<E>(&self, err: E, backstep: usize) -> ChiselError<E, S::Error> { self.0.error(err, backstep) }

	/** Returns the current byte offset of the Chisel. */
	#[inline]
	pub fn offset(&self) -> usize { self.0.offset() }

	/**
	* Fills a provided buffer with bytes from the source.
	*
	* This operation is endian-independent.
	*
	* ## Errors
	* This function returns an [`ChiselErrorData::EndOfInput`] if the end of the input is encountered
	* before the buffer is completely filled.
	*
	* If an error is returned, the contents of `buf` are unspecified and the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn read_buf(&mut self, buf: &mut [u8]) -> InfallibleFormatResult<(), S> { self.0.read_buf(buf) }

	/**
	* Reads a single byte [`u8`].
	*
	* This operation is endian-independent.
	*/
	#[inline]
	pub fn u8(&mut self) -> InfallibleFormatResult<u8, S> { self.0.u8() }
	ty_fn_endian!(u16, Big, big);
	ty_fn_endian!(u32, Big, big);
	ty_fn_endian!(u64, Big, big);

	/**
	* Reads a single _signed_ byte [`i8`].
	*
	* This operation is endian-independent.
	*/
	#[inline]
	pub fn i8(&mut self) -> InfallibleFormatResult<i8, S> { self.0.i8() }
	ty_fn_endian!(i16, Big, big);
	ty_fn_endian!(i32, Big, big);
	ty_fn_endian!(i64, Big, big);

	ty_fn_endian!(f32, Big, big);
	ty_fn_endian!(f64, Big, big);

	/**
	* Skips `how_many` bytes of the underlying source.
	*
	* This operation is endian-independent.
	*
	* This is provided for optimization reasons. This hint is forwarded to the source.
	*
	* Exactly `how_many` bytes are skipped, as-if [`read_buf`][Self::read_buf] were called with a buffer
	* of appropriate size, with the buffer being discarded.
	*
	* ## Errors
	*
	* If an end-of-input condition occurs before `how_many` bytes are skipped, an [`ChiselErrorData::EndOfInput`] error is returned.
	*
	* If an error is returned, the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn skip(&mut self, how_many: usize) -> InfallibleFormatResult<(), S> { self.0.skip(how_many) }

	#[cfg(feature = "alloc")]
	/**
	* Reads bytes into the provided `dest` Vec until the `delimiter` byte is encountered.
	*
	* This operation is endian-independent.
	*
	* ## Errors
	* If the end of the input is encountered before the delimiter is found, [`ChiselErrorData::EndOfInput`] is returned.
	*
	* If an error is returned, the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn read_until(&mut self, byte: u8, dest: &mut Vec<u8>) -> InfallibleFormatResult<(), S> { self.0.read_until(byte, dest) }

	#[cfg(feature = "alloc")]
	/**
	* Reads bytes into the provided `dest` Vec until the `delimiter` byte or end-of-input is encountered.
	*
	* This operation is endian-independent.
	* ## Errors
	* If an error is returned, the chisel [breaks][crate#breaking].
	*/
	#[inline]
	pub fn read_until_or_end(&mut self, byte: u8, dest: &mut Vec<u8>) -> InfallibleFormatResult<ReadUntilStopReason, S> { self.0.read_until_or_end(byte, dest) }
}