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 76 77 78 79 80 81 82 83 84 85 86
use std::borrow::Borrow;
use std::fmt::Debug;
use std::fmt::Display;
use std::process::ExitStatus;
#[cfg(doc)]
use std::process::Command;
#[cfg(doc)]
use std::process::Output;
#[cfg(doc)]
use crate::CommandExt;
use crate::CommandDisplay;
use crate::Error;
use crate::OutputError;
use crate::OutputLike;
/// Context around [`Command`] [`Output`].
///
/// This contains additional information about the command that was run (via a [`CommandDisplay`]
/// object) and can be used to construct error messages (for use in methods like
/// [`CommandExt::output_checked_as`]).
pub struct OutputContext<O> {
pub(crate) output: O,
pub(crate) command: Box<dyn CommandDisplay>,
}
impl<O> OutputContext<O>
where
O: OutputLike + 'static,
{
/// Get the [`OutputLike`] data contained in this context object.
pub fn into_output(self) -> O {
self.output
}
/// Get a reference to the [`OutputLike`] data contained in this context object.
pub fn output(&self) -> &O {
&self.output
}
/// Get the command's [`ExitStatus`].
pub fn status(&self) -> ExitStatus {
self.output.status()
}
/// Get a reference to the command contained in this context object, for use in error messages
/// or diagnostics.
pub fn command(&self) -> &dyn CommandDisplay {
self.command.borrow()
}
/// Construct an error that indicates this command failed, containing information about the
/// command and its output.
///
/// See [`CommandExt`] for examples of the error format.
pub fn error(self) -> Error {
Error::from(OutputError::new(self.command, Box::new(self.output)))
}
/// Construct an error that indicates this command failed, containing information about the
/// command, its output, and the provided error message.
///
/// See [`CommandExt::output_checked_as`] for examples of the error format.
pub fn error_msg<E>(self, message: E) -> Error
where
E: Debug + Display + 'static,
{
Error::from(
OutputError::new(self.command, Box::new(self.output))
.with_user_error(Some(Box::new(message))),
)
}
pub(crate) fn maybe_error_msg<E>(self, message: Option<E>) -> Error
where
E: Debug + Display + 'static,
{
let ret = OutputError::new(self.command, Box::new(self.output));
Error::from(match message {
Some(message) => ret.with_user_error(Some(Box::new(message))),
None => ret,
})
}
}