noodles_fasta/fai/
fs.rs

1//! FAI filesystem operations.
2
3use std::{
4    fs::File,
5    io::{self, BufReader, BufWriter},
6    path::Path,
7};
8
9use super::{
10    io::{Reader, Writer},
11    Index,
12};
13
14/// Reads the entire contents of a FASTA index.
15///
16/// This is a convenience function and is equivalent to opening the file at the given path and
17/// parsing each record.
18///
19/// # Examples
20///
21/// ```no_run
22/// use noodles_fasta::fai;
23/// let index = fai::fs::read("reference.fa.fai")?;
24/// # Ok::<(), std::io::Error>(())
25/// ```
26pub fn read<P>(src: P) -> io::Result<Index>
27where
28    P: AsRef<Path>,
29{
30    let mut reader = File::open(src).map(BufReader::new).map(Reader::new)?;
31    reader.read_index()
32}
33
34/// Writes a FASTA index to a file.
35///
36/// This is a convenience function and is equivalent to creating a file at the given path and
37/// writing the index.
38///
39/// # Examples
40///
41/// ```no_run
42/// use noodles_fasta::fai;
43/// let index = fai::Index::default();
44/// fai::fs::write("reference.fa.fai", &index)?;
45/// # Ok::<(), std::io::Error>(())
46/// ```
47pub fn write<P>(dst: P, index: &Index) -> io::Result<()>
48where
49    P: AsRef<Path>,
50{
51    let mut writer = File::create(dst).map(BufWriter::new).map(Writer::new)?;
52    writer.write_index(index)?;
53    Ok(())
54}