Skip to main content

xtask/
clean.rs

1//! Removing generated output that a phase is about to recreate.
2//!
3//! A leftover tree is worse than a failed removal: it silently joins the next
4//! report or artifact. So only "not found" is ignored.
5
6use std::io::{self, ErrorKind};
7use std::path::Path;
8
9use anyhow::{Context, Result};
10
11pub fn remove_dir_if_present(path: &Path) -> Result<()> {
12    ignore_not_found(std::fs::remove_dir_all(path))
13        .with_context(|| format!("cannot remove {}", path.display()))
14}
15
16pub fn remove_file_if_present(path: &Path) -> Result<()> {
17    ignore_not_found(std::fs::remove_file(path))
18        .with_context(|| format!("cannot remove {}", path.display()))
19}
20
21fn ignore_not_found(result: io::Result<()>) -> io::Result<()> {
22    match result {
23        Err(err) if err.kind() == ErrorKind::NotFound => Ok(()),
24        other => other,
25    }
26}
27
28#[cfg(test)]
29mod tests;