command_error/
wait_error.rs

1use std::fmt::Debug;
2use std::fmt::Display;
3
4#[cfg(doc)]
5use crate::ChildExt;
6use crate::CommandDisplay;
7#[cfg(feature = "miette")]
8use miette::Diagnostic;
9
10/// An error from failing to wait for a command. Produced by [`ChildExt`].
11///
12/// ```
13/// # use pretty_assertions::assert_eq;
14/// # use std::process::Command;
15/// # use command_error::Utf8ProgramAndArgs;
16/// # use command_error::CommandDisplay;
17/// # use command_error::WaitError;
18/// let mut command = Command::new("echo");
19/// command.arg("puppy doggy");
20/// let displayed: Utf8ProgramAndArgs = (&command).into();
21/// let error = WaitError::new(
22///     Box::new(displayed),
23///     std::io::Error::new(
24///         std::io::ErrorKind::NotFound,
25///         "File not found (os error 2)"
26///     ),
27/// );
28/// assert_eq!(
29///     error.to_string(),
30///     "Failed to wait for `echo`: File not found (os error 2)"
31/// );
32/// ```
33pub struct WaitError {
34    pub(crate) command: Box<dyn CommandDisplay + Send + Sync>,
35    pub(crate) inner: std::io::Error,
36}
37
38impl WaitError {
39    /// Construct a new [`WaitError`].
40    pub fn new(command: Box<dyn CommandDisplay + Send + Sync>, inner: std::io::Error) -> Self {
41        Self { command, inner }
42    }
43}
44
45impl Debug for WaitError {
46    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47        f.debug_struct("WaitError")
48            .field("program", &self.command.program())
49            .field("inner", &self.inner)
50            .finish()
51    }
52}
53
54impl Display for WaitError {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(
57            f,
58            "Failed to wait for `{}`: {}",
59            self.command.program_quoted(),
60            self.inner
61        )
62    }
63}
64
65impl std::error::Error for WaitError {}
66
67#[cfg(feature = "miette")]
68impl Diagnostic for WaitError {}
69
70#[cfg(test)]
71mod tests {
72    use super::*;
73    use static_assertions::assert_impl_all;
74
75    assert_impl_all!(WaitError: Send, Sync);
76}