noodles_tabix/
fs.rs

1//! tabix filesystem operations.
2
3use std::{fs::File, io, path::Path};
4
5use super::{
6    io::{Reader, Writer},
7    Index,
8};
9
10/// Reads the entire contents of a tabix index.
11///
12/// This is a convenience function and is equivalent to opening the file at the given path and
13/// reading the index.
14///
15/// # Examples
16///
17/// ```no_run
18/// use noodles_tabix as tabix;
19/// let index = tabix::fs::read("sample.vcf.gz.tbi")?;
20/// # Ok::<(), std::io::Error>(())
21/// ```
22pub fn read<P>(src: P) -> io::Result<Index>
23where
24    P: AsRef<Path>,
25{
26    let mut reader = File::open(src).map(Reader::new)?;
27    reader.read_index()
28}
29
30/// Writes a tabix index to a file.
31///
32/// This is a convenience function and is equivalent to creating a file at the given path and
33/// writing the index.
34///
35/// # Examples
36///
37/// ```no_run
38/// use noodles_csi::binning_index::index::Header;
39/// use noodles_tabix as tabix;
40/// let index = tabix::Index::builder().set_header(Header::default()).build();
41/// tabix::fs::write("sample.vcf.gz.tbi", &index)?;
42/// # Ok::<(), std::io::Error>(())
43/// ```
44pub fn write<P>(dst: P, index: &Index) -> io::Result<()>
45where
46    P: AsRef<Path>,
47{
48    let mut writer = File::create(dst).map(Writer::new)?;
49    writer.write_index(index)?;
50    Ok(())
51}