diff options
author | Teddy Wing | 2020-09-16 02:16:27 +0200 |
---|---|---|
committer | Teddy Wing | 2020-09-16 02:16:27 +0200 |
commit | 6fdf7b121bcddbc0328703f34fdb0fee71e03dc8 (patch) | |
tree | 98bb34d963c8d4a52c5174614a7409322ec2406e | |
parent | e68aa0dfd1a200ff781f19ddffd69d01d9796ad3 (diff) | |
download | git-todo-6fdf7b121bcddbc0328703f34fdb0fee71e03dc8.tar.bz2 |
lib.rs: Replace top-level `unwrap()`s with `Result`s
Add a new `Error` type that we can return in the failure case. Still
need to work out how to handle errors inside the diff callback.
-rw-r--r-- | Cargo.lock | 56 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/lib.rs | 26 | ||||
-rw-r--r-- | src/main.rs | 2 |
4 files changed, 78 insertions, 7 deletions
@@ -32,6 +32,7 @@ name = "git-todo" version = "0.0.1" dependencies = [ "git2", + "thiserror", ] [[package]] @@ -162,6 +163,55 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" [[package]] +name = "proc-macro2" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36e28516df94f3dd551a587da5357459d9b36d945a7c37c3557928c1c2ff2a2c" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6690e3e9f692504b941dc6c3b188fd28df054f7fb8469ab40680df52fdcc842b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "thiserror" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dfdd070ccd8ccb78f4ad66bf1982dc37f620ef696c6b5028fe2ed83dd3d0d08" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd80fc12f73063ac132ac92aceea36734f04a1d93c1240c6944e23a3b8841793" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] name = "tinyvec" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -186,6 +236,12 @@ dependencies = [ ] [[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + +[[package]] name = "url" version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -5,3 +5,4 @@ edition = "2018" [dependencies] git2 = "0.13.11" +thiserror = "1.0.20" @@ -3,6 +3,14 @@ use std::io::Write; use git2::{Repository, Tree}; +use thiserror::Error; + + +#[derive(Error, Debug)] +pub enum Error { + #[error(transparent)] + Git(#[from] git2::Error), +} pub struct Todos<'a> { @@ -10,8 +18,12 @@ pub struct Todos<'a> { } impl Todos<'_> { - pub fn write_since<W: Write>(&self, tree: Tree<'_>, write_to: &mut W) { - let diff = self.repo.diff_tree_to_workdir(Some(&tree), None).unwrap(); + pub fn write_since<W: Write>( + &self, + tree: Tree<'_>, + write_to: &mut W, + ) -> Result<(), Error> { + let diff = self.repo.diff_tree_to_workdir(Some(&tree), None)?; diff.foreach( &mut |_file, _progress| { @@ -45,12 +57,14 @@ impl Todos<'_> { true } ), - ).unwrap(); + )?; + + Ok(()) } - pub fn master_tree(&self) -> Tree<'_> { - let master = self.repo.find_branch("master", git2::BranchType::Local).unwrap(); + pub fn master_tree(&self) -> Result<Tree<'_>, Error> { + let master = self.repo.find_branch("master", git2::BranchType::Local)?; - master.get().peel_to_tree().unwrap() + Ok(master.get().peel_to_tree()?) } } diff --git a/src/main.rs b/src/main.rs index 48e8f5d..f743256 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,5 +9,5 @@ fn main() { let repo = Repository::open(".").unwrap(); let todos = Todos { repo: &repo }; - todos.write_since(todos.master_tree(), &mut std::io::stdout()); + todos.write_since(todos.master_tree().unwrap(), &mut std::io::stdout()).unwrap(); } |