use core::marker::PhantomData;
use std::collections::TryReserveError;
use std::io::Result;
use std::io::SeekFrom;
use qubit_codec::TranscodeDecodeInput;
use qubit_codec_binary::Leb128Codec;
use qubit_codec_binary::Leb128DecodePolicy;
use qubit_codec_binary::NonStrict;
use qubit_codec_binary::Strict;
use qubit_io::Buffer;
use qubit_io::ByteInput;
use qubit_io::Input;
use qubit_io::Seekable;
use qubit_io::SeekableByteInput;
use super::internal::TranscodeDecodeInputExt;
use crate::util::MIN_CODEC_BUFFER_CAPACITY;
use crate::util::read_utf8_payload;
#[cfg(not(target_pointer_width = "64"))]
use crate::util::usize_from_u64_len;
pub struct BufferedLeb128Reader<R, P>
where
R: ByteInput,
{
input: TranscodeDecodeInput<R>,
marker: PhantomData<fn() -> P>,
}
impl_input_methods!(BufferedLeb128Reader<R, P> where R: ByteInput);
impl_seekable_methods!(BufferedLeb128Reader<R, P> where R: SeekableByteInput);
impl<R, P> BufferedLeb128Reader<R, P>
where
R: ByteInput,
P: Leb128DecodePolicy,
{
#[must_use]
#[inline]
pub fn new(inner: R) -> Self {
Self {
input: TranscodeDecodeInput::new(inner),
marker: PhantomData,
}
}
#[must_use]
#[inline]
pub fn with_capacity(inner: R, capacity: usize) -> Self {
Self {
input: TranscodeDecodeInput::with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY)),
marker: PhantomData,
}
}
#[inline]
pub fn try_with_capacity(inner: R, capacity: usize) -> std::result::Result<Self, TryReserveError> {
Ok(Self {
input: TranscodeDecodeInput::try_with_capacity(inner, capacity.max(MIN_CODEC_BUFFER_CAPACITY))?,
marker: PhantomData,
})
}
#[must_use]
#[inline(always)]
pub const fn is_strict(&self) -> bool {
P::STRICT
}
#[must_use]
#[inline(always)]
pub const fn inner(&self) -> &R {
self.input.inner()
}
#[inline(always)]
#[must_use = "the returned inner reader and unread buffer must be handled"]
pub fn into_parts(self) -> (R, Buffer<u8>) {
self.input.into_parts()
}
}
macro_rules! impl_read_value {
($policy:ty, $method:ident, $ty:ty, $doc:literal) => {
#[doc = $doc]
#[doc = ""]
#[doc = "# Returns"]
#[doc = ""]
#[doc = concat!("Returns the decoded `", stringify!($ty), "`.")]
#[doc = ""]
#[doc = "# Errors"]
#[doc = ""]
#[doc = "Returns an input error or an invalid-data error when the \
payload is malformed or violates the selected policy."]
#[inline(always)]
pub fn $method(&mut self) -> Result<$ty> {
type Codec = Leb128Codec<$ty, $policy>;
self.input.read_decoded::<Codec>()
}
};
}
macro_rules! impl_for_policy {
(
$policy:ty,
$read_usize:ident,
$read_u64:ident,
$read_utf8_string_usize:ident,
$read_utf8_string_u64:ident,
{
$(($method:ident, $ty:ty, $doc:literal)),* $(,)?
}
$(,)?
) => {
impl<R> BufferedLeb128Reader<R, $policy>
where
R: ByteInput,
{
$(
impl_read_value!($policy, $method, $ty, $doc);
)*
#[inline]
pub fn $read_utf8_string_usize(
&mut self,
max_len: usize,
) -> Result<String> {
let len = self.$read_usize()?;
read_utf8_payload(&mut self.input, len, max_len)
}
#[inline]
pub fn $read_utf8_string_u64(
&mut self,
max_len: usize,
) -> Result<String> {
let len = self.$read_u64()?;
#[cfg(target_pointer_width = "64")]
let len = len as usize;
#[cfg(not(target_pointer_width = "64"))]
let len = usize_from_u64_len(len)?;
read_utf8_payload(&mut self.input, len, max_len)
}
}
};
}
impl_for_policy!(
NonStrict,
read_usize_non_strict,
read_u64_non_strict,
read_utf8_string_usize_non_strict,
read_utf8_string_u64_non_strict,
{
(read_u8_non_strict, u8, "Reads an unsigned LEB128 u8."),
(read_u16_non_strict, u16, "Reads an unsigned LEB128 u16."),
(read_u32_non_strict, u32, "Reads an unsigned LEB128 u32."),
(read_u64_non_strict, u64, "Reads an unsigned LEB128 u64."),
(read_u128_non_strict, u128, "Reads an unsigned LEB128 u128."),
(read_usize_non_strict, usize, "Reads an unsigned LEB128 usize."),
(read_i8_non_strict, i8, "Reads a signed LEB128 i8."),
(read_i16_non_strict, i16, "Reads a signed LEB128 i16."),
(read_i32_non_strict, i32, "Reads a signed LEB128 i32."),
(read_i64_non_strict, i64, "Reads a signed LEB128 i64."),
(read_i128_non_strict, i128, "Reads a signed LEB128 i128."),
(read_isize_non_strict, isize, "Reads a signed LEB128 isize."),
},
);
impl_for_policy!(
Strict,
read_usize,
read_u64,
read_utf8_string_usize,
read_utf8_string_u64,
{
(read_u8, u8, "Reads an unsigned LEB128 u8."),
(read_u16, u16, "Reads an unsigned LEB128 u16."),
(read_u32, u32, "Reads an unsigned LEB128 u32."),
(read_u64, u64, "Reads an unsigned LEB128 u64."),
(read_u128, u128, "Reads an unsigned LEB128 u128."),
(read_usize, usize, "Reads an unsigned LEB128 usize."),
(read_i8, i8, "Reads a signed LEB128 i8."),
(read_i16, i16, "Reads a signed LEB128 i16."),
(read_i32, i32, "Reads a signed LEB128 i32."),
(read_i64, i64, "Reads a signed LEB128 i64."),
(read_i128, i128, "Reads a signed LEB128 i128."),
(read_isize, isize, "Reads a signed LEB128 isize."),
},
);
impl<R, P> Input for BufferedLeb128Reader<R, P>
where
R: ByteInput,
{
type Item = u8;
#[inline(always)]
fn is_buffered(&self) -> bool {
true
}
#[inline(always)]
unsafe fn read_unchecked(&mut self, output: &mut [u8], index: usize, count: usize) -> Result<usize> {
unsafe { self.input.read_unchecked(output, index, count) }
}
}
impl<R, P> Seekable for BufferedLeb128Reader<R, P>
where
R: SeekableByteInput,
{
type Unit = u8;
#[inline(always)]
fn seek_to(&mut self, position: SeekFrom) -> Result<u64> {
self.input.seek(position)
}
}