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
64
65
66
67
68
69
70
71
72
73
74
75
use std::fmt::Display;

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

use crate::output_conversion_error::OutputConversionError;
use crate::ExecError;
use crate::OutputError;
use crate::WaitError;

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

/// An error produced by a [`Command`] failure.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
    /// An execution failure, when a [`Command`] fails to start.
    Exec(ExecError),
    /// A failure to wait for a [`Command`].
    ///
    /// See: [`Child::wait`].
    Wait(WaitError),
    /// An output failure, when a [`Command`] fails by returning a non-zero exit code (or in other
    /// cases, when custom validation logic is supplied in methods like
    /// [`CommandExt::output_checked_with`]).
    ///
    /// Note that this is also raised when non-capturing methods like
    /// [`CommandExt::status_checked`] fail.
    Output(OutputError),
    /// An output conversion error, when [`Output`] fails to convert to a custom format as
    /// requested by methods like [`CommandExt::output_checked_utf8`].
    Conversion(OutputConversionError),
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Error::Exec(error) => write!(f, "{}", error),
            Error::Wait(error) => write!(f, "{}", error),
            Error::Output(error) => write!(f, "{}", error),
            Error::Conversion(error) => write!(f, "{}", error),
        }
    }
}

impl From<ExecError> for Error {
    fn from(error: ExecError) -> Self {
        Self::Exec(error)
    }
}

impl From<WaitError> for Error {
    fn from(error: WaitError) -> Self {
        Self::Wait(error)
    }
}

impl From<OutputError> for Error {
    fn from(error: OutputError) -> Self {
        Self::Output(error)
    }
}

impl From<OutputConversionError> for Error {
    fn from(error: OutputConversionError) -> Self {
        Self::Conversion(error)
    }
}

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