1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use super::{signal_from_exit_status};
use super::std::{io, process, fmt};
use std::fmt::Display;
#[derive(Debug)]
pub enum StatusError {
Code(i32),
Io(io::Error),
Signal(i32),
Unknown,
}
impl Display for StatusError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match *self {
StatusError::Code(code) => write!(f, "Process exited with status code {}", code),
StatusError::Io(ref io_error) => write!(f, "Error executing process: {}", io_error),
StatusError::Signal(signal) => write!(f, "Process terminated by signal {}", signal),
StatusError::Unknown => write!(f, "Process experienced an unknown failure"),
}
}
}
pub fn status(mut command: process::Command) -> Result<(), StatusError> {
match command.status() {
Ok(status) => {
if let Some(code) = status.code() {
if code != 0 {
Err(StatusError::Code(code))
} else {
Ok(())
}
} else {
Err(match signal_from_exit_status(status) {
Some(signal) => StatusError::Signal(signal),
None => StatusError::Unknown,
})
}
}
Err(io_error) => Err(StatusError::Io(io_error)),
}
}