From 598c81e2498ea8ed4e3cc8de8c3d14046ea00030 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Mon, 12 Oct 2020 17:20:26 +0530 Subject: [PATCH] Simplify error handling Signed-off-by: Harsh Shandilya --- src/main.rs | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5f68f4b..3bd9929 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,12 @@ use git2::Repository; use std::path::Path; use walkdir::WalkDir; -fn main() { +fn main() -> anyhow::Result<()> { let dir = match std::env::args().nth(1) { Some(d) => d, None => { println!("Usage:\n gitice \n"); - return; + return Ok(()); } }; let mut items: Vec = Vec::new(); @@ -16,27 +16,14 @@ fn main() { let path = format!("{}/.git", entry.path().display()); let git_dir = Path::new(&path); if git_dir.exists() { - let repo = match Repository::open(git_dir) { - Ok(repo) => repo, - Err(e) => { - println!("Failed to open repository: {}", e); - return; - } - }; - let head = match repo.head() { - Ok(head) => head, - Err(e) => { - println!("Failed getting repository head: {}", e); - return; - } - }; items.push(format!( "{} = {}", entry.path().to_string_lossy().to_string(), - head.name().unwrap() + Repository::open(git_dir)?.head()?.name().unwrap_or("None") )); } }; } println!("{:#x?}", items); + Ok(()) }