noodles_bcf/record/
info.rs

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
mod field;

use std::io;

use noodles_vcf::{
    self as vcf,
    variant::record::{info::field::Value, Info as _},
};

use self::field::read_field;

/// BCF record info.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct Info<'r> {
    src: &'r [u8],
    field_count: usize,
}

impl<'r> Info<'r> {
    pub(super) fn new(src: &'r [u8], field_count: usize) -> Self {
        Self { src, field_count }
    }

    /// Returns the value with the given key.
    pub fn get<'h: 'r>(
        &'r self,
        header: &'h vcf::Header,
        key: &str,
    ) -> Option<io::Result<Option<Value<'r>>>> {
        for result in self.iter(header) {
            match result {
                Ok((k, v)) => {
                    if k == key {
                        return Some(Ok(v));
                    }
                }
                Err(e) => return Some(Err(e)),
            }
        }

        None
    }
}

impl<'r> AsRef<[u8]> for Info<'r> {
    fn as_ref(&self) -> &[u8] {
        self.src
    }
}

impl<'r> vcf::variant::record::Info for Info<'r> {
    fn is_empty(&self) -> bool {
        self.len() == 0
    }

    fn len(&self) -> usize {
        self.field_count
    }

    fn get<'a, 'h: 'a>(
        &'a self,
        header: &'h vcf::Header,
        key: &str,
    ) -> Option<io::Result<Option<Value<'a>>>> {
        self.get(header, key)
    }

    fn iter<'a, 'h: 'a>(
        &'a self,
        header: &'h vcf::Header,
    ) -> Box<dyn Iterator<Item = io::Result<(&'a str, Option<Value<'a>>)>> + 'a> {
        let mut src = self.as_ref();

        Box::new((0..self.len()).map(move |_| {
            read_field(&mut src, header).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
        }))
    }
}