Skip to content

Commit b5f209c

Browse files
committed
Make app return EXIT_FAILURE on error
1 parent 00e218f commit b5f209c

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

src/error.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,27 @@ impl std::fmt::Display for Error {
5959
std::fmt::Display::fmt(&self.inner, f)
6060
}
6161
}
62+
63+
64+
pub type CliResult = std::result::Result<(), CliError>;
65+
66+
pub struct CliError {
67+
error: Error,
68+
}
69+
70+
impl From<Error> for CliError {
71+
fn from(error: Error) -> Self {
72+
CliError { error }
73+
}
74+
}
75+
76+
impl std::fmt::Debug for CliError {
77+
fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
78+
write!(fmt, "{}.", self.error.kind())?;
79+
for cause in self.error.iter_causes() {
80+
write!(fmt, "\n {}.", cause)?;
81+
}
82+
83+
Ok(())
84+
}
85+
}

src/main.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@ mod error;
22
mod cli;
33
mod sys;
44

5-
use crate::error::{ErrorKind, Result, ResultExt};
5+
use crate::error::{ErrorKind, Result, ResultExt, CliResult};
66

77

8-
fn main() {
8+
fn main() -> CliResult {
99
let matches = cli::app().get_matches();
1010

11-
let result = match matches.subcommand() {
12-
("status", Some(m)) => cmd_status(m),
13-
("dgpu", Some(m)) => cmd_dgpu(m),
14-
("performance", Some(m)) => cmd_perf(m),
15-
("latch", Some(m)) => cmd_latch(m),
11+
match matches.subcommand() {
12+
("status", Some(m)) => cmd_status(m)?,
13+
("dgpu", Some(m)) => cmd_dgpu(m)?,
14+
("performance", Some(m)) => cmd_perf(m)?,
15+
("latch", Some(m)) => cmd_latch(m)?,
1616
_ => unreachable!(),
17-
};
18-
19-
if let Err(e) = result {
20-
eprintln!("Error: {}.", e.kind());
21-
22-
for cause in e.iter_causes() {
23-
eprintln!(" {}.", cause);
24-
}
2517
}
18+
19+
Ok(())
2620
}
2721

2822

0 commit comments

Comments
 (0)