use std::path::Path;
pub struct Iter<'a> {
cursor: Option<&'a Path>,
boundary: &'a Path,
}
impl<'a> Iter<'a> {
pub fn new(target: &'a Path, boundary: &'a Path) -> std::io::Result<Self> {
if !target.starts_with(boundary) {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
format!(
"Removal target {:?} must be contained in boundary {:?}",
target, boundary
),
));
}
let cursor = if target == boundary {
None
} else if target.exists() {
Some(target)
} else {
None
};
Ok(Iter { cursor, boundary })
}
}
impl<'a> Iterator for Iter<'a> {
type Item = std::io::Result<&'a Path>;
fn next(&mut self) -> Option<Self::Item> {
use std::io::ErrorKind::*;
match self.cursor.take() {
Some(dir) => {
let next = match std::fs::remove_dir(dir) {
Ok(()) => Some(Ok(dir)),
Err(err) => match err.kind() {
NotFound => Some(Ok(dir)),
_other_error_kind => return Some(Err(err)),
},
};
self.cursor = match dir.parent() {
Some(parent) => {
if parent == self.boundary {
None
} else {
Some(parent)
}
},
None => unreachable!("directory {:?} ran out of parents, this really shouldn't happen before hitting the boundary {:?}"),
};
next
}
None => None,
}
}
}
pub fn empty_until_boundary<'a>(delete_dir: &'a Path, boundary_dir: &'a Path) -> std::io::Result<&'a Path> {
for item in Iter::new(delete_dir, boundary_dir)? {
match item {
Ok(_dir) => continue,
Err(err) => return Err(err),
}
}
Ok(delete_dir)
}