Skip to content

Commit 85cae7f

Browse files
committed
Rework error handling
1 parent 8e02eea commit 85cae7f

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

src/main.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
// License, v. 2.0. If a copy of the MPL was not distributed with this
33
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
44

5+
use std::error::Error;
56
use std::process;
67

8+
const PPERROR: &str = "Unexpected end of data while parsing Git output";
9+
710
fn color(c: i32) {
811
if c >= 0 {
912
print!("\x1b[38;5;{}m", c);
@@ -20,24 +23,18 @@ fn bold(b: bool) {
2023
}
2124
}
2225

23-
fn main() {
24-
let status = match process::Command::new("git")
25-
.args(&["status", "--porcelain=v2", "-z", "--branch", "--untracked-files=all"])
26-
.stdin(process::Stdio::null())
27-
.stderr(process::Stdio::null())
28-
.output() {
29-
Err(e) => {
30-
eprintln!("git: {}", e);
31-
process::exit(1);
32-
},
33-
Ok(output) => {
34-
if !output.status.success() {
35-
// We're most likely not in a Git repo
36-
process::exit(0);
37-
}
38-
String::from_utf8(output.stdout).expect("Failed to decode output from Git")
39-
},
40-
};
26+
fn main() -> Result<(), Box<Error>> {
27+
let output = process::Command::new("git")
28+
.args(&["status", "--porcelain=v2", "-z", "--branch", "--untracked-files=all"])
29+
.stdin(process::Stdio::null())
30+
.stderr(process::Stdio::null())
31+
.output()?;
32+
if !output.status.success() {
33+
// We're most likely not in a Git repo
34+
return Ok(())
35+
}
36+
let status = String::from_utf8(output.stdout)
37+
.ok().ok_or("Invalid UTF-8 while decoding Git output")?;
4138

4239
// Details on the current branch
4340
let mut branch = None;
@@ -57,27 +54,27 @@ fn main() {
5754
match entry.next() {
5855
// Header lines
5956
Some("#") => {
60-
match entry.next().unwrap() {
57+
match entry.next().ok_or(PPERROR)? {
6158
"branch.head" => {
62-
let head = entry.next().unwrap();
59+
let head = entry.next().ok_or(PPERROR)?;
6360
if head != "(detached)" {
6461
branch = Some(head);
6562
}
6663
},
6764
"branch.ab" => {
68-
let a = entry.next().unwrap();
69-
let b = entry.next().unwrap();
70-
ahead = a.parse::<i64>().unwrap().abs();
71-
behind = b.parse::<i64>().unwrap().abs();
65+
let a = entry.next().ok_or(PPERROR)?;
66+
let b = entry.next().ok_or(PPERROR)?;
67+
ahead = a.parse::<i64>()?.abs();
68+
behind = b.parse::<i64>()?.abs();
7269
},
7370
_ => {},
7471
}
7572
},
7673
// File entries
7774
Some("1") | Some("2") => {
78-
let mut xy = entry.next().unwrap().chars();
79-
let x = xy.next().unwrap();
80-
let y = xy.next().unwrap();
75+
let mut xy = entry.next().ok_or(PPERROR)?.chars();
76+
let x = xy.next().ok_or(PPERROR)?;
77+
let y = xy.next().ok_or(PPERROR)?;
8178
if x != '.' {
8279
staged += 1;
8380
}
@@ -140,4 +137,6 @@ fn main() {
140137

141138
color(-1);
142139
print!(")");
140+
141+
Ok(())
143142
}

0 commit comments

Comments
 (0)