use crate::decode::{__read_marker, read_arg};
use crate::{Error, Marker, ReadBuffer, ReadBufferBorrowed};

/// Decode a `f32` value.
#[inline(always)]
pub fn read_f32(buf: &mut ReadBuffer) -> Result<f32, Error> {
    __read_f32(&buf.borrow())
}

/// Decode a `f64` value.
#[inline(always)]
pub fn read_f64(buf: &mut ReadBuffer) -> Result<f64, Error> {
    __read_f64(&buf.borrow())
}

pub(crate) fn __read_f32(buf: &ReadBufferBorrowed) -> Result<f32, Error> {
    let snapshot = buf.snapshot();
    match __read_marker(buf)? {
        Marker::F32 => read_arg!(buf as f32),
        _ => {
            // Undo the read_marker call.
            buf.restore(snapshot);

            Err(Error::Unexpected)
        }
    }
}

pub(crate) fn __read_f64(buf: &ReadBufferBorrowed) -> Result<f64, Error> {
    let snapshot = buf.snapshot();
    match __read_marker(buf)? {
        Marker::F64 => read_arg!(buf as f64),
        _ => {
            // Undo the read_marker call.
            buf.restore(snapshot);

            Err(Error::Unexpected)
        }
    }
}
