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.
This commit is contained in:
Ben S 2015-01-28 15:49:17 +00:00
parent f794f5eda6
commit 04b2483d1f

View File

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