Skip to main content

artifactize_tools/platform/
outcome.rs

1//! Shared process outcomes for standalone and host launchers.
2
3use std::process::{ExitCode, ExitStatus};
4
5/// Return the standalone command’s numeric result to the host process.
6pub fn exit_code(code: u8) -> ExitCode {
7    ExitCode::from(code)
8}
9
10/// How a finished process ended.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12pub enum ProcessEnd {
13    /// It exited with this code.
14    Exited(i32),
15    /// It was ended without an exit code: on Unix by a signal, when known.
16    Signaled(Option<i32>),
17}
18
19/// The reason a process ended and the host's wording for diagnostics.
20#[derive(Debug)]
21pub struct ProcessOutcome {
22    end: ProcessEnd,
23    description: String,
24}
25
26impl ProcessOutcome {
27    /// Translate native wait status at the platform boundary.
28    pub fn from_native(status: ExitStatus) -> Self {
29        Self {
30            end: match status.code() {
31                Some(code) => ProcessEnd::Exited(code),
32                None => ProcessEnd::Signaled(exit_signal(&status)),
33            },
34            description: status.to_string(),
35        }
36    }
37
38    pub fn end(&self) -> ProcessEnd {
39        self.end
40    }
41
42    pub fn code(&self) -> Option<i32> {
43        match self.end {
44            ProcessEnd::Exited(code) => Some(code),
45            ProcessEnd::Signaled(_) => None,
46        }
47    }
48
49    pub fn success(&self) -> bool {
50        self.code() == Some(0)
51    }
52}
53
54impl std::fmt::Display for ProcessOutcome {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        f.write_str(&self.description)
57    }
58}
59
60#[cfg(unix)]
61fn exit_signal(status: &ExitStatus) -> Option<i32> {
62    use std::os::unix::process::ExitStatusExt;
63    status.signal()
64}
65
66#[cfg(windows)]
67fn exit_signal(_status: &ExitStatus) -> Option<i32> {
68    // Windows processes never end with a signal.
69    None
70}