summaryrefslogtreecommitdiffstats
path: root/src/dir.rs
diff options
context:
space:
mode:
authorBen S <ogham@bsago.me>2015-08-25 11:27:08 +0100
committerBen S <ogham@bsago.me>2015-08-25 11:27:08 +0100
commit5d0bd371687610a627656a9002520564d9943c47 (patch)
tree258a665a13da2843e242fa1c912f1fbf0026d585 /src/dir.rs
parentd547c3f5d788f202de353938eaaedbb67a3624df (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/dir.rs')
-rw-r--r--src/dir.rs31
1 files changed, 21 insertions, 10 deletions
diff --git a/src/dir.rs b/src/dir.rs
index f48bcc4..e6cd649 100644
--- a/src/dir.rs
+++ b/src/dir.rs
@@ -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