use core::{
fmt::{
self,
Display,
Formatter,
},
num::NonZeroUsize,
};
use super::internal::leb128_decode_error_details::Leb128DecodeErrorDetails;
use crate::Leb128DecodeErrorKind;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct Leb128DecodeError {
start_index: usize,
error_index: usize,
details: Leb128DecodeErrorDetails,
}
impl Leb128DecodeError {
pub const fn incomplete(
start_index: usize,
required: NonZeroUsize,
available: usize,
) -> Self {
assert!(
required.get() > available,
"incomplete LEB128 required bytes must exceed available bytes",
);
Self {
start_index,
error_index: add_offset(start_index, available),
details: Leb128DecodeErrorDetails::Incomplete {
required,
available,
},
}
}
pub const fn malformed(
start_index: usize,
error_index: usize,
consumed: NonZeroUsize,
) -> Self {
assert_error_index_in_consumed_span(start_index, error_index, consumed);
Self {
start_index,
error_index,
details: Leb128DecodeErrorDetails::Invalid {
kind: Leb128DecodeErrorKind::Malformed,
consumed,
},
}
}
pub const fn noncanonical(
start_index: usize,
consumed: NonZeroUsize,
) -> Self {
Self {
start_index,
error_index: last_consumed_index(start_index, consumed),
details: Leb128DecodeErrorDetails::Invalid {
kind: Leb128DecodeErrorKind::NonCanonical,
consumed,
},
}
}
#[must_use]
#[inline(always)]
pub const fn kind(self) -> Leb128DecodeErrorKind {
match self.details {
Leb128DecodeErrorDetails::Incomplete { .. } => {
Leb128DecodeErrorKind::Incomplete
}
Leb128DecodeErrorDetails::Invalid { kind, .. } => kind,
}
}
#[must_use]
#[inline(always)]
pub const fn start_index(self) -> usize {
self.start_index
}
#[must_use]
#[inline(always)]
pub const fn error_index(self) -> usize {
self.error_index
}
#[must_use]
#[inline(always)]
pub const fn consumed(self) -> Option<NonZeroUsize> {
match self.details {
Leb128DecodeErrorDetails::Incomplete { .. } => None,
Leb128DecodeErrorDetails::Invalid { consumed, .. } => {
Some(consumed)
}
}
}
#[must_use]
#[inline(always)]
pub const fn is_incomplete(self) -> bool {
matches!(self.details, Leb128DecodeErrorDetails::Incomplete { .. })
}
#[must_use]
#[inline(always)]
pub const fn is_malformed(self) -> bool {
matches!(
self.details,
Leb128DecodeErrorDetails::Invalid {
kind: Leb128DecodeErrorKind::Malformed,
..
}
)
}
#[must_use]
#[inline(always)]
pub const fn is_noncanonical(self) -> bool {
matches!(
self.details,
Leb128DecodeErrorDetails::Invalid {
kind: Leb128DecodeErrorKind::NonCanonical,
..
}
)
}
#[must_use]
#[inline(always)]
pub const fn required(self) -> Option<NonZeroUsize> {
match self.details {
Leb128DecodeErrorDetails::Incomplete { required, .. } => {
Some(required)
}
Leb128DecodeErrorDetails::Invalid { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn additional(self) -> Option<NonZeroUsize> {
match self.details {
Leb128DecodeErrorDetails::Incomplete {
required,
available,
} => {
let additional = required.get() - available;
Some(qubit_utils::nonzero(additional))
}
Leb128DecodeErrorDetails::Invalid { .. } => None,
}
}
#[must_use]
#[inline(always)]
pub const fn available(self) -> Option<usize> {
match self.details {
Leb128DecodeErrorDetails::Incomplete { available, .. } => {
Some(available)
}
Leb128DecodeErrorDetails::Invalid { .. } => None,
}
}
}
impl Display for Leb128DecodeError {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
match self.details {
Leb128DecodeErrorDetails::Incomplete {
required,
available,
} => {
write!(
formatter,
"{} at byte {}: need at least {} bytes, only {} available (next byte boundary {})",
Leb128DecodeErrorKind::Incomplete,
self.start_index,
required,
available,
self.error_index,
)
}
Leb128DecodeErrorDetails::Invalid { kind, consumed } => {
write!(
formatter,
"{} at byte {}: detected at byte {} after consuming {} bytes",
kind, self.start_index, self.error_index, consumed,
)
}
}
}
}
impl std::error::Error for Leb128DecodeError {}
const fn add_offset(index: usize, offset: usize) -> usize {
match index.checked_add(offset) {
Some(value) => value,
None => panic!("LEB128 byte index overflow"),
}
}
const fn last_consumed_index(
start_index: usize,
consumed: NonZeroUsize,
) -> usize {
add_offset(start_index, consumed.get() - 1)
}
const fn assert_error_index_in_consumed_span(
start_index: usize,
error_index: usize,
consumed: NonZeroUsize,
) {
let last_index = last_consumed_index(start_index, consumed);
assert!(
error_index >= start_index,
"LEB128 error index must not precede value start",
);
assert!(
error_index <= last_index,
"LEB128 error index must lie inside consumed input",
);
}