use crate::cursor::bounded_len;
use super::error::SourceLocation;
use super::probe::{ParseError, ParseErrorKind};
use super::space::SpaceId;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BoundedCount(usize);
impl BoundedCount {
pub fn get(self) -> usize {
self.0
}
}
#[derive(Debug, Clone, Copy)]
pub struct View<'a> {
bytes: &'a [u8],
space: SpaceId,
start: usize,
end: usize,
position: usize,
}
impl<'a> View<'a> {
pub(crate) fn over_space(bytes: &'a [u8], space: SpaceId) -> View<'a> {
View {
bytes,
space,
start: 0,
end: bytes.len(),
position: 0,
}
}
pub(crate) fn space(self) -> SpaceId {
self.space
}
pub fn position(self) -> usize {
self.position
}
pub fn start(self) -> usize {
self.start
}
pub fn end(self) -> usize {
self.end
}
pub fn remaining(self) -> usize {
self.end.saturating_sub(self.position)
}
pub fn location(self) -> SourceLocation {
SourceLocation {
space: self.space,
offset: self.position as u64,
}
}
pub fn window(self) -> &'a [u8] {
self.bytes.get(self.start..self.end).unwrap_or_default()
}
pub fn take(&mut self, count: usize) -> Option<&'a [u8]> {
let end = self.position.checked_add(count)?;
if end > self.end {
return None;
}
let bytes = self.bytes.get(self.position..end)?;
self.position = end;
Some(bytes)
}
pub fn array<const N: usize>(&mut self) -> Option<[u8; N]> {
self.take(N)?.try_into().ok()
}
pub fn skip(&mut self, count: usize) -> Option<()> {
self.take(count).map(|_| ())
}
pub fn seek(&mut self, position: usize) -> Option<()> {
(self.start <= position && position <= self.end).then(|| self.position = position)
}
pub fn u8(&mut self) -> Option<u8> {
self.array::<1>().map(|[value]| value)
}
pub fn child(self, start: usize, end: usize) -> Option<View<'a>> {
if self.start <= start && start <= end && end <= self.end {
Some(View {
bytes: self.bytes,
space: self.space,
start,
end,
position: start,
})
} else {
None
}
}
pub fn counted(self, count: u64, min_element_size: usize) -> Option<BoundedCount> {
bounded_len(count, min_element_size, self.remaining()).map(BoundedCount)
}
fn eof(self, needed: u64) -> ParseError {
ParseError {
location: self.location(),
kind: ParseErrorKind::UnexpectedEof {
needed,
remaining: self.remaining() as u64,
},
}
}
pub fn req_take(&mut self, count: usize) -> Result<&'a [u8], ParseError> {
match self.take(count) {
Some(bytes) => Ok(bytes),
None => Err(self.eof(count as u64)),
}
}
pub fn req_u8(&mut self) -> Result<u8, ParseError> {
match self.u8() {
Some(value) => Ok(value),
None => Err(self.eof(1)),
}
}
}
macro_rules! view_readers {
($(($probe:ident, $req:ident, $ty:ty, $conv:ident, $size:literal)),* $(,)?) => {
impl View<'_> {
$(
#[doc = concat!("Probe read of a `", stringify!($ty), "` via `", stringify!($conv), "`.")]
pub fn $probe(&mut self) -> Option<$ty> {
self.array::<$size>().map(<$ty>::$conv)
}
#[doc = concat!("Required-read mirror of [`View::", stringify!($probe), "`].")]
pub fn $req(&mut self) -> Result<$ty, ParseError> {
match self.array::<$size>() {
Some(bytes) => Ok(<$ty>::$conv(bytes)),
None => Err(self.eof($size)),
}
}
)*
}
};
}
view_readers!(
(u16_le, req_u16_le, u16, from_le_bytes, 2),
(i16_le, req_i16_le, i16, from_le_bytes, 2),
(u32_le, req_u32_le, u32, from_le_bytes, 4),
(i32_le, req_i32_le, i32, from_le_bytes, 4),
(u64_le, req_u64_le, u64, from_le_bytes, 8),
(i64_le, req_i64_le, i64, from_le_bytes, 8),
(f32_le, req_f32_le, f32, from_le_bytes, 4),
(f64_le, req_f64_le, f64, from_le_bytes, 8),
(u16_be, req_u16_be, u16, from_be_bytes, 2),
(u32_be, req_u32_be, u32, from_be_bytes, 4),
(u64_be, req_u64_be, u64, from_be_bytes, 8),
(f64_be, req_f64_be, f64, from_be_bytes, 8),
);