Skip to content

Commit b892a63

Browse files
authored
Merge pull request #4 from rye/clean-up-checkout-branch
Centralize result reporting logic
2 parents 2e5d9d7 + ad62a0f commit b892a63

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/main.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ fn similarity(str1: &str, str2: &str) -> f32 {
4747
distance / biggest_len
4848
}
4949

50+
fn interpret_checkout_result(branch: &str, result: Result<(), git2::Error>) {
51+
match result {
52+
Ok(_) => {
53+
println!("Successfully checked out branch {}.", &branch);
54+
}
55+
Err(e) => {
56+
eprintln!("An error occurred while checking out {}: {}", &branch, e);
57+
}
58+
}
59+
}
60+
5061
fn main() {
5162
let mut repository = find_repository().expect("Could not find a repository");
5263

@@ -55,10 +66,8 @@ fn main() {
5566
let mut branch_names: Vec<String> = branch_names(&repository).unwrap();
5667

5768
if branch_names.contains(&branch) {
58-
match checkout(&mut repository, &branch) {
59-
Ok(r) => { println!("Successfully checked out branch {} {:?}", &branch, r); }
60-
Err(e) => { eprintln!("Unable to successfully check out branch: {:?}", e); }
61-
}
69+
let result = checkout(&mut repository, &branch);
70+
interpret_checkout_result(&branch, result);
6271
} else {
6372
branch_names.sort_by(|a, b| {
6473
let a_dist = similarity(&branch, a);
@@ -69,9 +78,7 @@ fn main() {
6978

7079
let lowest_distance_branch = &branch_names[0];
7180

72-
match checkout(&mut repository, &lowest_distance_branch) {
73-
Ok(r) => { println!("Checked out branch {}: {:?}", lowest_distance_branch, r); }
74-
Err(e) => { eprintln!("Error while checking out {}: {:?}", lowest_distance_branch, e); }
75-
}
81+
let result = checkout(&mut repository, &lowest_distance_branch);
82+
interpret_checkout_result(&lowest_distance_branch, result);
7683
}
7784
}

0 commit comments

Comments
 (0)