use std::path::{Path, PathBuf};
use crate::Error;
pub fn add_extension(path: &Path, extension: &str) -> PathBuf {
let mut new_path = path.to_owned();
match new_path.extension() {
Some(ext) => {
new_path.set_extension(format!("{}.{}", ext.to_string_lossy(), extension));
}
None => {
new_path.set_extension(extension);
}
}
new_path
}
pub fn open_file_for_writing(part_file: &Path) -> Result<std::fs::File, Error> {
if let Some(parent) = part_file.parent() {
std::fs::create_dir_all(parent).map_err(|e| Error::Write {
action: "creating directory",
path: parent.to_path_buf(),
cause: e,
})?;
}
let file = std::fs::OpenOptions::new()
.create(true)
.append(true)
.open(part_file)
.map_err(|e| Error::Write {
action: "opening file for writing",
path: part_file.to_path_buf(),
cause: e,
})?;
file.lock().map_err(|e| Error::Write {
action: "locking file",
path: part_file.to_path_buf(),
cause: e,
})?;
Ok(file)
}