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