Skip to content

Commit 487c95b

Browse files
committed
refactor: simplify get_repository error handling using and_then and with_context
1 parent 9f616cb commit 487c95b

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/file_manager/git.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,14 @@ impl GitManager {
164164

165165
fn get_repository(&self) -> Result<Repository> {
166166
let path = PathBuf::from(UPLOAD_DIR).join(self.problem_id.to_string());
167-
if let Ok(repo) = Repository::open(&path) {
168-
let mut config = repo.config()?;
169-
config.set_str("user.name", "admin")?;
170-
config.set_str("user.email", "[email protected]")?;
171-
Ok(repo)
172-
} else {
173-
Err(anyhow::anyhow!(
174-
"Failed to open git repository at {:?}",
175-
path
176-
))
177-
}
167+
Repository::open(&path)
168+
.with_context(|| format!("Failed to open git repository at {:?}", path))
169+
.and_then(|repo| {
170+
let mut config = repo.config()?;
171+
config.set_str("user.name", "admin")?;
172+
config.set_str("user.email", "[email protected]")?;
173+
Ok(repo)
174+
})
178175
}
179176
}
180177

0 commit comments

Comments
 (0)