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