Make Dir construction a bit cleaner

This commit is contained in:
Benjamin Sago 2017-06-27 18:13:18 +01:00
parent 20793ce7f4
commit 4295b243e5
2 changed files with 10 additions and 13 deletions

View File

@ -36,24 +36,21 @@ impl Dir {
/// The `read_dir` iterator doesnt actually yield the `.` and `..` /// The `read_dir` iterator doesnt actually yield the `.` and `..`
/// entries, so if the user wants to see them, well have to add them /// entries, so if the user wants to see them, well have to add them
/// ourselves after the files have been read. /// ourselves after the files have been read.
pub fn read_dir(path: &Path, dots: DotFilter, git: bool) -> IOResult<Dir> { pub fn read_dir(path: PathBuf, dots: DotFilter, git: bool) -> IOResult<Dir> {
let mut paths: Vec<PathBuf> = try!(fs::read_dir(path)? let mut contents: Vec<PathBuf> = try!(fs::read_dir(&path)?
.map(|result| result.map(|entry| entry.path())) .map(|result| result.map(|entry| entry.path()))
.collect()); .collect());
match dots { 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 => {/* Dont add or remove anything */}, DotFilter::Dotfiles => {/* Dont add or remove anything */},
DotFilter::DotfilesAndDots => { DotFilter::DotfilesAndDots => {
paths.insert(0, path.join("..")); contents.insert(0, path.join(".."));
paths.insert(0, path.join(".")); contents.insert(0, path.join("."));
} }
} }
Ok(Dir { let git = if git { Git::scan(&path).ok() } else { None };
contents: paths, Ok(Dir { contents, path, git })
path: path.to_path_buf(),
git: if git { Git::scan(path).ok() } else { None },
})
} }
/// Produce an iterator of IO results of trying to read all the files in /// Produce an iterator of IO results of trying to read all the files in

View File

@ -95,7 +95,7 @@ impl<'dir> File<'dir> {
/// Returns an IO error upon failure, but this shouldn't be used to check /// 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()`. /// 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> { pub fn to_dir(&self, dots: DotFilter, scan_for_git: bool) -> IOResult<Dir> {
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 /// Whether this file is a regular file on the filesystem - that is, not a