diff --git a/noodles-fasta/src/fai/async/fs.rs b/noodles-fasta/src/fai/async/fs.rs index 203930217..5117b4bd5 100644 --- a/noodles-fasta/src/fai/async/fs.rs +++ b/noodles-fasta/src/fai/async/fs.rs @@ -4,10 +4,10 @@ use std::path::Path; use tokio::{ fs::File, - io::{self, BufReader}, + io::{self, BufReader, BufWriter}, }; -use super::io::Reader; +use super::io::{Reader, Writer}; use crate::fai::Index; /// Reads the entire contents of a FASTA index. @@ -32,3 +32,35 @@ where let mut reader = File::open(src).await.map(BufReader::new).map(Reader::new)?; reader.read_index().await } + +/// Writes a FASTA index to a file. +/// +/// This is a convenience function and is equivalent to creating a file at the given path and +/// writing the index. +/// +/// # Examples +/// +/// ```no_run +/// # #[tokio::main] +/// # async fn main() -> tokio::io::Result<()> { +/// use noodles_fasta::fai; +/// let index = fai::Index::default(); +/// fai::r#async::fs::write("reference.fa.fai", &index).await?; +/// # Ok(()) +/// # } +/// ``` +pub async fn write

(dst: P, index: &Index) -> io::Result<()> +where + P: AsRef, +{ + let mut writer = File::create(dst) + .await + .map(BufWriter::new) + .map(Writer::new)?; + + writer.write_index(index).await?; + + writer.shutdown().await?; + + Ok(()) +}