noodles_tabix/io/
writer.rs

1mod index;
2mod num;
3
4use std::io::{self, Write};
5
6use noodles_bgzf as bgzf;
7
8use self::index::write_index;
9use crate::Index;
10
11/// A tabix writer.
12pub struct Writer<W>
13where
14    W: Write,
15{
16    inner: bgzf::io::Writer<W>,
17}
18
19impl<W> Writer<W>
20where
21    W: Write,
22{
23    /// Creates a tabix writer.
24    ///
25    /// # Examples
26    ///
27    /// ```
28    /// use noodles_tabix as tabix;
29    /// let writer = tabix::io::Writer::new(Vec::new());
30    /// ```
31    pub fn new(writer: W) -> Self {
32        Self {
33            inner: bgzf::io::Writer::new(writer),
34        }
35    }
36
37    /// Returns a reference to the underlying writer.
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// # use std::io;
43    /// use noodles_tabix as tabix;
44    /// let writer = tabix::io::Writer::new(io::sink());
45    /// let _inner = writer.get_ref();
46    /// ```
47    pub fn get_ref(&self) -> &bgzf::io::Writer<W> {
48        &self.inner
49    }
50
51    /// Returns a mutable reference to the underlying writer.
52    ///
53    /// # Examples
54    ///
55    /// ```
56    /// # use std::io;
57    /// use noodles_tabix as tabix;
58    /// let mut writer = tabix::io::Writer::new(io::sink());
59    /// let _inner = writer.get_mut();
60    /// ```
61    pub fn get_mut(&mut self) -> &mut bgzf::io::Writer<W> {
62        &mut self.inner
63    }
64
65    /// Returns the underlying writer.
66    ///
67    /// # Examples
68    ///
69    /// ```
70    /// # use std::io;
71    /// use noodles_tabix as tabix;
72    /// let writer = tabix::io::Writer::new(io::sink());
73    /// let _inner = writer.into_inner();
74    /// ```
75    pub fn into_inner(self) -> bgzf::io::Writer<W> {
76        self.inner
77    }
78
79    /// Attempts to finish the output stream.
80    ///
81    /// This is typically only manually called if the underlying stream is needed before the writer
82    /// is dropped.
83    ///
84    /// # Examples
85    ///
86    /// ```
87    /// # use std::io;
88    /// use noodles_tabix as tabix;
89    /// let mut writer = tabix::io::Writer::new(Vec::new());
90    /// writer.try_finish()?;
91    /// # Ok::<(), io::Error>(())
92    /// ```
93    pub fn try_finish(&mut self) -> io::Result<()> {
94        self.inner.try_finish()
95    }
96
97    /// Writes a tabix index.
98    ///
99    /// # Examples
100    ///
101    /// ```
102    /// use noodles_csi::binning_index::index::Header;
103    /// use noodles_tabix as tabix;
104    ///
105    /// let mut writer = tabix::io::Writer::new(Vec::new());
106    /// let index = tabix::Index::builder().set_header(Header::default()).build();
107    /// writer.write_index(&index)?;
108    /// # Ok::<(), std::io::Error>(())
109    /// ```
110    pub fn write_index(&mut self, index: &Index) -> io::Result<()> {
111        write_index(&mut self.inner, index)
112    }
113}