use crate::beve::reader::Reader;
use crate::beve::traits::Read;
use crate::options::Options;
use super::StreamError;
use super::split::Splitter;
pub(crate) type Window = crate::stream::Window<Splitter>;
pub(crate) fn read<'a, O: Options, T: Read<'a> + Default>(
win: &'a Window,
span: (usize, usize),
) -> Result<T, StreamError> {
let mut value = T::default();
read_into::<O, T>(win, span, &mut value)?;
Ok(value)
}
pub(crate) fn read_into<'a, O: Options, T: Read<'a>>(
win: &'a Window,
(start, end): (usize, usize),
value: &mut T,
) -> Result<(), StreamError> {
let bytes = &win.bytes()[start..end];
let mut r = match win.framer().implied() {
Some(h) => Reader::<O>::with_implied(bytes, h),
None => Reader::<O>::with_options(bytes),
};
match value.read(&mut r).and_then(|()| r.finish()) {
Ok(()) => Ok(()),
Err(code) => Err(win.error_at(code, start + r.position(), r.error_key())),
}
}