1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::fmt::Debug;
use std::fmt::Display;

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

/// An error from failing to wait for a command. Produced by [`ChildExt`].
///
/// ```
/// # use pretty_assertions::assert_eq;
/// # use std::process::Command;
/// # use command_error::Utf8ProgramAndArgs;
/// # use command_error::CommandDisplay;
/// # use command_error::WaitError;
/// let mut command = Command::new("echo");
/// command.arg("puppy doggy");
/// let displayed: Utf8ProgramAndArgs = (&command).into();
/// let error = WaitError::new(
///     Box::new(displayed),
///     std::io::Error::new(
///         std::io::ErrorKind::NotFound,
///         "File not found (os error 2)"
///     ),
/// );
/// assert_eq!(
///     error.to_string(),
///     "Failed to wait for `echo`: File not found (os error 2)"
/// );
/// ```
pub struct WaitError {
    pub(crate) command: Box<dyn CommandDisplay>,
    pub(crate) inner: std::io::Error,
}

impl WaitError {
    /// Construct a new [`WaitError`].
    pub fn new(command: Box<dyn CommandDisplay>, inner: std::io::Error) -> Self {
        Self { command, inner }
    }
}

impl Debug for WaitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("WaitError")
            .field("program", &self.command.program())
            .field("inner", &self.inner)
            .finish()
    }
}

impl Display for WaitError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Failed to wait for `{}`: {}",
            self.command.program_quoted(),
            self.inner
        )
    }
}

impl std::error::Error for WaitError {}