crumbkit_process/outcome.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. [`ChildFinished::status`](crate::ChildFinished::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 Outcome {
13 /// The child ended on its own with this status, and the run did not
14 /// stop it.
15 Ended(Status),
16 /// The run's deadline passed before the child exited, and the run
17 /// stopped the child.
18 TimedOut,
19 /// The run stopped the child for the reason given.
20 Stopped(StopReason),
21}
22
23/// Why a run stopped its child before its deadline.
24#[derive(Clone, Copy, Debug, PartialEq, Eq)]
25#[non_exhaustive]
26pub enum StopReason {
27 /// The run's [`Cancel`](crate::Cancel) was cancelled.
28 Cancelled,
29 /// An output stream whose [`OverLimit`](crate::OverLimit) is `Stop`
30 /// passed its limit, or one of its limits when it is delivered as
31 /// lines.
32 OverLimit,
33}
34
35/// How the child process itself ended, as the run reaped it. A run that
36/// stopped the child gives the status that the stop caused, such as the
37/// signal of its first step or the code that the child's handler of that
38/// signal exited with.
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
40#[non_exhaustive]
41pub enum Status {
42 /// The child exited with this code. On Windows a child in a job that
43 /// the crate ended exits with 1.
44 Code(i32),
45 /// A signal, by its number, ended the child. Only Unix has this status.
46 Signal(i32),
47}
48
49impl Status {
50 /// Returns the code that a POSIX shell gives for this status: the exit
51 /// code, or 128 plus the signal's number, so a child that SIGINT ended
52 /// gives 130.
53 #[must_use]
54 pub const fn shell_code(self) -> i32 {
55 match self {
56 Self::Code(code) => code,
57 Self::Signal(signal) => 128 + signal,
58 }
59 }
60}