Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse FileType from DirEntry #833

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/fs/dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::fs::feature::git::GitCache;
use crate::fs::fields::GitStatus;
use std::fs;
use std::fs::DirEntry;
use std::io;
use std::path::{Path, PathBuf};
use std::slice::Iter as SliceIter;
Expand All @@ -17,7 +18,7 @@ use crate::fs::File;
/// accordingly. (See `File#get_source_files`)
pub struct Dir {
/// A vector of the files that have been read from this directory.
contents: Vec<PathBuf>,
contents: Vec<DirEntry>,

/// The path that was read.
pub path: PathBuf,
Expand All @@ -35,9 +36,7 @@ impl Dir {
pub fn read_dir(path: PathBuf) -> io::Result<Self> {
info!("Reading directory {:?}", &path);

let contents = fs::read_dir(&path)?
.map(|result| result.map(|entry| entry.path()))
.collect::<Result<_, _>>()?;
let contents = fs::read_dir(&path)?.collect::<Result<Vec<_>, _>>()?;

info!("Read directory success {:?}", &path);
Ok(Self { contents, path })
Expand Down Expand Up @@ -67,7 +66,7 @@ impl Dir {

/// Whether this directory contains a file with the given path.
pub fn contains(&self, path: &Path) -> bool {
self.contents.iter().any(|p| p.as_path() == path)
self.contents.iter().any(|p| p.path().as_path() == path)
}

/// Append a path onto the path specified by this directory.
Expand All @@ -80,7 +79,7 @@ impl Dir {
#[allow(clippy::struct_excessive_bools)]
pub struct Files<'dir, 'ig> {
/// The internal iterator over the paths that have been read already.
inner: SliceIter<'dir, PathBuf>,
inner: SliceIter<'dir, DirEntry>,

/// The directory that begat those paths.
dir: &'dir Dir,
Expand Down Expand Up @@ -117,8 +116,9 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
/// varies depending on the dotfile visibility flag)
fn next_visible_file(&mut self) -> Option<Result<File<'dir>, (PathBuf, io::Error)>> {
loop {
if let Some(path) = self.inner.next() {
let filename = File::filename(path);
if let Some(entry) = self.inner.next() {
let path = entry.path();
let filename = File::filename(&path);
if !self.dotfiles && filename.starts_with('.') {
continue;
}
Expand All @@ -131,7 +131,7 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
}

if self.git_ignoring {
let git_status = self.git.map(|g| g.get(path, false)).unwrap_or_default();
let git_status = self.git.map(|g| g.get(&path, false)).unwrap_or_default();
if git_status.unstaged == GitStatus::Ignored {
continue;
}
Expand All @@ -143,6 +143,7 @@ impl<'dir, 'ig> Files<'dir, 'ig> {
filename,
self.deref_links,
self.total_size,
entry.file_type().ok(),
)
.map_err(|e| (path.clone(), e));

Expand Down
Loading
Loading