Skip to main content

crumbkit_process/
exit.rs

1//! How a run ended.
2
3#[cfg(test)]
4mod tests;
5
6/// How a run's child ended. The caller can tell an exit, a signal, a
7/// timeout and a stop apart. [`Finished::status`](crate::Finished::status)
8/// gives the child's own status as the run reaped it, which a timeout or a
9/// stop also has.
10#[derive(Clone, Copy, Debug, PartialEq, Eq)]
11#[non_exhaustive]
12pub enum Exit {
13    /// The child exited with this code. On Windows a child in a job that
14    /// the crate ended exits with 1.
15    Code(i32),
16    /// A signal, by its number, ended the child, and the run did not send
17    /// it. Only Unix has this exit.
18    Signal(i32),
19    /// The run's deadline passed before the child exited, and the run
20    /// stopped the child.
21    TimedOut,
22    /// The run stopped the child for the reason given.
23    Stopped(Stop),
24}
25
26/// Why a run stopped its child before its deadline.
27#[derive(Clone, Copy, Debug, PartialEq, Eq)]
28#[non_exhaustive]
29pub enum Stop {
30    /// The run's [`Cancel`](crate::Cancel) was cancelled.
31    Cancelled,
32    /// An output stream whose [`OverLimit`](crate::OverLimit) is `Stop`
33    /// passed its limit, or one of its limits when it is delivered as
34    /// lines.
35    OverLimit,
36}
37
38/// How the child process itself ended, as the run reaped it. A run that
39/// stopped the child gives the status that the stop caused, such as the
40/// signal of its first step or the code that the child's handler of that
41/// signal exited with.
42#[derive(Clone, Copy, Debug, PartialEq, Eq)]
43#[non_exhaustive]
44pub enum Status {
45    /// The child exited with this code. On Windows a child in a job that
46    /// the crate ended exits with 1.
47    Code(i32),
48    /// A signal, by its number, ended the child. Only Unix has this status.
49    Signal(i32),
50}
51
52impl Status {
53    /// Returns the code that a POSIX shell gives for this status: the exit
54    /// code, or 128 plus the signal's number, so a child that SIGINT ended
55    /// gives 130.
56    #[must_use]
57    pub const fn shell_code(self) -> i32 {
58        match self {
59            Self::Code(code) => code,
60            Self::Signal(signal) => 128 + signal,
61        }
62    }
63
64    /// Returns the exit of a run whose child ended on its own with this
65    /// status.
66    pub(crate) const fn exit(self) -> Exit {
67        match self {
68            Self::Code(code) => Exit::Code(code),
69            Self::Signal(signal) => Exit::Signal(signal),
70        }
71    }
72}