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!
This commit is contained in:
Ben S 2015-08-25 11:27:08 +01:00
parent d547c3f5d7
commit 5d0bd37168
3 changed files with 24 additions and 12 deletions

View File

@ -1,6 +1,7 @@
use std::io; use std::io;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::slice::Iter as SliceIter;
use feature::Git; use feature::Git;
use file::{File, fields}; use file::{File, fields};
@ -36,17 +37,12 @@ impl Dir {
/// ///
/// Passing in `recurse` means that any directories will be scanned for /// Passing in `recurse` means that any directories will be scanned for
/// their contents, as well. /// their contents, as well.
pub fn files(&self, recurse: bool) -> Vec<File> { pub fn files<'dir>(&'dir self, recurse: bool) -> Files<'dir> {
let mut files = vec![]; Files {
inner: self.contents.iter(),
for path in self.contents.iter() { recurse: recurse,
match File::from_path(path, Some(self), recurse) { dir: &self,
Ok(file) => files.push(file),
Err(e) => println!("{}: {}", path.display(), e),
}
} }
files
} }
/// Whether this directory contains a file with the given path. /// 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))
}
}

View File

@ -146,7 +146,7 @@ impl<'dir> Exa<'dir> {
match Dir::readdir(&dir_path, self.options.should_scan_for_git()) { match Dir::readdir(&dir_path, self.options.should_scan_for_git()) {
Ok(ref dir) => { 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); self.options.transform_files(&mut files);
// When recursing, add any directories to the dirs stack // When recursing, add any directories to the dirs stack

View File

@ -99,7 +99,8 @@ impl Details {
// them, so we don't examine any directories that wouldn't // them, so we don't examine any directories that wouldn't
// have their contents listed anyway. // have their contents listed anyway.
if let Some(ref dir) = file.this { 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); filter.transform_files(&mut files);
self.add_files_to_table(table, &files, depth + 1); self.add_files_to_table(table, &files, depth + 1);
} }