use std::path::{Path, PathBuf};
use std::{fs::File, io::BufReader};
use crate::helper::types::OutputFmt;
use flate2::read::MultiGzDecoder;
pub fn decode_gzip(path: &Path) -> BufReader<MultiGzDecoder<File>> {
let file = File::open(path).expect("Failed to open file");
let decoder = MultiGzDecoder::new(file);
BufReader::new(decoder)
}
pub fn open_file(path: &Path) -> BufReader<File> {
let file = File::open(path).expect("Failed opening a file");
BufReader::new(file)
}
pub fn create_output_fname(dir: &Path, file: &Path, output_fmt: &OutputFmt) -> PathBuf {
let path = dir.join(
file.file_name()
.expect("Failed parsing filename for output file"),
);
create_output_fname_from_path(&path, output_fmt)
}
pub fn create_output_fname_for_text(dir: &Path, prefix: &Path) -> PathBuf {
dir.join(prefix.with_extension("txt"))
}
pub fn create_output_fname_from_path(path: &Path, output_fmt: &OutputFmt) -> PathBuf {
match output_fmt {
OutputFmt::Fasta | OutputFmt::FastaInt => path.with_extension("fas"),
OutputFmt::Nexus | OutputFmt::NexusInt => path.with_extension("nex"),
OutputFmt::Phylip | OutputFmt::PhylipInt => path.with_extension("phy"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_output_fname() {
let path = Path::new("tests/test_create_output_fname.nex");
let dir = Path::new("tests");
assert_eq!(
create_output_fname(dir, path, &OutputFmt::Fasta),
Path::new("tests/test_create_output_fname.fas")
);
}
}