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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#![warn(missing_docs)]
#[cfg(feature = "async")]
mod r#async;
mod block;
mod gz;
mod reader;
pub mod virtual_position;
pub mod writer;
pub use self::{reader::Reader, virtual_position::VirtualPosition, writer::Writer};
#[cfg(feature = "async")]
pub use self::r#async::{Reader as AsyncReader, Writer as AsyncWriter};
use self::block::Block;
const GZIP_XLEN_SIZE: usize = 2;
const BGZF_XLEN: usize = 6;
pub(crate) const BGZF_HEADER_SIZE: usize = gz::HEADER_SIZE + GZIP_XLEN_SIZE + BGZF_XLEN;
#[cfg(test)]
mod tests {
use std::io::{self, BufRead, Read, Write};
use super::*;
#[test]
fn test_self() -> io::Result<()> {
let mut writer = Writer::new(Vec::new());
writer.write_all(b"noodles")?;
writer.flush()?;
writer.write_all(b"-")?;
writer.flush()?;
writer.write_all(b"bgzf")?;
let data = writer.finish()?;
let mut reader = Reader::new(&data[..]);
let mut buf = Vec::new();
reader.read_to_end(&mut buf)?;
assert_eq!(buf, b"noodles-bgzf");
Ok(())
}
#[test]
fn test_self_buffered() -> io::Result<()> {
let mut writer = Writer::new(Vec::new());
writer.write_all(b"noodles\n-\nbgzf\nbuffered")?;
let data = writer.finish()?;
let mut reader = Reader::new(&data[..]);
let mut lines = Vec::new();
let mut virtual_positions = Vec::new();
loop {
virtual_positions.push(reader.virtual_position());
let mut line = String::new();
match reader.read_line(&mut line) {
Ok(0) => {
virtual_positions.pop();
break;
}
Err(e) => return Err(e),
_ => (),
}
lines.push(line);
}
let expected_lines = vec!["noodles\n", "-\n", "bgzf\n", "buffered"];
assert_eq!(lines, expected_lines);
let expected_upos = vec![0, 8, 10, 15];
let expected_virtual_positions: Vec<VirtualPosition> = expected_upos
.iter()
.map(|x| VirtualPosition::try_from((0, *x)).unwrap())
.collect();
assert_eq!(virtual_positions, expected_virtual_positions);
Ok(())
}
}