summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2019-07-17 19:47:58 +0200
committerMatthias Beyer <mail@beyermatthias.de>2019-07-17 19:47:58 +0200
commit6c2a2d7f0b65ab0bf7da35b871697abde5b075e1 (patch)
treebe9bc6d569975d4a69ca882f050e5223a86cb8c1
parent90739d66833ea90f47f9c3e00ba32b2c475d11f1 (diff)
Rewrite function to be more rusty
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--src/git.rs24
1 files changed, 11 insertions, 13 deletions
diff --git a/src/git.rs b/src/git.rs
index 35b3dbf..daaceb0 100644
--- a/src/git.rs
+++ b/src/git.rs
@@ -34,19 +34,17 @@ pub fn crawl_git_tree<P: AsRef<Path>>(path: P) -> io::Result<Vec<u8>> {
pub fn parse_ls_tree_output<'a>(output: &'a [u8]) -> Result<Vec<Blob<'a>>, Utf8Error> {
let re = Regex::new(r"^[^ ]+ [^ ]+ ([^\t]+)\t(.+)$").unwrap();
- let mut blobs = Vec::new();
- for line in output.split(|&byte| byte == 0) {
- let line = str::from_utf8(line)?;
-
- if let Some(captures) = re.captures(line) {
- blobs.push(Blob {
- object: captures.get(1).unwrap().as_str(),
- path: Path::new(captures.get(2).unwrap().as_str()),
- })
- }
- }
-
- Ok(blobs)
+ output.split(|byte| *byte == 0)
+ .map(str::from_utf8)
+ .filter_map(|r_l| match r_l {
+ Err(e) => Some(Err(e)),
+ Ok(l) => re.captures(l).map(Ok),
+ })
+ .map(|r_captures| r_captures.map(|captures| Blob {
+ object: captures.get(1).unwrap().as_str(),
+ path: Path::new(captures.get(2).unwrap().as_str()),
+ }))
+ .collect()
}
#[cfg(test)]