command_error/
try_wait_context.rs

1use std::borrow::Borrow;
2use std::fmt::Debug;
3use std::process::ExitStatus;
4
5#[cfg(doc)]
6use std::process::Child;
7#[cfg(doc)]
8use std::process::Command;
9
10use crate::CommandDisplay;
11use crate::OutputContext;
12
13/// An optional [`ExitStatus`] combined with context about the [`Command`] that produced it.
14///
15/// See also: [`OutputContext`].
16pub struct TryWaitContext {
17    pub(crate) status: Option<ExitStatus>,
18    pub(crate) command: Box<dyn CommandDisplay + Send + Sync>,
19}
20
21impl TryWaitContext {
22    /// Get the result of the [`Child::try_wait`] call.
23    pub fn status(&self) -> Option<ExitStatus> {
24        self.status
25    }
26
27    /// Get a reference to the command contained in this context object, for use in error messages
28    /// or diagnostics.
29    pub fn command(&self) -> &(dyn CommandDisplay + Send + Sync) {
30        self.command.borrow()
31    }
32
33    /// Get the command contained in this context object, for use in error messages or diagnostics.
34    pub fn into_command(self) -> Box<(dyn CommandDisplay + Send + Sync)> {
35        self.command
36    }
37
38    /// If the [`ExitStatus`] is present, get an [`OutputContext`] for constructing error messages.
39    pub fn into_output_context(self) -> Option<OutputContext<ExitStatus>> {
40        self.status.map(|status| OutputContext {
41            output: status,
42            command: self.command,
43        })
44    }
45}
46
47impl Debug for TryWaitContext {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        f.debug_struct("TryWaitContext")
50            .field("status", &self.status)
51            .field("command", &self.command.to_string())
52            .finish()
53    }
54}