Skip to content

Commit

Permalink
bam/record/data/field/value: Add byte reader
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Dec 14, 2024
1 parent 5f1b103 commit ed92418
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions noodles-bam/src/record/data/field/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use self::array::decode_array;

pub(crate) fn decode_value<'a>(src: &mut &'a [u8], ty: Type) -> io::Result<Value<'a>> {
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),
Expand All @@ -26,18 +26,6 @@ pub(crate) fn decode_value<'a>(src: &mut &'a [u8], ty: Type) -> io::Result<Value
}
}

fn decode_character<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> {
src.read_u8().map(Value::Character)
}

fn decode_i8<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> {
src.read_i8().map(Value::Int8)
}

fn decode_u8<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> {
src.read_u8().map(Value::UInt8)
}

fn decode_i16<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> {
src.read_i16::<LittleEndian>().map(Value::Int16)
}
Expand Down Expand Up @@ -77,6 +65,16 @@ fn decode_hex<'a>(src: &mut &'a [u8]) -> io::Result<Value<'a>> {
decode_string(src).map(Value::Hex)
}

fn read_u8(src: &mut &[u8]) -> io::Result<u8> {
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;
Expand Down

0 comments on commit ed92418

Please sign in to comment.