noodles_vcf/record/samples/
keys.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
use std::iter;

/// VCF record samples keys.
#[derive(Debug, Eq, PartialEq)]
pub struct Keys<'a>(&'a str);

impl<'a> Keys<'a> {
    pub(super) fn new(src: &'a str) -> Self {
        Self(src)
    }

    /// Returns an iterator over keys.
    pub fn iter(&self) -> impl Iterator<Item = &'a str> + 'a {
        let mut src = self.0;

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

fn parse_key<'a>(src: &mut &'a str) -> &'a str {
    const DELIMITER: char = ':';

    let (buf, rest) = src
        .split_once(DELIMITER)
        .unwrap_or_else(|| src.split_at(src.len()));

    *src = rest;

    buf
}

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

    #[test]
    fn test_iter() {
        let genotypes = Keys::new("");
        assert!(genotypes.iter().next().is_none());

        let keys = Keys::new("GT:GQ");
        let actual: Vec<_> = keys.iter().collect();
        let expected = ["GT", "GQ"];
        assert_eq!(actual, expected);
    }
}