pub mod archive;
pub mod contigs;
pub mod partition;
pub mod read;
pub mod sequences;
pub mod summary;
pub mod text;
use std::fs::{self, File, OpenOptions};
use std::io::BufWriter;
use std::path::Path;
use anyhow::{Context, Result};
use zip::ZipWriter;
trait FileWriter {
fn create_output_file(&self, path: &Path) -> Result<BufWriter<File>> {
create_parent_directory(path)?;
let file = OpenOptions::new().write(true).create_new(true).open(path);
match file {
Ok(writer) => Ok(BufWriter::new(writer)),
Err(error) => panic!("Failed writing to {}: {}", path.display(), error),
}
}
fn create_zip_writer(&self, output: &Path) -> Result<ZipWriter<File>> {
create_parent_directory(output)?;
let file = OpenOptions::new()
.write(true)
.create_new(true)
.open(output)?;
let zip: ZipWriter<File> = ZipWriter::new(file);
Ok(zip)
}
fn append_output_file(&self, path: &Path) -> Result<BufWriter<File>> {
create_parent_directory(path)?;
let file = OpenOptions::new().append(true).create(true).open(path);
match file {
Ok(writer) => Ok(BufWriter::new(writer)),
Err(error) => panic!("Failed appending to {}: {}", path.display(), error),
}
}
}
fn create_parent_directory(path: &Path) -> Result<()> {
let dir_name = path.parent().expect("Failed creating parent directory");
fs::create_dir_all(dir_name)
.with_context(|| format!("Failed creating an output directory for {}", path.display()))?;
Ok(())
}