Skip to content

Commit 03505b9

Browse files
author
Mehran Kordi
authored
Replace unwrap calls in asyncgit with error handling - closes #53
1 parent f2dd2b6 commit 03505b9

File tree

23 files changed

+616
-380
lines changed

23 files changed

+616
-380
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
/release
33
.DS_Store
4+
/.idea/

Cargo.lock

Lines changed: 56 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

asyncgit/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ crossbeam-channel = "0.4"
1717
log = "0.4"
1818
is_executable = "0.1"
1919
scopetime = { path = "../scopetime", version = "0.1" }
20-
tempfile = "3.1"
20+
tempfile = "3.1"
21+
thiserror = "1.0"

asyncgit/src/diff.rs

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::error::Result;
12
use crate::{hash, sync, AsyncNotification, FileDiff, CWD};
23
use crossbeam_channel::Sender;
34
use log::trace;
@@ -42,21 +43,22 @@ impl AsyncDiff {
4243
}
4344

4445
///
45-
pub fn last(&mut self) -> Option<(DiffParams, FileDiff)> {
46-
let last = self.last.lock().unwrap();
47-
if let Some(res) = last.clone() {
48-
Some((res.params, res.result))
49-
} else {
50-
None
51-
}
46+
pub fn last(&mut self) -> Result<Option<(DiffParams, FileDiff)>> {
47+
let last = self.last.lock()?;
48+
49+
Ok(match last.clone() {
50+
Some(res) => Some((res.params, res.result)),
51+
None => None,
52+
})
5253
}
5354

5455
///
55-
pub fn refresh(&mut self) {
56-
if let Some(param) = self.get_last_param() {
57-
self.clear_current();
58-
self.request(param);
56+
pub fn refresh(&mut self) -> Result<()> {
57+
if let Ok(Some(param)) = self.get_last_param() {
58+
self.clear_current()?;
59+
self.request(param)?;
5960
}
61+
Ok(())
6062
}
6163

6264
///
@@ -68,16 +70,16 @@ impl AsyncDiff {
6870
pub fn request(
6971
&mut self,
7072
params: DiffParams,
71-
) -> Option<FileDiff> {
73+
) -> Result<Option<FileDiff>> {
7274
trace!("request");
7375

7476
let hash = hash(&params);
7577

7678
{
77-
let mut current = self.current.lock().unwrap();
79+
let mut current = self.current.lock()?;
7880

7981
if current.0 == hash {
80-
return current.1.clone();
82+
return Ok(current.1.clone());
8183
}
8284

8385
current.0 = hash;
@@ -91,25 +93,13 @@ impl AsyncDiff {
9193
rayon_core::spawn(move || {
9294
arc_pending.fetch_add(1, Ordering::Relaxed);
9395

94-
let res =
95-
sync::diff::get_diff(CWD, params.0.clone(), params.1);
96-
let mut notify = false;
97-
{
98-
let mut current = arc_current.lock().unwrap();
99-
if current.0 == hash {
100-
current.1 = Some(res.clone());
101-
notify = true;
102-
}
103-
}
104-
105-
{
106-
let mut last = arc_last.lock().unwrap();
107-
*last = Some(LastResult {
108-
result: res,
109-
hash,
110-
params,
111-
});
112-
}
96+
let notify = AsyncDiff::get_diff_helper(
97+
params,
98+
arc_last,
99+
arc_current,
100+
hash,
101+
)
102+
.expect("error getting diff");
113103

114104
arc_pending.fetch_sub(1, Ordering::Relaxed);
115105

@@ -120,16 +110,49 @@ impl AsyncDiff {
120110
}
121111
});
122112

123-
None
113+
Ok(None)
114+
}
115+
116+
fn get_diff_helper(
117+
params: DiffParams,
118+
arc_last: Arc<
119+
Mutex<Option<LastResult<DiffParams, FileDiff>>>,
120+
>,
121+
arc_current: Arc<Mutex<Request<u64, FileDiff>>>,
122+
hash: u64,
123+
) -> Result<bool> {
124+
let res =
125+
sync::diff::get_diff(CWD, params.0.clone(), params.1)?;
126+
127+
let mut notify = false;
128+
{
129+
let mut current = arc_current.lock()?;
130+
if current.0 == hash {
131+
current.1 = Some(res.clone());
132+
notify = true;
133+
}
134+
}
135+
136+
{
137+
let mut last = arc_last.lock()?;
138+
*last = Some(LastResult {
139+
result: res,
140+
hash,
141+
params,
142+
});
143+
}
144+
145+
Ok(notify)
124146
}
125147

126-
fn get_last_param(&self) -> Option<DiffParams> {
127-
self.last.lock().unwrap().clone().map(|e| e.params)
148+
fn get_last_param(&self) -> Result<Option<DiffParams>> {
149+
Ok(self.last.lock()?.clone().map(|e| e.params))
128150
}
129151

130-
fn clear_current(&mut self) {
131-
let mut current = self.current.lock().unwrap();
152+
fn clear_current(&mut self) -> Result<()> {
153+
let mut current = self.current.lock()?;
132154
current.0 = 0;
133155
current.1 = None;
156+
Ok(())
134157
}
135158
}

asyncgit/src/error.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use thiserror::Error;
2+
3+
#[derive(Error, Debug)]
4+
pub enum Error {
5+
#[error("`{0}`")]
6+
Generic(String),
7+
8+
#[error("io error:{0}")]
9+
Io(#[from] std::io::Error),
10+
11+
#[error("git error:{0}")]
12+
Git(#[from] git2::Error),
13+
}
14+
15+
pub type Result<T> = std::result::Result<T, Error>;
16+
17+
impl<T> From<std::sync::PoisonError<T>> for Error {
18+
fn from(error: std::sync::PoisonError<T>) -> Self {
19+
Error::Generic(format!("poison error: {}", error))
20+
}
21+
}

asyncgit/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![deny(clippy::all)]
66

77
mod diff;
8+
mod error;
89
mod revlog;
910
mod status;
1011
pub mod sync;

0 commit comments

Comments
 (0)