1use 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;