command-error 0.8.0

Detailed error messages and status checking for `std::process::Command`
Documentation
use std::borrow::Borrow;
use std::fmt::Debug;

#[cfg(doc)]
use std::process::Child;
#[cfg(doc)]
use std::process::Command;

#[cfg(doc)]
use crate::ChildExt;
use crate::CommandDisplay;
#[cfg(doc)]
use crate::OutputContext;

/// A [`Child`] process combined with context about the [`Command`] that produced it.
///
/// The context information stored in this type is used to produce diagnostics in [`ChildExt`].
///
/// See: [`OutputContext`].
pub struct ChildContext<C> {
    child: C,
    command: Box<dyn CommandDisplay + Send + Sync>,
}

impl<C> ChildContext<C> {
    /// Construct a new [`ChildContext`].
    pub fn new(child: C, command: Box<dyn CommandDisplay + Send + Sync>) -> Self {
        Self { child, command }
    }

    /// Get the child process.
    pub fn into_child(self) -> C {
        self.child
    }

    /// Get a reference to the child process.
    pub fn child(&self) -> &C {
        &self.child
    }

    /// Get a mutable reference to the child process.
    pub fn child_mut(&mut self) -> &mut C {
        &mut self.child
    }

    /// Get a reference to the command which produced this child process.
    pub fn command(&self) -> &(dyn CommandDisplay + Send + Sync + 'static) {
        self.command.borrow()
    }

    /// Get a reference to the [`Box`] containing the command which produced this child process.
    ///
    /// This value, unlike the return value from [`ChildContext::command`], can be [`Clone`]d.
    #[expect(clippy::borrowed_box)]
    pub fn command_boxed(&self) -> &Box<dyn CommandDisplay + Send + Sync> {
        &self.command
    }

    /// Get the child and command which produced the child.
    pub fn into_child_and_command(self) -> (C, Box<dyn CommandDisplay + Send + Sync>) {
        (self.child, self.command)
    }
}

impl<C> Debug for ChildContext<C>
where
    C: Debug,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("ChildContext")
            .field("child", &self.child)
            .field("command", &self.command.to_string())
            .finish()
    }
}