use flate2::read::GzDecoder;
use std::io::{Read, Result};
use std::path::Path;
pub fn deflate_gzip_file<P: AsRef<Path>>(path: P) -> Result<String> {
let contents = file_contents_as_bytes(path)?;
let mut gz = GzDecoder::new(&contents[..]);
let mut s = String::new();
gz.read_to_string(&mut s)?;
Ok(s)
}
pub fn open_file<P: AsRef<Path>>(path: P) -> Result<std::fs::File> {
if !path.as_ref().exists() {
let path_string: String = path.as_ref().to_string_lossy().to_string();
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
format!("File does not exist: {}", path_string),
));
}
let file = std::fs::File::open(path)?;
Ok(file)
}
pub fn file_contents_as_bytes<P: AsRef<Path>>(path: P) -> Result<Vec<u8>> {
let mut file = open_file(path)?;
let mut contents = Vec::<u8>::new();
file.read_to_end(&mut contents)?;
Ok(contents)
}
pub fn file_contents_as_string<P: AsRef<Path>>(path: P) -> Result<String> {
let mut file = open_file(path)?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
Ok(contents)
}
pub fn option_vector_append<T>(mut orig: Option<Vec<T>>, mut other: Option<Vec<T>>) -> Option<Vec<T>> {
if other.is_some() {
if orig.is_none() {
orig = other.take();
} else {
let mut new_orig = orig.take().unwrap();
let mut new_other = other.take().unwrap();
new_orig.append(&mut new_other);
orig = Some(new_orig);
}
}
orig
}