Browse Source

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!
Ben S 10 years ago
parent
commit
5d0bd37168
3 changed files with 24 additions and 12 deletions
  1. 21 10
      src/dir.rs
  2. 1 1
      src/main.rs
  3. 2 1
      src/output/details.rs

+ 21 - 10
src/dir.rs

@@ -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> {
-        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.
     /// 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))
+    }
+}

+ 1 - 1
src/main.rs

@@ -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

+ 2 - 1
src/output/details.rs

@@ -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);
                 }
                 }