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