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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! Raw BAM record data.

pub mod field;

use std::{borrow::Borrow, io, iter};

use noodles_sam as sam;

use self::field::{decode_field, Tag, Value};

/// Raw BAM record data.
#[derive(Debug, Eq, PartialEq)]
pub struct Data<'a>(&'a [u8]);

impl<'a> Data<'a> {
    pub(super) fn new(src: &'a [u8]) -> Self {
        Self(src)
    }

    /// Returns whether there are any fields.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Returns the value of the given tag.
    pub fn get<K>(&self, tag: &K) -> Option<io::Result<Value<'_>>>
    where
        K: Borrow<[u8; 2]>,
    {
        for result in self.iter() {
            match result {
                Ok((t, value)) => {
                    if &t == tag.borrow() {
                        return Some(Ok(value));
                    }
                }
                Err(e) => return Some(Err(e)),
            };
        }

        None
    }

    /// Returns an iterator over all tag-value pairs.
    pub fn iter(&self) -> impl Iterator<Item = io::Result<(Tag, Value<'_>)>> + '_ {
        let mut src = self.0;

        iter::from_fn(move || {
            if src.is_empty() {
                None
            } else {
                Some(decode_field(&mut src))
            }
        })
    }
}

impl<'a> AsRef<[u8]> for Data<'a> {
    fn as_ref(&self) -> &[u8] {
        self.0
    }
}

impl<'a> TryFrom<Data<'a>> for sam::record::Data {
    type Error = io::Error;

    fn try_from(bam_data: Data<'a>) -> Result<Self, Self::Error> {
        use crate::record::codec::decoder::get_data;

        let mut src = bam_data.0;
        let mut sam_data = Self::default();
        get_data(&mut src, &mut sam_data)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;

        Ok(sam_data)
    }
}

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

    #[test]
    fn test_get() -> io::Result<()> {
        use sam::record::data::field::tag;

        let data = Data::new(&[b'N', b'H', b'C', 0x01]);

        assert!(data.get(&tag::ALIGNMENT_HIT_COUNT).is_some());
        assert!(data.get(&[b'N', b'H']).is_some());

        assert!(data.get(&tag::COMMENT).is_none());

        Ok(())
    }

    #[test]
    fn test_iter() -> io::Result<()> {
        let data = Data::new(&[]);
        assert!(data.iter().next().is_none());

        let data = Data::new(&[b'N', b'H', b'C', 0x01]);
        let actual: Vec<_> = data.iter().collect::<io::Result<_>>()?;
        let expected = [([b'N', b'H'], Value::UInt8(1))];
        assert_eq!(actual, expected);

        Ok(())
    }
}