Skip to content

Commit

Permalink
filtering and globbing
Browse files Browse the repository at this point in the history
  • Loading branch information
solidiquis committed Dec 2, 2023
1 parent 64c3742 commit f55bd00
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 38 deletions.
107 changes: 69 additions & 38 deletions src/file/tree/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,27 +132,38 @@ impl Tree {
.map_err(Error::InvalidRegex)
.into_report(ErrorCategory::User)?;

let to_remove = match layout {
Layout::Flat => self
.root_id
.descendants(&self.arena)
.filter(|node_id| {
!regex.is_match(self.arena[*node_id].get().path().to_string_lossy().as_ref())
})
.collect::<Vec<_>>(),
_ => self
.root_id
.descendants(&self.arena)
.filter(|node_id| {
let node = self.arena[*node_id].get();
!node.is_dir() && !regex.is_match(node.path().to_string_lossy().as_ref())
})
.collect::<Vec<_>>(),
};
match layout {
Layout::Flat => {
let to_remove = self
.root_id
.descendants(&self.arena)
.skip(1)
.filter(|node_id| {
!regex
.is_match(self.arena[*node_id].get().path().to_string_lossy().as_ref())
})
.collect::<Vec<_>>();

to_remove
.into_iter()
.for_each(|n| n.remove_subtree(&mut self.arena));
to_remove
.into_iter()
.for_each(|n| n.remove(&mut self.arena));
},
_ => {
let to_remove = self
.root_id
.descendants(&self.arena)
.skip(1)
.filter(|node_id| {
let node = self.arena[*node_id].get();
!node.is_dir() && !regex.is_match(node.path().to_string_lossy().as_ref())
})
.collect::<Vec<_>>();

to_remove
.into_iter()
.for_each(|n| n.remove_subtree(&mut self.arena));
},
};

Ok(())
}
Expand All @@ -161,6 +172,7 @@ impl Tree {
let Context {
globbing: Globbing { iglob, .. },
pattern,
layout,
..
} = ctx;

Expand Down Expand Up @@ -197,26 +209,45 @@ impl Tree {
override_builder.build().into_report(ErrorCategory::User)?
};

let no_match = |node_id: &NodeId| {
let dirent = self.arena[*node_id].get();

if dirent.is_dir() {
false
} else {
let matched = overrides.matched(dirent.path(), dirent.is_dir());
!(negated_glob ^ matched.is_whitelist())
}
};
match layout {
Layout::Flat => {
let to_remove = self
.root_id
.descendants(&self.arena)
.skip(1)
.filter(|node_id| {
let dirent = self.arena[*node_id].get();
let matched = overrides.matched(dirent.path(), dirent.is_dir());
!(negated_glob ^ matched.is_whitelist())
})
.collect::<Vec<_>>();

let to_remove = self
.root_id
.descendants(&self.arena)
.filter(no_match)
.collect::<Vec<_>>();
to_remove
.into_iter()
.for_each(|n| n.remove(&mut self.arena));
},
_ => {
let to_remove = self
.root_id
.descendants(&self.arena)
.skip(1)
.filter(|node_id| {
let dirent = self.arena[*node_id].get();

if dirent.is_dir() {
false
} else {
let matched = overrides.matched(dirent.path(), dirent.is_dir());
!(negated_glob ^ matched.is_whitelist())
}
})
.collect::<Vec<_>>();

to_remove
.into_iter()
.for_each(|n| n.remove_subtree(&mut self.arena));
to_remove
.into_iter()
.for_each(|n| n.remove_subtree(&mut self.arena));
},
}

Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ fn flat(file_tree: &file::Tree, ctx: &Context) -> Result<String> {
for node_edge in root.traverse(arena) {
let node_id = match node_edge {
NodeEdge::Start(_) => continue,
NodeEdge::End(id) if id.is_removed(arena) => continue,
NodeEdge::End(id) => id,
};

Expand Down

0 comments on commit f55bd00

Please sign in to comment.