artifactize_foundation/platform/process/
outcome.rs1use std::process::ExitStatus;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum ProcessEnd {
8 Exited(i32),
10 Signaled(Option<i32>),
12}
13
14#[derive(Debug)]
16pub struct ProcessOutcome {
17 end: ProcessEnd,
18 description: String,
19}
20
21impl ProcessOutcome {
22 pub fn from_native(status: ExitStatus) -> Self {
24 Self {
25 end: match status.code() {
26 Some(code) => ProcessEnd::Exited(code),
27 None => ProcessEnd::Signaled(exit_signal(&status)),
28 },
29 description: status.to_string(),
30 }
31 }
32
33 pub fn end(&self) -> ProcessEnd {
34 self.end
35 }
36
37 pub fn code(&self) -> Option<i32> {
38 match self.end {
39 ProcessEnd::Exited(code) => Some(code),
40 ProcessEnd::Signaled(_) => None,
41 }
42 }
43
44 pub fn success(&self) -> bool {
45 self.code() == Some(0)
46 }
47}
48
49impl std::fmt::Display for ProcessOutcome {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 f.write_str(&self.description)
52 }
53}
54
55#[cfg(unix)]
56fn exit_signal(status: &ExitStatus) -> Option<i32> {
57 use std::os::unix::process::ExitStatusExt;
58 status.signal()
59}
60
61#[cfg(windows)]
62fn exit_signal(_status: &ExitStatus) -> Option<i32> {
63 None
65}