From 4b994b808a8b0b03e95067c352e9d230ebec92d6 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Wed, 29 Jul 2020 00:43:12 +0200 Subject: Suggestion: Remove `unwrap`s and return `Result`s from diff methods Extend the `Error` enum with new variants to capture the possible errors from `diff()` and `diff_with_repo()`. --- src/bin/git-sugpatch.rs | 2 +- src/suggestion.rs | 58 +++++++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/bin/git-sugpatch.rs b/src/bin/git-sugpatch.rs index 1e21923..4b0a24b 100644 --- a/src/bin/git-sugpatch.rs +++ b/src/bin/git-sugpatch.rs @@ -21,5 +21,5 @@ fn main() { let suggestion = client.fetch(&url.comment_id).unwrap(); - print!("{}", suggestion.diff()); + print!("{}", suggestion.diff().unwrap()); } diff --git a/src/suggestion.rs b/src/suggestion.rs index 531f4b1..b89bd55 100644 --- a/src/suggestion.rs +++ b/src/suggestion.rs @@ -9,6 +9,20 @@ use thiserror::Error; #[derive(Debug, Error)] pub enum Error { + #[error(transparent)] + Git(#[from] git2::Error), + + #[error("{0} is not a blob")] + GitObjectNotBlob(git2::Oid), + + #[error("{message}")] + BufWriter { + source: std::io::IntoInnerError>>, + message: String, + }, + + #[error("{0} is not valid UTF-8")] + InvalidUtf8(String), } #[derive(Debug, PartialEq)] @@ -37,28 +51,33 @@ pub struct Suggestion { } impl Suggestion { - pub fn diff(&self) -> String { - let repo = Repository::open(".").unwrap(); + pub fn diff(&self) -> Result { + let repo = Repository::open(".")?; self.diff_with_repo(&repo) } - fn diff_with_repo(&self, repo: &Repository) -> String { - let commit = repo.find_commit(self.commit.parse().unwrap()).unwrap(); + fn diff_with_repo(&self, repo: &Repository) -> Result { + let commit = repo.find_commit(self.commit.parse()?)?; let path = Path::new(&self.path); let object = commit - .tree().unwrap() - .get_path(path).unwrap() - .to_object(&repo).unwrap(); + .tree()? + .get_path(path)? + .to_object(&repo)?; - let blob = object.as_blob().unwrap(); + let blob = object.as_blob() + .ok_or_else(|| Error::GitObjectNotBlob(object.id()))?; let blob_reader = BufReader::new(blob.content()); let mut new = BufWriter::new(Vec::new()); - self.apply_to(blob_reader, &mut new).unwrap(); - let new_buffer = new.into_inner().unwrap(); + self.apply_to(blob_reader, &mut new)?; + let new_buffer = new.into_inner() + .map_err(|e| Error::BufWriter { + source: e, + message: "unable to read right side of patch".to_owned(), + })?; let mut diff = Patch::from_blob_and_buffer( blob, @@ -66,13 +85,14 @@ impl Suggestion { &new_buffer, Some(&path), None, - ).unwrap(); - - diff.to_buf() - .unwrap() - .as_str() - .unwrap_or("") - .to_owned() + )?; + + Ok( + diff.to_buf()? + .as_str() + .ok_or_else(|| Error::InvalidUtf8("diff".to_owned()))? + .to_owned() + ) } fn suggestion_patch(&self) -> String { @@ -101,7 +121,7 @@ impl Suggestion { pub fn apply(&self) -> Result<(), Error> { let repo = Repository::open(".").unwrap(); - let diff_text = self.diff_with_repo(&repo); + let diff_text = self.diff_with_repo(&repo).unwrap(); let diff = git2::Diff::from_buffer(diff_text.as_bytes()).unwrap(); repo.apply( @@ -330,7 +350,7 @@ index 89840a2..06acdfc 100644 "#; assert_eq!( - suggestion.diff_with_repo(&repo), + suggestion.diff_with_repo(&repo).unwrap(), expected, ); } -- cgit v1.2.3