💄 Reduce nesting in Snapshot::remove_path

This commit is contained in:
Max Brunsfeld 2021-04-19 12:15:24 -07:00
parent 3e93fb9459
commit 0fd3f55730

View File

@ -258,51 +258,50 @@ impl Snapshot {
}
fn remove_path(&mut self, path: &Path) {
if let Some(mut parent_entry) = path.parent().and_then(|p| self.entry_for_path(p).cloned())
{
let mut edits = Vec::new();
let mut parent_entry = match path.parent().and_then(|p| self.entry_for_path(p).cloned()) {
Some(e) => e,
None => return,
};
let parent_inode = parent_entry.inode();
let mut entry_inode = None;
if let Entry::Dir { children, .. } = &mut parent_entry {
let mut new_children = Vec::new();
for (child_inode, child_name) in children.as_ref() {
if Some(child_name.as_ref()) == path.file_name() {
entry_inode = Some(*child_inode);
} else {
new_children.push((*child_inode, child_name.clone()));
}
let mut edits = Vec::new();
let parent_inode = parent_entry.inode();
let mut entry_inode = None;
if let Entry::Dir { children, .. } = &mut parent_entry {
let mut new_children = Vec::new();
for (child_inode, child_name) in children.as_ref() {
if Some(child_name.as_ref()) == path.file_name() {
entry_inode = Some(*child_inode);
} else {
new_children.push((*child_inode, child_name.clone()));
}
if new_children.iter().any(|c| Some(c.0) == entry_inode) {
entry_inode = None;
}
*children = new_children.into();
edits.push(Edit::Insert(parent_entry));
} else {
unreachable!();
}
if let Some(entry_inode) = entry_inode {
// Recursively remove the orphaned nodes' descendants.
let mut descendant_stack = Vec::new();
descendant_stack.push((entry_inode, parent_inode));
while let Some((inode, parent_inode)) = descendant_stack.pop() {
if let Some(entry) = self.entries.get(&inode) {
if entry.parent() == Some(parent_inode) {
edits.push(Edit::Remove(inode));
if let Entry::Dir { children, .. } = entry {
descendant_stack
.extend(children.iter().map(|c| (c.0, entry.inode())));
}
if new_children.iter().any(|c| Some(c.0) == entry_inode) {
entry_inode = None;
}
*children = new_children.into();
edits.push(Edit::Insert(parent_entry));
} else {
unreachable!();
}
if let Some(entry_inode) = entry_inode {
let mut descendant_stack = Vec::new();
descendant_stack.push((entry_inode, parent_inode));
while let Some((inode, parent_inode)) = descendant_stack.pop() {
if let Some(entry) = self.entries.get(&inode) {
if entry.parent() == Some(parent_inode) {
edits.push(Edit::Remove(inode));
if let Entry::Dir { children, .. } = entry {
descendant_stack.extend(children.iter().map(|c| (c.0, entry.inode())));
}
}
}
}
self.entries.edit(edits);
}
self.entries.edit(edits);
}
fn fmt_entry(
@ -372,19 +371,19 @@ impl FileHandle {
#[derive(Clone, Debug)]
pub enum Entry {
Dir {
parent: Option<u64>,
inode: u64,
parent: Option<u64>,
is_symlink: bool,
is_ignored: bool,
children: Arc<[(u64, Arc<OsStr>)]>,
pending: bool,
},
File {
parent: Option<u64>,
path: PathEntry,
inode: u64,
parent: Option<u64>,
is_symlink: bool,
is_ignored: bool,
path: PathEntry,
},
}