use std::path::PathBuf;
use std::{fs, io};
use simply_colored::*;
use crate::PathExt as _;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct WritePath {
pub path: PathBuf,
pub contents: String,
}
#[derive(Debug)]
pub struct Analysis {
pub writes: Vec<WritePath>,
}
impl Analysis {
pub fn finish(self) {
for WritePath { path, contents } in self.writes {
let contents = contents.to_string();
if let Err(err) = match fs::remove_file(&path) {
Err(err) if err.kind() == io::ErrorKind::NotFound => Ok(()),
Err(err) => Err(err),
Ok(()) => Ok(()),
} {
log::error!("failed to remove file {}: {err}", path.show());
continue;
}
log::warn!("{RED}removed{RESET} {}", path.show());
let Some(dir) = path.parent() else {
log::error!("failed to obtain parent of {}", path.show());
continue;
};
if let Err(err) = fs::create_dir_all(dir) {
log::error!("failed to create directory for {}: {err}", dir.show());
}
if let Err(err) = fs::write(&path, contents) {
log::error!("failed to write to {}: {err}", path.show());
}
log::info!("wrote to {}", path.show());
}
}
}