#[cfg(feature = "std")]
use std::io;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "std", derive(thiserror::Error))]
pub enum ParseError {
#[cfg_attr(
feature = "std",
error("invalid character {byte:?} at position {position}")
)]
InvalidChar { byte: u8, position: usize },
#[cfg_attr(
feature = "std",
error("wrong string length (expected {expected}, got {got})")
)]
WrongLength { expected: u8, got: usize },
}
impl ParseError {
pub const fn valid_up_to(&self) -> usize {
match *self {
Self::InvalidChar { position, .. } => position,
Self::WrongLength { expected, got } if got > (expected as _) => expected as _,
Self::WrongLength { got, .. } => got,
}
}
}
#[cfg(feature = "std")]
impl From<ParseError> for io::Error {
fn from(error: ParseError) -> Self {
Self::new(io::ErrorKind::InvalidData, error)
}
}
#[cfg(any(
target_pointer_width = "32",
target_pointer_width = "64",
target_pointer_width = "128"
))]
const _: () =
assert!(core::mem::size_of::<Option<ParseError>>() <= core::mem::size_of::<usize>() * 2);