noodles_bgzf/gzi/io/reader.rs
1mod index;
2
3use std::io::{self, Read};
4
5use self::index::read_index;
6use crate::gzi::Index;
7
8/// A gzip index (GZI) reader.
9pub struct Reader<R> {
10 inner: R,
11}
12
13impl<R> Reader<R> {
14 /// Returns a reference to the underlying reader.
15 ///
16 /// # Examples
17 ///
18 /// ```
19 /// # use std::io;
20 /// use noodles_bgzf::gzi;
21 /// let reader = gzi::io::Reader::new(io::empty());
22 /// let _inner = reader.get_ref();
23 /// ```
24 pub fn get_ref(&self) -> &R {
25 &self.inner
26 }
27
28 /// Returns a mutable reference to the underlying reader.
29 ///
30 /// # Examples
31 ///
32 /// ```
33 /// # use std::io;
34 /// use noodles_bgzf::gzi;
35 /// let mut reader = gzi::io::Reader::new(io::empty());
36 /// let _inner = reader.get_mut();
37 /// ```
38 pub fn get_mut(&mut self) -> &mut R {
39 &mut self.inner
40 }
41
42 /// Returns the underlying reader.
43 ///
44 /// # Examples
45 ///
46 /// ```
47 /// # use std::io;
48 /// use noodles_bgzf::gzi;
49 /// let reader = gzi::io::Reader::new(io::empty());
50 /// let _inner = reader.into_inner();
51 /// ```
52 pub fn into_inner(self) -> R {
53 self.inner
54 }
55}
56
57impl<R> Reader<R>
58where
59 R: Read,
60{
61 /// Creates a gzip index (GZI) reader.
62 ///
63 /// # Examples
64 ///
65 /// ```
66 /// use noodles_bgzf::gzi;
67 /// let data = [0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00];
68 /// let reader = gzi::io::Reader::new(&data[..]);
69 /// ```
70 pub fn new(inner: R) -> Self {
71 Self { inner }
72 }
73
74 /// Reads a gzip index.
75 ///
76 /// The position of the stream is expected to be at the start.
77 ///
78 /// # Examples
79 ///
80 /// ```no_run
81 /// # use std::fs::File;
82 /// use noodles_bgzf::gzi;
83 /// let mut reader = File::open("in.gzi").map(gzi::io::Reader::new)?;
84 /// let index = reader.read_index()?;
85 /// # Ok::<(), std::io::Error>(())
86 /// ```
87 pub fn read_index(&mut self) -> io::Result<Index> {
88 read_index(&mut self.inner)
89 }
90}