noodles_vcf/record/
alternate_bases.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
79
80
81
82
83
84
85
use std::{fmt, io, iter};

use crate::variant::record::AlternateBases as _;

/// VCF record alternate bases.
pub struct AlternateBases<'a>(&'a str);

const DELIMITER: char = ',';

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

impl<'a> fmt::Debug for AlternateBases<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_list().entries(self.iter()).finish()
    }
}

impl<'a> AsRef<str> for AlternateBases<'a> {
    fn as_ref(&self) -> &str {
        self.0
    }
}

impl<'a> crate::variant::record::AlternateBases for AlternateBases<'a> {
    fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    fn len(&self) -> usize {
        if self.is_empty() {
            0
        } else {
            self.iter().count()
        }
    }

    fn iter(&self) -> Box<dyn Iterator<Item = io::Result<&str>> + '_> {
        if self.is_empty() {
            Box::new(iter::empty())
        } else {
            Box::new(self.0.split(DELIMITER).map(Ok))
        }
    }
}

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

    #[test]
    fn test_is_empty() {
        assert!(AlternateBases::new("").is_empty());
        assert!(!AlternateBases::new("A").is_empty());
        assert!(!AlternateBases::new("A,C").is_empty());
    }

    #[test]
    fn test_len() {
        assert_eq!(AlternateBases::new("").len(), 0);
        assert_eq!(AlternateBases::new("A").len(), 1);
        assert_eq!(AlternateBases::new("A,C").len(), 2);
    }

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

        let alternate_bases = AlternateBases::new("A");
        let actual: Vec<_> = alternate_bases.iter().collect::<io::Result<_>>()?;
        let expected = ["A"];
        assert_eq!(actual, expected);

        let alternate_bases = AlternateBases::new("A,C");
        let actual: Vec<_> = alternate_bases.iter().collect::<io::Result<_>>()?;
        let expected = ["A", "C"];
        assert_eq!(actual, expected);

        Ok(())
    }
}