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
86
87
88
89
90
91
use bam::HeaderView;
pub struct Header {
records: Vec<Vec<u8>>
}
impl Header {
pub fn new() -> Self {
Header { records: Vec::new() }
}
pub fn from_template(header: &HeaderView) -> Self {
let mut record = header.as_bytes().to_owned();
while let Some(&last_char) = record.last() {
if last_char==b'\n' {
record.pop();
} else {
break;
}
}
Header { records: vec![record] }
}
pub fn push_record(&mut self, record: &HeaderRecord) -> &mut Self {
self.records.push(record.to_bytes());
self
}
pub fn push_comment(&mut self, comment: &[u8]) -> &mut Self {
self.records.push([&b"@CO"[..], comment].join(&b'\t'));
self
}
pub fn to_bytes(&self) -> Vec<u8> {
self.records.join(&b'\n')
}
}
pub struct HeaderRecord<'a> {
rec_type: Vec<u8>,
tags: Vec<(&'a [u8], Vec<u8>)>,
}
impl<'a> HeaderRecord<'a> {
pub fn new(rec_type: &'a [u8]) -> Self {
HeaderRecord { rec_type: [&b"@"[..], rec_type].concat(), tags: Vec::new() }
}
pub fn push_tag<V: ToString>(&mut self, tag: &'a [u8], value: &V) -> &mut Self {
self.tags.push((tag, value.to_string().into_bytes()));
self
}
fn to_bytes(&self) -> Vec<u8> {
let mut out = Vec::new();
out.extend(self.rec_type.iter());
for &(tag, ref value) in self.tags.iter() {
out.push(b'\t');
out.extend(tag.iter());
out.push(b':');
out.extend(value.iter());
}
out
}
}