Skip to content

Commit bad15ec

Browse files
committed
refactor: separate error code
Signed-off-by: Aminu 'Seun Joshua <[email protected]>
1 parent a34c8d6 commit bad15ec

File tree

1 file changed

+28
-15
lines changed

1 file changed

+28
-15
lines changed

src/subprocess.rs

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
1-
use std::os::unix::process::ExitStatusExt as _;
2-
31
/// An error representing a subprocess that errored
42
///
53
/// This can be used to propogate a subprocesses exit status.
64
/// When this error is encountered the cli will exit with the status code
75
/// instead of printing an error,
86
#[derive(Debug)]
9-
pub struct ExitStatusError {
10-
status: Option<i32>,
7+
pub enum ExitStatusError {
8+
ExitCode(i32),
9+
Signal(i32),
10+
Unknown,
1111
}
1212

1313
impl ExitStatusError {
14-
#[cfg(windows)]
14+
#[cfg(unix)]
1515
pub(crate) fn new(status: std::process::ExitStatus) -> Self {
16-
Self {
17-
status: status.code(),
16+
use std::os::unix::process::ExitStatusExt as _;
17+
18+
if let Some(code) = status.code() {
19+
Self::ExitCode(code)
20+
} else if let Some(signal) = status.signal() {
21+
Self::Signal(signal)
22+
} else {
23+
Self::Unknown
1824
}
1925
}
2026

21-
#[cfg(unix)]
27+
#[cfg(windows)]
2228
pub(crate) fn new(status: std::process::ExitStatus) -> Self {
23-
Self {
24-
status: status.code().or_else(|| status.signal()),
29+
if let Some(code) = status.code() {
30+
Self::ExitCode(code)
31+
} else {
32+
Self::Unknown
2533
}
2634
}
2735

2836
pub fn code(&self) -> i32 {
29-
self.status.unwrap_or(1)
37+
match self {
38+
Self::ExitCode(code) => *code,
39+
Self::Signal(signal) => *signal,
40+
Self::Unknown => 1,
41+
}
3042
}
3143
}
3244

@@ -35,10 +47,11 @@ impl std::error::Error for ExitStatusError {}
3547
impl std::fmt::Display for ExitStatusError {
3648
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3749
let _ = write!(f, "subprocess exited with status: ");
38-
if let Some(status) = self.status {
39-
writeln!(f, "{status}")
40-
} else {
41-
writeln!(f, "unknown")
50+
51+
match self {
52+
Self::ExitCode(code) => writeln!(f, "{code}"),
53+
Self::Signal(signal) => writeln!(f, "{signal}"),
54+
Self::Unknown => writeln!(f, "unknown"),
4255
}
4356
}
4457
}

0 commit comments

Comments
 (0)