Skip to main content

artifactize_foundation/platform/process/
outcome.rs

1//! Shared process outcomes for standalone and host launchers.
2
3use std::process::ExitStatus;
4
5/// How a finished process ended.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum ProcessEnd {
8    /// It exited with this code.
9    Exited(i32),
10    /// It was ended without an exit code: on Unix by a signal, when known.
11    Signaled(Option<i32>),
12}
13
14/// The reason a process ended and the host's wording for diagnostics.
15#[derive(Debug)]
16pub struct ProcessOutcome {
17    end: ProcessEnd,
18    description: String,
19}
20
21impl ProcessOutcome {
22    /// Translate native wait status at the platform boundary.
23    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    // Windows processes never end with a signal.
64    None
65}