command_error/
child_context.rs

1use std::borrow::Borrow;
2use std::fmt::Debug;
3
4#[cfg(doc)]
5use std::process::Child;
6#[cfg(doc)]
7use std::process::Command;
8
9#[cfg(doc)]
10use crate::ChildExt;
11use crate::CommandDisplay;
12#[cfg(doc)]
13use crate::OutputContext;
14
15/// A [`Child`] process combined with context about the [`Command`] that produced it.
16///
17/// The context information stored in this type is used to produce diagnostics in [`ChildExt`].
18///
19/// See: [`OutputContext`].
20pub struct ChildContext<C> {
21    pub(crate) child: C,
22    pub(crate) command: Box<dyn CommandDisplay + Send + Sync>,
23}
24
25impl<C> ChildContext<C> {
26    /// Get the child process.
27    pub fn into_child(self) -> C {
28        self.child
29    }
30
31    /// Get a reference to the child process.
32    pub fn child(&self) -> &C {
33        &self.child
34    }
35
36    /// Get a mutable reference to the child process.
37    pub fn child_mut(&mut self) -> &mut C {
38        &mut self.child
39    }
40
41    /// Get a reference to the command which produced this child process.
42    pub fn command(&self) -> &(dyn CommandDisplay + Send + Sync) {
43        self.command.borrow()
44    }
45}
46
47impl<C> Debug for ChildContext<C>
48where
49    C: Debug,
50{
51    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52        f.debug_struct("ChildContext")
53            .field("child", &self.child)
54            .field("command", &self.command.to_string())
55            .finish()
56    }
57}