From 4b784ea9f7efd13f3d530016d2fcfae3a17c5d7a Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Wed, 15 Jan 2025 13:11:29 -0600 Subject: [PATCH] fasta/fai/async/fs: Add convenience write function --- noodles-fasta/src/fai/async/fs.rs | 36 +++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) 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(()) +}