noodles_cram/crai/async.rs
1//! Async CRAM index.
2
3pub mod io;
4
5#[deprecated(since = "0.74.0", note = "Use `crai::r#async::io::Reader` instead.")]
6pub use self::io::Reader;
7
8#[deprecated(since = "0.74.0", note = "Use `crai::r#async::io::Writer` instead.")]
9pub use self::io::Writer;
10
11use std::path::Path;
12
13use tokio::fs::File;
14
15use super::{Index, Record};
16
17/// Reads the entire contents of a CRAM index.
18///
19/// This is a convenience function and is equivalent to opening the file at the given path and
20/// reading the index.
21///
22/// # Examples
23///
24/// ```no_run
25/// # use std::io;
26/// #
27/// # #[tokio::main]
28/// # async fn main() -> io::Result<()> {
29/// use noodles_cram::crai;
30/// let index = crai::r#async::read("sample.cram.crai").await?;
31/// # Ok(())
32/// # }
33/// ```
34pub async fn read<P>(src: P) -> tokio::io::Result<Index>
35where
36 P: AsRef<Path>,
37{
38 let mut reader = File::open(src).await.map(Reader::new)?;
39 reader.read_index().await
40}
41
42/// Writes a CRAM index to a file.
43///
44/// This is a convenience function and is equivalent to creating a file at the given path and
45/// writing the index.
46///
47/// # Examples
48///
49/// ```no_run
50/// # use std::io;
51/// #
52/// # #[tokio::main]
53/// # async fn main() -> io::Result<()> {
54/// use noodles_cram::crai;
55/// let index = crai::Index::default();
56/// crai::r#async::write("sample.cram.crai", &index).await?;
57/// # Ok(())
58/// # }
59/// ```
60pub async fn write<P>(dst: P, index: &[Record]) -> tokio::io::Result<()>
61where
62 P: AsRef<Path>,
63{
64 let mut writer = File::create(dst).await.map(Writer::new)?;
65 writer.write_index(index).await?;
66 writer.shutdown().await?;
67 Ok(())
68}