diff options
author | Ben S <ogham@bsago.me> | 2015-08-25 11:27:08 +0100 |
---|---|---|
committer | Ben S <ogham@bsago.me> | 2015-08-25 11:27:08 +0100 |
commit | 5d0bd371687610a627656a9002520564d9943c47 (patch) | |
tree | 258a665a13da2843e242fa1c912f1fbf0026d585 /src | |
parent | d547c3f5d788f202de353938eaaedbb67a3624df (diff) |
Make Dir return an Iterator of files, not Vec
This is part of work to make the flow of files more iterator-able, rather than going in and out of vectors. Here, a Dir returns an iterator of files, rather than a pre-filled vector.
For now, this removes the ability for error messages to be displayed. Will be added in later though!
Diffstat (limited to 'src')
-rw-r--r-- | src/dir.rs | 31 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/output/details.rs | 3 |
3 files changed, 24 insertions, 12 deletions
@@ -1,6 +1,7 @@ use std::io; use std::fs; use std::path::{Path, PathBuf}; +use std::slice::Iter as SliceIter; use feature::Git; use file::{File, fields}; @@ -36,17 +37,12 @@ impl Dir { /// /// Passing in `recurse` means that any directories will be scanned for /// their contents, as well. - pub fn files(&self, recurse: bool) -> Vec<File> { - let mut files = vec![]; - - for path in self.contents.iter() { - match File::from_path(path, Some(self), recurse) { - Ok(file) => files.push(file), - Err(e) => println!("{}: {}", path.display(), e), - } + pub fn files<'dir>(&'dir self, recurse: bool) -> Files<'dir> { + Files { + inner: self.contents.iter(), + recurse: recurse, + dir: &self, } - - files } /// Whether this directory contains a file with the given path. @@ -73,3 +69,18 @@ impl Dir { } } } + + +pub struct Files<'dir> { + inner: SliceIter<'dir, PathBuf>, + recurse: bool, + dir: &'dir Dir, +} + +impl<'dir> Iterator for Files<'dir> { + type Item = io::Result<File<'dir>>; + + fn next(&mut self) -> Option<Self::Item> { + self.inner.next().map(|path| File::from_path(path, Some(self.dir), self.recurse)) + } +}
\ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 720409d..a5f180c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -146,7 +146,7 @@ impl<'dir> Exa<'dir> { match Dir::readdir(&dir_path, self.options.should_scan_for_git()) { Ok(ref dir) => { - let mut files = dir.files(false); + let mut files = dir.files(false).flat_map(|f| f).collect(); self.options.transform_files(&mut files); // When recursing, add any directories to the dirs stack diff --git a/src/output/details.rs b/src/output/details.rs index f7c50b7..2a8850d 100644 --- a/src/output/details.rs +++ b/src/output/details.rs @@ -99,7 +99,8 @@ impl Details { // them, so we don't examine any directories that wouldn't // have their contents listed anyway. if let Some(ref dir) = file.this { - let mut files = dir.files(true); + let mut files = dir.files(true).flat_map(|f| f).collect(); + filter.transform_files(&mut files); self.add_files_to_table(table, &files, depth + 1); } |