1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//! BAM record data field.

mod tag;
mod ty;
pub mod value;

use std::io;

use noodles_sam::alignment::record::data::field::{Tag, Value};

pub(crate) use self::{tag::decode_tag, ty::decode_type, value::decode_value};

pub(super) fn decode_field<'a>(src: &mut &'a [u8]) -> io::Result<(Tag, Value<'a>)> {
    let tag = decode_tag(src)?;

    let ty = decode_type(src)?;
    let value = decode_value(src, ty)?;

    Ok((tag, value))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_decode_field() -> io::Result<()> {
        let mut src = &[b'N', b'H', b'C', 0x01][..];
        assert!(matches!(
            decode_field(&mut src)?,
            (Tag::ALIGNMENT_HIT_COUNT, Value::UInt8(1))
        ));

        let mut src = &[][..];
        assert!(matches!(
            decode_field(&mut src),
            Err(e) if e.kind() == io::ErrorKind::UnexpectedEof
        ));

        Ok(())
    }
}