From ed92418d14e23635b814d4386d59edfa575c64ee Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Sat, 14 Dec 2024 15:21:23 -0600 Subject: [PATCH] bam/record/data/field/value: Add byte reader --- noodles-bam/src/record/data/field/value.rs | 28 ++++++++++------------ 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/noodles-bam/src/record/data/field/value.rs b/noodles-bam/src/record/data/field/value.rs index 3f95533d4..268d439a6 100644 --- a/noodles-bam/src/record/data/field/value.rs +++ b/noodles-bam/src/record/data/field/value.rs @@ -12,9 +12,9 @@ use self::array::decode_array; pub(crate) fn decode_value<'a>(src: &mut &'a [u8], ty: Type) -> io::Result> { match ty { - Type::Character => decode_character(src), - Type::Int8 => decode_i8(src), - Type::UInt8 => decode_u8(src), + Type::Character => read_u8(src).map(Value::Character), + Type::Int8 => read_u8(src).map(|n| Value::Int8(n as i8)), + Type::UInt8 => read_u8(src).map(Value::UInt8), Type::Int16 => decode_i16(src), Type::UInt16 => decode_u16(src), Type::Int32 => decode_i32(src), @@ -26,18 +26,6 @@ pub(crate) fn decode_value<'a>(src: &mut &'a [u8], ty: Type) -> io::Result(src: &mut &'a [u8]) -> io::Result> { - src.read_u8().map(Value::Character) -} - -fn decode_i8<'a>(src: &mut &'a [u8]) -> io::Result> { - src.read_i8().map(Value::Int8) -} - -fn decode_u8<'a>(src: &mut &'a [u8]) -> io::Result> { - src.read_u8().map(Value::UInt8) -} - fn decode_i16<'a>(src: &mut &'a [u8]) -> io::Result> { src.read_i16::().map(Value::Int16) } @@ -77,6 +65,16 @@ fn decode_hex<'a>(src: &mut &'a [u8]) -> io::Result> { decode_string(src).map(Value::Hex) } +fn read_u8(src: &mut &[u8]) -> io::Result { + let Some((n, rest)) = src.split_first() else { + return Err(io::Error::from(io::ErrorKind::UnexpectedEof)); + }; + + *src = rest; + + Ok(*n) +} + #[cfg(test)] mod tests { use noodles_sam::alignment::record::data::field::value::Array;