Propagate errors that occur during readdir

Fixes #71 - the I/O error should now be displayed as an error, rather than as a panic.

Also, fix some comments.
This commit is contained in:
Ben S 2015-08-26 12:19:23 +01:00
parent 5e1ff9cdcd
commit 5f48bfd8b4

View File

@ -23,20 +23,21 @@ impl Dir {
/// Create a new Dir object filled with all the files in the directory /// Create a new Dir object filled with all the files in the directory
/// pointed to by the given path. Fails if the directory can't be read, or /// pointed to by the given path. Fails if the directory can't be read, or
/// isn't actually a directory. /// isn't actually a directory, or if there's an IO error that occurs
/// while scanning.
pub fn readdir(path: &Path, git: bool) -> io::Result<Dir> { pub fn readdir(path: &Path, git: bool) -> io::Result<Dir> {
fs::read_dir(path).map(|dir_obj| Dir { let reader = try!(fs::read_dir(path));
contents: dir_obj.map(|entry| entry.unwrap().path()).collect(), let contents = try!(reader.map(|e| e.map(|e| e.path())).collect());
Ok(Dir {
contents: contents,
path: path.to_path_buf(), path: path.to_path_buf(),
git: if git { Git::scan(path).ok() } else { None }, git: if git { Git::scan(path).ok() } else { None },
}) })
} }
/// Produce a vector of File objects from an initialised directory, /// Produce an iterator of IO results of trying to read all the files in
/// printing out an error if any of the Files fail to be created. /// this directory.
///
/// Passing in `recurse` means that any directories will be scanned for
/// their contents, as well.
pub fn files<'dir>(&'dir self) -> Files<'dir> { pub fn files<'dir>(&'dir self) -> Files<'dir> {
Files { Files {
inner: self.contents.iter(), inner: self.contents.iter(),