noodles_vcf/record/
ids.rs

1use std::iter;
2
3/// Raw VCF record IDs.
4#[derive(Debug, Eq, PartialEq)]
5pub struct Ids<'a>(&'a str);
6
7impl<'a> Ids<'a> {
8    pub(super) fn new(buf: &'a str) -> Self {
9        Self(buf)
10    }
11}
12
13impl AsRef<str> for Ids<'_> {
14    fn as_ref(&self) -> &str {
15        self.0
16    }
17}
18
19impl crate::variant::record::Ids for Ids<'_> {
20    fn is_empty(&self) -> bool {
21        self.0.is_empty()
22    }
23
24    fn len(&self) -> usize {
25        self.iter().count()
26    }
27
28    fn iter(&self) -> Box<dyn Iterator<Item = &str> + '_> {
29        const DELIMITER: char = ';';
30
31        if self.is_empty() {
32            return Box::new(iter::empty());
33        }
34
35        Box::new(self.0.split(DELIMITER))
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::variant::record::Ids as _;
43
44    #[test]
45    fn test_is_empty() {
46        assert!(Ids::new("").is_empty());
47        assert!(!Ids::new("nd0").is_empty());
48        assert!(!Ids::new("nd0;nd1").is_empty());
49    }
50
51    #[test]
52    fn test_len() {
53        assert_eq!(Ids::new("").len(), 0);
54        assert_eq!(Ids::new("nd0").len(), 1);
55        assert_eq!(Ids::new("nd0;nd1").len(), 2);
56    }
57
58    #[test]
59    fn test_iter() {
60        let ids = Ids::new("");
61        assert!(ids.iter().next().is_none());
62
63        let ids = Ids::new("nd0;nd1");
64        let actual: Vec<_> = ids.iter().collect();
65        let expected = ["nd0", "nd1"];
66        assert_eq!(actual, expected);
67    }
68}