aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorTeddy Wing2020-09-16 02:16:27 +0200
committerTeddy Wing2020-09-16 02:16:27 +0200
commit6fdf7b121bcddbc0328703f34fdb0fee71e03dc8 (patch)
tree98bb34d963c8d4a52c5174614a7409322ec2406e /src
parente68aa0dfd1a200ff781f19ddffd69d01d9796ad3 (diff)
downloadgit-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.
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs26
-rw-r--r--src/main.rs2
2 files changed, 21 insertions, 7 deletions
diff --git a/src/lib.rs b/src/lib.rs
index bc3c9b0..0b64b22 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -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();
}