Skip to content

Commit

Permalink
fasta/fai/async/fs: Add convenience write function
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Jan 15, 2025
1 parent 42beb12 commit 4b784ea
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions noodles-fasta/src/fai/async/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<P>(dst: P, index: &Index) -> io::Result<()>
where
P: AsRef<Path>,
{
let mut writer = File::create(dst)
.await
.map(BufWriter::new)
.map(Writer::new)?;

writer.write_index(index).await?;

writer.shutdown().await?;

Ok(())
}

0 comments on commit 4b784ea

Please sign in to comment.