noodles_tabix/async/
fs.rs

1//! Async tabix filesystem operations.
2
3use std::path::Path;
4
5use tokio::{fs::File, io};
6
7use super::io::{Reader, Writer};
8use crate::Index;
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/// # #[tokio::main]
19/// # async fn main() -> std::io::Result<()> {
20/// use noodles_tabix as tabix;
21/// let index = tabix::r#async::read("sample.vcf.gz.tbi").await?;
22/// # Ok(())
23/// # }
24/// ```
25pub async fn read<P>(src: P) -> io::Result<Index>
26where
27    P: AsRef<Path>,
28{
29    let mut reader = File::open(src).await.map(Reader::new)?;
30    reader.read_index().await
31}
32
33/// Writes a tabix index to a file.
34///
35/// This is a convenience function and is equivalent to creating a file at the given path and
36/// writing the index.
37///
38/// # Examples
39///
40/// ```no_run
41/// # #[tokio::main]
42/// # async fn main() -> std::io::Result<()> {
43/// use noodles_csi::binning_index::index::Header;
44/// use noodles_tabix as tabix;
45///
46/// let index = tabix::Index::builder().set_header(Header::default()).build();
47/// tabix::r#async::write("sample.vcf.gz.tbi", &index).await?;
48/// # Ok(())
49/// # }
50/// ```
51pub async fn write<P>(dst: P, index: &Index) -> io::Result<()>
52where
53    P: AsRef<Path>,
54{
55    let mut writer = File::create(dst).await.map(Writer::new)?;
56    writer.write_index(index).await?;
57    writer.shutdown().await?;
58    Ok(())
59}