use crate::{errors::ChecksumFileError, outputs::OUTPUTS_DIRECTORY_NAME};
use serde::Deserialize;
use std::{
borrow::Cow,
fs::{self, File},
io::Write,
path::Path,
};
pub static CHECKSUM_FILE_EXTENSION: &str = ".sum";
#[derive(Deserialize)]
pub struct ChecksumFile {
pub package_name: String,
}
impl ChecksumFile {
pub fn new(package_name: &str) -> Self {
Self {
package_name: package_name.to_string(),
}
}
pub fn exists_at(&self, path: &Path) -> bool {
let path = self.setup_file_path(path);
path.exists()
}
pub fn read_from(&self, path: &Path) -> Result<String, ChecksumFileError> {
let path = self.setup_file_path(path);
fs::read_to_string(&path).map_err(|_| ChecksumFileError::FileReadError(path.into_owned()))
}
pub fn write_to(&self, path: &Path, checksum: String) -> Result<(), ChecksumFileError> {
let path = self.setup_file_path(path);
let mut file = File::create(&path)?;
file.write_all(checksum.as_bytes())?;
Ok(())
}
pub fn remove(&self, path: &Path) -> Result<bool, ChecksumFileError> {
let path = self.setup_file_path(path);
if !path.exists() {
return Ok(false);
}
fs::remove_file(&path).map_err(|_| ChecksumFileError::FileRemovalError(path.into_owned()))?;
Ok(true)
}
fn setup_file_path<'a>(&self, path: &'a Path) -> Cow<'a, Path> {
let mut path = Cow::from(path);
if path.is_dir() {
if !path.ends_with(OUTPUTS_DIRECTORY_NAME) {
path.to_mut().push(OUTPUTS_DIRECTORY_NAME);
}
path.to_mut()
.push(format!("{}{}", self.package_name, CHECKSUM_FILE_EXTENSION));
}
path
}
}