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
use std::fmt::Debug;
use std::fmt::Display;

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

/// An error from failing to execute a command. Produced by [`CommandExt`].
///
/// This is a command that fails to start, rather than a command that exits with a non-zero status
/// or similar, like [`OutputError`].
pub struct ExecError {
    pub(crate) command: Box<dyn CommandDisplay>,
    pub(crate) inner: std::io::Error,
}

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

impl Display for ExecError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        // TODO: Should this contain an additional message like
        // "Is `program` installed and present in your `$PATH`?"
        write!(f, "Failed to execute `{}`: {}", self.command, self.inner)
    }
}

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