Răsfoiți Sursa

Compare vectors, not strings

This is good for two reasons: 1) shorter code! 2) it won't fail if any of the filenames aren't valid UTF-8.
Ben S 11 ani în urmă
părinte
comite
04b2483d1f
1 a modificat fișierele cu 7 adăugiri și 7 ștergeri
  1. 7 7
      src/dir.rs

+ 7 - 7
src/dir.rs

@@ -72,7 +72,7 @@ impl Dir {
 /// Container of Git statuses for all the files in this folder's Git repository.
 #[cfg(feature="git")]
 struct Git {
-    statuses: Vec<(String, git2::Status)>,
+    statuses: Vec<(Vec<u8>, git2::Status)>,
 }
 
 #[cfg(feature="git")]
@@ -82,16 +82,16 @@ impl Git {
     /// the files' statuses if one is found.
     fn scan(path: &Path) -> Result<Git, git2::Error> {
         let repo = try!(git2::Repository::discover(path));
-        let statuses = try!(repo.statuses(None));
-
-        Ok(Git { statuses: statuses.iter().map(|e| (e.path().unwrap().to_string(), e.status())).collect() })
+        let statuses = try!(repo.statuses(None)).iter()
+                                                .map(|e| (e.path_bytes().to_vec(), e.status()))
+                                                .collect();
+        Ok(Git { statuses: statuses })
     }
 
     /// Get the status for the file at the given path, if present.
     fn status(&self, path: &Path) -> String {
         let status = self.statuses.iter()
-                                  .find(|p| p.0 == path.as_str().unwrap());
-
+                                  .find(|p| p.0 == path.as_vec());
         match status {
             Some(&(_, s)) => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)),
             None => GREY.paint("--").to_string(),
@@ -103,7 +103,7 @@ impl Git {
     /// directories, which don't really have an 'official' status.
     fn dir_status(&self, dir: &Path) -> String {
         let status = self.statuses.iter()
-                                  .filter(|p| p.0.starts_with(dir.as_str().unwrap()))
+                                  .filter(|p| p.0.starts_with(dir.as_vec()))
                                   .fold(git2::Status::empty(), |a, b| a | b.1);
         match status {
             s => format!("{}{}", Git::index_status(s), Git::working_tree_status(s)),