Forráskód Böngészése

Make Dir construction a bit cleaner

Benjamin Sago 8 éve
szülő
commit
4295b243e5
2 módosított fájl, 10 hozzáadás és 13 törlés
  1. 9 12
      src/fs/dir.rs
  2. 1 1
      src/fs/file.rs

+ 9 - 12
src/fs/dir.rs

@@ -36,24 +36,21 @@ impl Dir {
     /// The `read_dir` iterator doesn’t actually yield the `.` and `..`
     /// entries, so if the user wants to see them, we’ll have to add them
     /// ourselves after the files have been read.
-    pub fn read_dir(path: &Path, dots: DotFilter, git: bool) -> IOResult<Dir> {
-        let mut paths: Vec<PathBuf> = try!(fs::read_dir(path)?
-                                              .map(|result| result.map(|entry| entry.path()))
-                                              .collect());
+    pub fn read_dir(path: PathBuf, dots: DotFilter, git: bool) -> IOResult<Dir> {
+        let mut contents: Vec<PathBuf> = try!(fs::read_dir(&path)?
+                                                 .map(|result| result.map(|entry| entry.path()))
+                                                 .collect());
         match dots {
-            DotFilter::JustFiles => paths.retain(|p| p.file_name().and_then(|name| name.to_str()).map(|s| !s.starts_with('.')).unwrap_or(true)),
+            DotFilter::JustFiles => contents.retain(|p| p.file_name().and_then(|name| name.to_str()).map(|s| !s.starts_with('.')).unwrap_or(true)),
             DotFilter::Dotfiles => {/* Don’t add or remove anything */},
             DotFilter::DotfilesAndDots => {
-                paths.insert(0, path.join(".."));
-                paths.insert(0, path.join("."));
+                contents.insert(0, path.join(".."));
+                contents.insert(0, path.join("."));
             }
         }
 
-        Ok(Dir {
-            contents: paths,
-            path: path.to_path_buf(),
-            git: if git { Git::scan(path).ok() } else { None },
-        })
+        let git = if git { Git::scan(&path).ok() } else { None };
+        Ok(Dir { contents, path, git })
     }
 
     /// Produce an iterator of IO results of trying to read all the files in

+ 1 - 1
src/fs/file.rs

@@ -95,7 +95,7 @@ impl<'dir> File<'dir> {
     /// Returns an IO error upon failure, but this shouldn't be used to check
     /// if a `File` is a directory or not! For that, just use `is_directory()`.
     pub fn to_dir(&self, dots: DotFilter, scan_for_git: bool) -> IOResult<Dir> {
-        Dir::read_dir(&*self.path, dots, scan_for_git)
+        Dir::read_dir(self.path.clone(), dots, scan_for_git)
     }
 
     /// Whether this file is a regular file on the filesystem - that is, not a