From 4295b243e5d643c8f5901d4245490464301b21d1 Mon Sep 17 00:00:00 2001 From: Benjamin Sago Date: Tue, 27 Jun 2017 18:13:18 +0100 Subject: [PATCH] Make Dir construction a bit cleaner --- src/fs/dir.rs | 21 +++++++++------------ src/fs/file.rs | 2 +- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/fs/dir.rs b/src/fs/dir.rs index 802e583..9314e9e 100644 --- a/src/fs/dir.rs +++ b/src/fs/dir.rs @@ -36,24 +36,21 @@ impl Dir { /// The `read_dir` iterator doesn’t actually yield the `.` and `..` /// entries, so if the user wants to see them, we’ll have to add them /// ourselves after the files have been read. - pub fn read_dir(path: &Path, dots: DotFilter, git: bool) -> IOResult { - let mut paths: Vec = try!(fs::read_dir(path)? - .map(|result| result.map(|entry| entry.path())) - .collect()); + pub fn read_dir(path: PathBuf, dots: DotFilter, git: bool) -> IOResult { + let mut contents: Vec = try!(fs::read_dir(&path)? + .map(|result| result.map(|entry| entry.path())) + .collect()); match dots { - DotFilter::JustFiles => paths.retain(|p| p.file_name().and_then(|name| name.to_str()).map(|s| !s.starts_with('.')).unwrap_or(true)), + DotFilter::JustFiles => contents.retain(|p| p.file_name().and_then(|name| name.to_str()).map(|s| !s.starts_with('.')).unwrap_or(true)), DotFilter::Dotfiles => {/* Don’t add or remove anything */}, DotFilter::DotfilesAndDots => { - paths.insert(0, path.join("..")); - paths.insert(0, path.join(".")); + contents.insert(0, path.join("..")); + contents.insert(0, path.join(".")); } } - Ok(Dir { - contents: paths, - path: path.to_path_buf(), - git: if git { Git::scan(path).ok() } else { None }, - }) + let git = if git { Git::scan(&path).ok() } else { None }; + Ok(Dir { contents, path, git }) } /// Produce an iterator of IO results of trying to read all the files in diff --git a/src/fs/file.rs b/src/fs/file.rs index 1b635b0..853109d 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -95,7 +95,7 @@ impl<'dir> File<'dir> { /// Returns an IO error upon failure, but this shouldn't be used to check /// if a `File` is a directory or not! For that, just use `is_directory()`. pub fn to_dir(&self, dots: DotFilter, scan_for_git: bool) -> IOResult { - Dir::read_dir(&*self.path, dots, scan_for_git) + Dir::read_dir(self.path.clone(), dots, scan_for_git) } /// Whether this file is a regular file on the filesystem - that is, not a -- GitLab