use std::fmt::Debug;
use std::fmt::Display;
use crate::CommandDisplay;
#[cfg(doc)]
use crate::CommandExt;
#[cfg(doc)]
use crate::OutputError;
#[cfg(feature = "miette")]
use miette::Diagnostic;
pub struct ExecError {
command: Box<dyn CommandDisplay + Send + Sync>,
inner: std::io::Error,
}
impl ExecError {
pub fn new(command: Box<dyn CommandDisplay + Send + Sync>, inner: std::io::Error) -> Self {
Self { command, inner }
}
}
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 {
write!(
f,
"Failed to execute `{}`: {}",
self.command.program_quoted(),
self.inner
)
}
}
impl std::error::Error for ExecError {}
#[cfg(feature = "miette")]
impl Diagnostic for ExecError {
fn help<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
Some(Box::new(format!(
"Is {} installed and present on your $PATH?",
self.command.program_quoted()
)))
}
}
#[cfg(test)]
mod tests {
use super::*;
use static_assertions::assert_impl_all;
assert_impl_all!(ExecError: Send, Sync);
}