Lines
100 %
Functions
Branches
//! Removing generated output that a phase is about to recreate.
//!
//! A leftover tree is worse than a failed removal: it silently joins the next
//! report or artifact. So only "not found" is ignored.
use std::io::{self, ErrorKind};
use std::path::Path;
use anyhow::{Context, Result};
pub fn remove_dir_if_present(path: &Path) -> Result<()> {
ignore_not_found(std::fs::remove_dir_all(path))
.with_context(|| format!("cannot remove {}", path.display()))
}
pub fn remove_file_if_present(path: &Path) -> Result<()> {
ignore_not_found(std::fs::remove_file(path))
fn ignore_not_found(result: io::Result<()>) -> io::Result<()> {
match result {
Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
other => other,
#[cfg(test)]
mod tests;