use crate::{Error, Marker, ReadBuffer, ReadBufferBorrowed};

/// Read and decode the next marker.
#[inline(always)]
pub fn read_marker(buf: &mut ReadBuffer) -> Result<Marker, Error> {
    __read_marker(&buf.borrow())
}

/// Peek and decode the next marker.
#[inline(always)]
pub fn peek_marker(buf: &ReadBuffer) -> Result<Marker, Error> {
    __peek_marker(&buf.borrow())
}

pub(crate) fn __read_marker(buf: &ReadBufferBorrowed) -> Result<Marker, Error> {
    buf.read(1, |buf| {
        // Safety: 1 byte available in buf.
        unsafe { *buf.get_unchecked(0) }
    })
    .and_then(Marker::decode)
}

pub(crate) fn __peek_marker(buf: &ReadBufferBorrowed) -> Result<Marker, Error> {
    buf.peek(1, |buf| {
        // Safety: 1 byte available in buf.
        unsafe { *buf.get_unchecked(0) }
    })
    .and_then(Marker::decode)
}
